Class: Steep::TypeInference::BlockParams

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/type_inference/block_params.rb

Defined Under Namespace

Classes: MultipleParam, Param

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(leading_params:, optional_params:, rest_param:, trailing_params:, block_param:) ⇒ BlockParams

Returns a new instance of BlockParams.



95
96
97
98
99
100
101
# File 'lib/steep/type_inference/block_params.rb', line 95

def initialize(leading_params:, optional_params:, rest_param:, trailing_params:, block_param:)
  @leading_params = leading_params
  @optional_params = optional_params
  @rest_param = rest_param
  @trailing_params = trailing_params
  @block_param = block_param
end

Instance Attribute Details

#block_paramObject (readonly)

Returns the value of attribute block_param.



93
94
95
# File 'lib/steep/type_inference/block_params.rb', line 93

def block_param
  @block_param
end

#leading_paramsObject (readonly)

Returns the value of attribute leading_params.



89
90
91
# File 'lib/steep/type_inference/block_params.rb', line 89

def leading_params
  @leading_params
end

#optional_paramsObject (readonly)

Returns the value of attribute optional_params.



90
91
92
# File 'lib/steep/type_inference/block_params.rb', line 90

def optional_params
  @optional_params
end

#rest_paramObject (readonly)

Returns the value of attribute rest_param.



91
92
93
# File 'lib/steep/type_inference/block_params.rb', line 91

def rest_param
  @rest_param
end

#trailing_paramsObject (readonly)

Returns the value of attribute trailing_params.



92
93
94
# File 'lib/steep/type_inference/block_params.rb', line 92

def trailing_params
  @trailing_params
end

Class Method Details

.from_multiple(node, annotations) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/steep/type_inference/block_params.rb', line 390

def self.from_multiple(node, annotations)
  # @type var params: Array[Param | MultipleParam]
  params = []

  node.children.each do |child|
    if child.type == :mlhs
      params << from_multiple(child, annotations)
    else
      var = child.children.first

      raise unless var.is_a?(Symbol)
      type = annotations.var_type(lvar: var)

      params << Param.new(var: var, node: child, value: nil, type: type)
    end
  end

  MultipleParam.new(node: node, params: params)
end

.from_node(node, annotations:) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/steep/type_inference/block_params.rb', line 113

def self.from_node(node, annotations:)
  # @type var leading_params: Array[Param | MultipleParam]
  leading_params = []
  # @type var optional_params: Array[Param]
  optional_params = []
  # @type var rest_param: Param?
  rest_param = nil
  # @type var trailing_params: Array[Param | MultipleParam]
  trailing_params = []
  # @type var block_param: Param?
  block_param = nil

  default_params = leading_params

  node.children.each do |arg|
    case
    when arg.type == :mlhs
      default_params << from_multiple(arg, annotations)
    when arg.type == :procarg0 && arg.children.size > 1
      default_params << from_multiple(arg, annotations)
    else
      var = arg.children[0]
      type = annotations.var_type(lvar: var)
      case arg.type
      when :arg
        default_params << Param.new(var: var, type: type, value: nil, node: arg)
      when :procarg0
        var = arg.children[0]
        if var.is_a?(Symbol)
          default_params << Param.new(var: var, type: type, value: nil, node: arg)
        else
          var = var.children[0]
          default_params << Param.new(var: var, type: type, value: nil, node: arg)
        end
      when :optarg
        default_params = trailing_params
        optional_params << Param.new(var: var, type: type, value: arg.children.last, node: arg)
      when :restarg
        default_params = trailing_params
        rest_param = Param.new(var: var, type: type, value: nil, node: arg)
      when :blockarg
        block_param = Param.new(var: var, type: type, value: nil, node: arg)
        break
      end
    end
  end

  new(
    leading_params: leading_params,
    optional_params: optional_params,
    rest_param: rest_param,
    trailing_params: trailing_params,
    block_param: block_param
  )
end

Instance Method Details

#each(&block) ⇒ Object



369
370
371
372
373
374
375
# File 'lib/steep/type_inference/block_params.rb', line 369

def each(&block)
  if block
    params.each(&block)
  else
    enum_for :each
  end
end

#each_single_paramObject



377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/steep/type_inference/block_params.rb', line 377

def each_single_param()
  each do |param|
    case param
    when Param
      yield param
    when MultipleParam
      param.each_param do |p|
        yield p
      end
    end
  end
end

#expandable?Boolean

Returns:

  • (Boolean)


356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/steep/type_inference/block_params.rb', line 356

def expandable?
  case
  when leading_params.size + trailing_params.size > 1
    true
  when (leading_params.any? || trailing_params.any?) && rest_param
    true
  when params.size == 1 && params.fetch(0).node.type == :arg
    true
  else
    false
  end
end

#expandable_params?(params_type, factory) ⇒ Boolean

Returns:

  • (Boolean)


340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/steep/type_inference/block_params.rb', line 340

def expandable_params?(params_type, factory)
  if params_type.flat_unnamed_params.size == 1
    type = params_type.required.first or raise
    type = factory.deep_expand_alias(type) || type

    case type
    when AST::Types::Tuple
      type
    when AST::Types::Name::Base
      if AST::Builtin::Array.instance_type?(type)
        type
      end
    end
  end
end

#paramsObject



103
104
105
106
107
108
109
110
111
# File 'lib/steep/type_inference/block_params.rb', line 103

def params
  params = [] #: Array[Param | MultipleParam]
  params.push(*leading_params)
  params.push(*optional_params)
  params.push rest_param if rest_param
  params.push(*trailing_params)
  params.push(block_param) if block_param
  params
