228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
# File 'lib/steep/type_inference/method_params.rb', line 228
def self.build(node:, method_type:)
args_node =
case node.type
when :def
node.children[1]
when :defs
node.children[2]
else
raise
end
original = args_node.children args = original.dup
instance = new(args: original, method_type: method_type, forward_arg_type: nil)
unless method_type.type.params
args.each do |arg|
case arg.type
when :arg
name = arg.children[0]
instance.params[name] = PositionalParameter.new(name: name, type: AST::Builtin.any_type, node: arg)
when :optarg
name = arg.children[0]
instance.params[name] = PositionalParameter.new(name: name, type: AST::Builtin.any_type, node: arg)
when :forward_arg
return instance.update(forward_arg_type: true)
end
end
return instance
end
positional_params = method_type.type.params.positional_params
loop do
arg = args.first or break
case arg.type
when :arg
name = arg.children[0]
param = positional_params&.head
case param
when Interface::Function::Params::PositionalParams::Required
instance.params[name] = PositionalParameter.new(name: name, type: param.type, node: arg)
when Interface::Function::Params::PositionalParams::Optional
method_param = PositionalParameter.new(name: name, type: param.type, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
method_param: method_param,
method_type: method_type
)
when Interface::Function::Params::PositionalParams::Rest
method_param = PositionalParameter.new(name: name, type: param.type, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
method_param: method_param,
method_type: method_type
)
when nil
method_param = PositionalParameter.new(name: name, type: nil, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
method_param: method_param,
method_type: method_type
)
end
positional_params = positional_params&.tail
when :optarg
name = arg.children[0]
param = positional_params&.head
case param
when Interface::Function::Params::PositionalParams::Required
method_param = PositionalParameter.new(name: name, type: param.type, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
method_param: method_param,
method_type: method_type
)
when Interface::Function::Params::PositionalParams::Optional
instance.params[name] = PositionalParameter.new(name: name, type: param.type, node: arg)
when Interface::Function::Params::PositionalParams::Rest
method_param = PositionalParameter.new(name: name, type: param.type, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
method_param: method_param,
method_type: method_type
)
when nil
method_param = PositionalParameter.new(name: name, type: nil, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
method_param: method_param,
method_type: method_type
)
end
positional_params = positional_params&.tail
else
break
end
args.shift
end
if (arg = args.first) && arg.type == :forward_arg
forward_params = method_type.type.params.update(positional_params: positional_params)
return instance.update(forward_arg_type: [forward_params, method_type.block])
end
if (arg = args.first) && arg.type == :restarg
name = arg.children[0]
rest_types = [] has_error = false
loop do
param = positional_params&.head
case param
when Interface::Function::Params::PositionalParams::Required
rest_types << param.type
has_error = true
when Interface::Function::Params::PositionalParams::Optional
rest_types << param.type
has_error = true
when Interface::Function::Params::PositionalParams::Rest
rest_types << param.type
positional_params = nil
args.shift
break
when nil
has_error = true
break
end
if positional_params
positional_params = positional_params.tail
else
raise "Fatal error"
end
end
type = rest_types.empty? ? nil : AST::Types::Union.build(types: rest_types)
method_param = PositionalRestParameter.new(name: name, type: type, node: arg)
instance.params[name] = method_param
if has_error
instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
method_param: method_param,
method_type: method_type
)
end
end
if positional_params
instance.errors << Diagnostic::Ruby::MethodArityMismatch.new(node: node, method_type: method_type)
end
keyword_params = method_type.type.params.keyword_params
keywords = keyword_params.keywords
loop do
arg = args.first or break
case arg.type
when :kwarg
name = arg.children[0]
case
when type = keyword_params.requireds[name]
instance.params[name] = KeywordParameter.new(name: name, type: type, node: arg)
keywords.delete(name)
when type = keyword_params.optionals[name]
method_param = KeywordParameter.new(name: name, type: type, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
method_param: method_param,
method_type: method_type
)
keywords.delete(name)
when type = keyword_params.rest
method_param = KeywordParameter.new(name: name, type: type, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
method_param: method_param,
method_type: method_type
)
keywords.delete(name)
else
method_param = KeywordParameter.new(name: name, type: nil, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
method_param: method_param,
method_type: method_type
)
end
when :kwoptarg
name = arg.children[0]
case
when type = keyword_params.requireds[name]
method_param = KeywordParameter.new(name: name, type: type, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
method_param: method_param,
method_type: method_type
)
keywords.delete(name)
when type = keyword_params.optionals[name]
method_param = KeywordParameter.new(name: name, type: type, node: arg)
instance.params[name] = method_param
keywords.delete(name)
when type = keyword_params.rest
method_param = KeywordParameter.new(name: name, type: type, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
method_param: method_param,
method_type: method_type
)
keywords.delete(name)
else
method_param = KeywordParameter.new(name: name, type: nil, node: arg)
instance.params[name] = method_param
instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
method_param: method_param,
method_type: method_type
)
end
else
break
end
args.shift
end
if (arg = args.first) && arg.type == :kwrestarg
name = arg.children[0]
rest_types = [] has_error = false
keywords.each do |keyword|
rest_types << (keyword_params.requireds[keyword] || keyword_params.optionals.fetch(keyword))
has_error = true
end
keywords.clear
if keyword_params.rest
rest_types << keyword_params.rest
else
has_error = true
end
type = rest_types.empty? ? nil : AST::Types::Union.build(types: rest_types)
method_param = KeywordRestParameter.new(name: name, type: type, node: arg)
instance.params[name] = method_param
if has_error
instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
method_param: method_param,
method_type: method_type
)
end
args.shift
else
if !keywords.empty? || keyword_params.rest
instance.errors << Diagnostic::Ruby::MethodArityMismatch.new(
node: node,
method_type: method_type
)
end
end
if (arg = args.first) && arg.type == :blockarg
name = arg.children[0]
if method_type.block
instance.params[name] = BlockParameter.new(
name: name,
type: method_type.block.type,
optional: method_type.block.optional?,
node: arg,
self_type: method_type.block.self_type
)
else
instance.params[name] = BlockParameter.new(
name: name,
type: nil,
optional: nil,
node: arg,
self_type: nil
)
end
end
instance
end
|