end

#params_type(hint: nil) ⇒ Object



169
170
171
# File 'lib/steep/type_inference/block_params.rb', line 169

def params_type(hint: nil)
  params_type0(hint: hint) or params_type0(hint: nil)
end

#params_type0(hint:) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/steep/type_inference/block_params.rb', line 173

def params_type0(hint:)
  # @type var leadings: Array[AST::Types::t]
  # @type var optionals: Array[AST::Types::t]

  if hint
    case
    when leading_params.size == hint.required.size
      leadings = leading_params.map.with_index do |param, index|
        param.type || hint.required[index]
      end
    when !hint.rest && hint.optional.empty? && leading_params.size > hint.required.size
      leadings = leading_params.take(hint.required.size).map.with_index do |param, index|
        param.type || hint.required[index]
      end
    when !hint.rest && hint.optional.empty? && leading_params.size < hint.required.size
      leadings = leading_params.map.with_index do |param, index|
        param.type || hint.required[index]
      end + hint.required.drop(leading_params.size)
    else
      return nil
    end

    case
    when optional_params.size == hint.optional.size
      optionals = optional_params.map.with_index do |param, index|
        param.type || hint.optional[index]
      end
    when !hint.rest && optional_params.size > hint.optional.size
      optionals = optional_params.take(hint.optional.size).map.with_index do |param, index|
        param.type || hint.optional[index]
      end
    when !hint.rest && optional_params.size < hint.optional.size
      optionals = optional_params.map.with_index do |param, index|
        param.type || hint.optional[index]
      end + hint.optional.drop(optional_params.size)
    else
      return nil
    end

    if rest_param
      if hint.rest
        if rest_type = rest_param.type
          if AST::Builtin::Array.instance_type?(rest_type)
            rest_type.is_a?(AST::Types::Name::Instance) or raise
            rest = rest_type.args.first or raise
          end
        end

        rest ||= hint.rest
      end
    end
  else
    leadings = leading_params.map {|param| param.type || AST::Types::Any.instance }
    optionals = optional_params.map {|param| param.type || AST::Types::Any.instance }

    if rest_param
      if rest_type = rest_param.type
        if array = AST::Builtin::Array.instance_type?(rest_type)
          rest = array.args.first or raise
        end
      end
      rest ||= AST::Types::Any.instance
    end
  end

  Interface::Function::Params.build(
    required: leadings,
    optional: optionals,
    rest: rest
  )
end

#untyped_args?(params) ⇒ Boolean

Returns:

  • (Boolean)


410
411
412
413
# File 'lib/steep/type_inference/block_params.rb', line 410

def untyped_args?(params)
  flat = params.flat_unnamed_params
  flat.size == 1 && flat.dig(0, 1)&.is_a?(AST::Types::Any)
end

#zip(params_type, block, factory:) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/steep/type_inference/block_params.rb', line 245

def zip(params_type, block, factory:)
  if trailing_params.any?
    Steep.logger.error "Block definition with trailing required parameters are not supported yet"
  end

  # @type var zip: Array[[Param | MultipleParam, AST::Types::t]]
  zip = []

  if params_type.nil? || untyped_args?(params_type)
    params.each do |param|
      if param == rest_param
        zip << [param, AST::Builtin::Array.instance_type(fill_untyped: true)]
      else
        zip << [param, AST::Builtin.any_type]
      end
    end
    return zip
  end

  if expandable? && (type = expandable_params?(params_type, factory))
    case
    when AST::Builtin::Array.instance_type?(type)
      type.is_a?(AST::Types::Name::Instance) or raise

      type_arg = type.args.fetch(0)
      params.each do |param|
        unless param == rest_param
          zip << [param, AST::Types::Union.build(types: [type_arg, AST::Builtin.nil_type])]
        else
          zip << [param, AST::Builtin::Array.instance_type(type_arg)]
        end
      end
    when type.is_a?(AST::Types::Tuple)
      types = type.types.dup
      (leading_params + optional_params).each do |param|
        ty = types.shift
        if ty
          zip << [param, ty]
        else
          zip << [param, AST::Types::Nil.instance]
        end
      end

      if rest_param
        if types.any?
          union = AST::Types::Union.build(types: types)
          zip << [rest_param, AST::Builtin::Array.instance_type(union)]
        else
          zip << [rest_param, AST::Types::Nil.instance]
        end
      end
    end
  else
    types = params_type.flat_unnamed_params

    (leading_params + optional_params).each do |param|
      typ = types.shift&.last || params_type.rest

      if typ
        zip << [param, typ]
      else
        zip << [param, AST::Builtin.nil_type]
      end
    end

    if rest_param
      if types.empty?
        array = AST::Builtin::Array.instance_type(params_type.rest || AST::Builtin.any_type)
        zip << [rest_param, array]
      else
        union_members = types.map(&:last)
        union_members << params_type.rest if params_type.rest
        union = AST::Types::Union.build(types: union_members)
        array = AST::Builtin::Array.instance_type(union)
        zip << [rest_param, array]
      end
    end
  end

  if block_param
    if block
      proc_type = AST::Types::Proc.new(type: block.type, block: nil, self_type: block.self_type)
      if block.optional?
        proc_type = AST::Types::Union.build(types: [proc_type, AST::Builtin.nil_type])
      end

      zip << [block_param, proc_type]
    else
      zip << [block_param, AST::Builtin.nil_type]
    end
  end

  zip
end