steep 0.27.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/bin/smoke_runner.rb +3 -4
- data/lib/steep.rb +1 -1
- data/lib/steep/ast/builtin.rb +2 -20
- data/lib/steep/ast/types/factory.rb +60 -51
- data/lib/steep/ast/types/intersection.rb +12 -9
- data/lib/steep/ast/types/name.rb +2 -58
- data/lib/steep/ast/types/union.rb +5 -6
- data/lib/steep/errors.rb +14 -0
- data/lib/steep/interface/interface.rb +5 -62
- data/lib/steep/interface/method_type.rb +335 -74
- data/lib/steep/interface/substitution.rb +16 -4
- data/lib/steep/project/completion_provider.rb +1 -1
- data/lib/steep/project/hover_content.rb +1 -1
- data/lib/steep/server/base_worker.rb +1 -0
- data/lib/steep/server/code_worker.rb +2 -0
- data/lib/steep/server/master.rb +10 -3
- data/lib/steep/source.rb +2 -2
- data/lib/steep/subtyping/check.rb +29 -40
- data/lib/steep/type_construction.rb +192 -188
- data/lib/steep/type_inference/block_params.rb +5 -0
- data/lib/steep/version.rb +1 -1
- data/smoke/alias/a.rb +1 -1
- data/smoke/case/a.rb +1 -1
- data/smoke/if/a.rb +1 -1
- data/smoke/module/a.rb +1 -1
- data/smoke/rescue/a.rb +4 -13
- data/steep.gemspec +1 -1
- metadata +5 -5
@@ -90,20 +90,32 @@ module Steep
|
|
90
90
|
|
91
91
|
def except(vars)
|
92
92
|
self.class.new(
|
93
|
-
dictionary: dictionary
|
93
|
+
dictionary: dictionary,
|
94
94
|
instance_type: instance_type,
|
95
95
|
module_type: module_type,
|
96
96
|
self_type: self_type
|
97
|
-
)
|
97
|
+
).except!(vars)
|
98
98
|
end
|
99
99
|
|
100
|
-
def
|
100
|
+
def except!(vars)
|
101
|
+
vars.each do |var|
|
102
|
+
dictionary.delete(var)
|
103
|
+
end
|
104
|
+
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
def merge!(s, overwrite: false)
|
101
109
|
dictionary.transform_values! {|ty| ty.subst(s) }
|
102
110
|
dictionary.merge!(s.dictionary) do |key, a, b|
|
103
111
|
if a == b
|
104
112
|
a
|
105
113
|
else
|
106
|
-
|
114
|
+
if overwrite
|
115
|
+
b
|
116
|
+
else
|
117
|
+
raise "Duplicated key on merge!: #{key}, #{a}, #{b} (#{self})"
|
118
|
+
end
|
107
119
|
end
|
108
120
|
end
|
109
121
|
|
@@ -234,7 +234,7 @@ module Steep
|
|
234
234
|
when AST::Types::Name::Instance
|
235
235
|
type_name = subtyping.factory.type_name_1(type.name)
|
236
236
|
subtyping.factory.definition_builder.build_instance(type_name)
|
237
|
-
when AST::Types::Name::
|
237
|
+
when AST::Types::Name::Singleton
|
238
238
|
type_name = subtyping.factory.type_name_1(type.name)
|
239
239
|
subtyping.factory.definition_builder.build_singleton(type_name)
|
240
240
|
when AST::Types::Name::Interface
|
@@ -92,7 +92,7 @@ module Steep
|
|
92
92
|
method_definition
|
93
93
|
]
|
94
94
|
end
|
95
|
-
when AST::Types::Name::
|
95
|
+
when AST::Types::Name::Singleton
|
96
96
|
method_definition = method_definition_for(factory, receiver_type.name, singleton_method: method_name)
|
97
97
|
if method_definition&.defined_in
|
98
98
|
owner_name = factory.type_name(method_definition.defined_in)
|
data/lib/steep/server/master.rb
CHANGED
@@ -23,7 +23,7 @@ module Steep
|
|
23
23
|
@signature_worker = signature_worker
|
24
24
|
@code_workers = code_workers
|
25
25
|
@worker_to_paths = {}
|
26
|
-
@
|
26
|
+
@shutdown_request_id = nil
|
27
27
|
end
|
28
28
|
|
29
29
|
def start
|
@@ -60,7 +60,14 @@ module Steep
|
|
60
60
|
end
|
61
61
|
|
62
62
|
while job = queue.pop
|
63
|
-
|
63
|
+
if @shutdown_request_id
|
64
|
+
if job[:id] == @shutdown_request_id
|
65
|
+
writer.write(job)
|
66
|
+
break
|
67
|
+
end
|
68
|
+
else
|
69
|
+
writer.write(job)
|
70
|
+
end
|
64
71
|
end
|
65
72
|
|
66
73
|
writer.io.close
|
@@ -155,7 +162,7 @@ module Steep
|
|
155
162
|
|
156
163
|
when "shutdown"
|
157
164
|
queue << { id: id, result: nil }
|
158
|
-
@
|
165
|
+
@shutdown_request_id = id
|
159
166
|
|
160
167
|
when "exit"
|
161
168
|
queue << nil
|
data/lib/steep/source.rb
CHANGED
@@ -38,7 +38,7 @@ module Steep
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def self.parser
|
41
|
-
::Parser::
|
41
|
+
::Parser::Ruby27.new(Builder.new).tap do |parser|
|
42
42
|
parser.diagnostics.all_errors_are_fatal = true
|
43
43
|
parser.diagnostics.ignore_warnings = true
|
44
44
|
end
|
@@ -60,7 +60,7 @@ module Steep
|
|
60
60
|
_, comments, _ = yield_self do
|
61
61
|
buffer = ::Parser::Source::Buffer.new(path.to_s)
|
62
62
|
buffer.source = source_code
|
63
|
-
parser = ::Parser::
|
63
|
+
parser = ::Parser::Ruby27.new
|
64
64
|
|
65
65
|
parser.tokenize(buffer)
|
66
66
|
end
|
@@ -42,9 +42,8 @@ module Steep
|
|
42
42
|
)
|
43
43
|
end
|
44
44
|
when RBS::Definition::Ancestor::Singleton
|
45
|
-
AST::Types::Name::
|
45
|
+
AST::Types::Name::Singleton.new(
|
46
46
|
name: name,
|
47
|
-
constructor: nil,
|
48
47
|
location: nil
|
49
48
|
)
|
50
49
|
end
|
@@ -78,9 +77,8 @@ module Steep
|
|
78
77
|
)
|
79
78
|
end
|
80
79
|
when RBS::Definition::Ancestor::Singleton
|
81
|
-
AST::Types::Name::
|
80
|
+
AST::Types::Name::Singleton.new(
|
82
81
|
name: name,
|
83
|
-
constructor: nil,
|
84
82
|
location: nil
|
85
83
|
)
|
86
84
|
end
|
@@ -180,6 +178,14 @@ module Steep
|
|
180
178
|
constraints: constraints
|
181
179
|
)
|
182
180
|
|
181
|
+
when relation.super_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.super_type.name)
|
182
|
+
constraints.add(relation.super_type.name, sub_type: relation.sub_type)
|
183
|
+
success(constraints: constraints)
|
184
|
+
|
185
|
+
when relation.sub_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.sub_type.name)
|
186
|
+
constraints.add(relation.sub_type.name, super_type: relation.super_type)
|
187
|
+
success(constraints: constraints)
|
188
|
+
|
183
189
|
when relation.sub_type.is_a?(AST::Types::Union)
|
184
190
|
results = relation.sub_type.types.map do |sub_type|
|
185
191
|
check(Relation.new(sub_type: sub_type, super_type: relation.super_type),
|
@@ -232,14 +238,6 @@ module Steep
|
|
232
238
|
results.find(&:failure?)
|
233
239
|
end
|
234
240
|
|
235
|
-
when relation.super_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.super_type.name)
|
236
|
-
constraints.add(relation.super_type.name, sub_type: relation.sub_type)
|
237
|
-
success(constraints: constraints)
|
238
|
-
|
239
|
-
when relation.sub_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.sub_type.name)
|
240
|
-
constraints.add(relation.sub_type.name, super_type: relation.super_type)
|
241
|
-
success(constraints: constraints)
|
242
|
-
|
243
241
|
when relation.super_type.is_a?(AST::Types::Var) || relation.sub_type.is_a?(AST::Types::Var)
|
244
242
|
failure(error: Result::Failure::UnknownPairError.new(relation: relation),
|
245
243
|
trace: trace)
|
@@ -266,7 +264,7 @@ module Steep
|
|
266
264
|
possible_sub_types = case relation.sub_type
|
267
265
|
when AST::Types::Name::Instance
|
268
266
|
instance_super_types(relation.sub_type.name, args: relation.sub_type.args)
|
269
|
-
when AST::Types::Name::
|
267
|
+
when AST::Types::Name::Singleton
|
270
268
|
singleton_super_types(relation.sub_type.name)
|
271
269
|
else
|
272
270
|
[]
|
@@ -399,7 +397,7 @@ module Steep
|
|
399
397
|
case type
|
400
398
|
when AST::Types::Name::Instance
|
401
399
|
factory.definition_builder.build_instance(type_name)
|
402
|
-
when AST::Types::Name::
|
400
|
+
when AST::Types::Name::Singleton
|
403
401
|
factory.definition_builder.build_singleton(type_name)
|
404
402
|
when AST::Types::Name::Interface
|
405
403
|
factory.definition_builder.build_interface(type_name)
|
@@ -484,11 +482,7 @@ module Steep
|
|
484
482
|
if sub_type.name == super_type.name && sub_type.args.size == super_type.args.size
|
485
483
|
sub_type.args.zip(super_type.args)
|
486
484
|
end
|
487
|
-
when sub_type.is_a?(AST::Types::Name::
|
488
|
-
if sub_type.name == super_type.name
|
489
|
-
[]
|
490
|
-
end
|
491
|
-
when sub_type.is_a?(AST::Types::Name::Module) && super_type.is_a?(AST::Types::Name::Module)
|
485
|
+
when sub_type.is_a?(AST::Types::Name::Singleton) && super_type.is_a?(AST::Types::Name::Singleton)
|
492
486
|
if sub_type.name == super_type.name
|
493
487
|
[]
|
494
488
|
end
|
@@ -535,29 +529,24 @@ module Steep
|
|
535
529
|
|
536
530
|
def check_method(name, sub_method, super_method, self_type:, assumption:, trace:, constraints:)
|
537
531
|
trace.method name, sub_method, super_method do
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
trace: trace,
|
548
|
-
constraints: constraints
|
549
|
-
end.yield_self do |results|
|
550
|
-
results.find(&:success?) || results[0]
|
551
|
-
end
|
532
|
+
super_method.method_types.map do |super_type|
|
533
|
+
sub_method.method_types.map do |sub_type|
|
534
|
+
check_generic_method_type name,
|
535
|
+
sub_type,
|
536
|
+
super_type,
|
537
|
+
self_type: self_type,
|
538
|
+
assumption: assumption,
|
539
|
+
trace: trace,
|
540
|
+
constraints: constraints
|
552
541
|
end.yield_self do |results|
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
542
|
+
results.find(&:success?) || results[0]
|
543
|
+
end
|
544
|
+
end.yield_self do |results|
|
545
|
+
if results.all?(&:success?)
|
546
|
+
success constraints: constraints
|
547
|
+
else
|
548
|
+
results.select(&:failure?).last
|
558
549
|
end
|
559
|
-
else
|
560
|
-
raise "aaaaaaaaaaaaaa"
|
561
550
|
end
|
562
551
|
end
|
563
552
|
end
|
@@ -113,6 +113,7 @@ module Steep
|
|
113
113
|
end
|
114
114
|
|
115
115
|
def check_relation(sub_type:, super_type:, constraints: Subtyping::Constraints.empty)
|
116
|
+
Steep.logger.debug { "check_relation: self:#{self_type} |- #{sub_type} <: #{super_type}" }
|
116
117
|
checker.check(Subtyping::Relation.new(sub_type: sub_type, super_type: super_type), self_type: self_type, constraints: constraints)
|
117
118
|
end
|
118
119
|
|
@@ -286,7 +287,6 @@ module Steep
|
|
286
287
|
|
287
288
|
instance_type = AST::Types::Intersection.build(
|
288
289
|
types: [
|
289
|
-
AST::Types::Name::Instance.new(name: module_name, args: module_args),
|
290
290
|
AST::Builtin::Object.instance_type,
|
291
291
|
*module_entry.self_types.map {|module_self|
|
292
292
|
type = case
|
@@ -304,11 +304,12 @@ module Steep
|
|
304
304
|
)
|
305
305
|
end
|
306
306
|
checker.factory.type(type)
|
307
|
-
}
|
307
|
+
},
|
308
|
+
AST::Types::Name::Instance.new(name: module_name, args: module_args)
|
308
309
|
].compact
|
309
310
|
)
|
310
311
|
|
311
|
-
module_type = AST::Types::Name::
|
312
|
+
module_type = AST::Types::Name::Singleton.new(name: module_name)
|
312
313
|
end
|
313
314
|
|
314
315
|
if annots.instance_type
|
@@ -379,7 +380,7 @@ module Steep
|
|
379
380
|
module_def = checker.factory.definition_builder.build_singleton(type_name_)
|
380
381
|
|
381
382
|
instance_type = AST::Types::Name::Instance.new(name: class_name, args: class_args)
|
382
|
-
module_type = AST::Types::Name::
|
383
|
+
module_type = AST::Types::Name::Singleton.new(name: class_name)
|
383
384
|
end
|
384
385
|
|
385
386
|
if annots.instance_type
|
@@ -443,18 +444,26 @@ module Steep
|
|
443
444
|
end
|
444
445
|
|
445
446
|
module_type = case instance_type
|
446
|
-
when AST::Types::Name::
|
447
|
-
|
448
|
-
|
449
|
-
|
447
|
+
when AST::Types::Name::Singleton
|
448
|
+
type_name = checker.factory.type_name_1(instance_type.name)
|
449
|
+
|
450
|
+
case checker.factory.env.class_decls[type_name]
|
451
|
+
when RBS::Environment::ModuleEntry
|
452
|
+
AST::Builtin::Module.instance_type
|
453
|
+
when RBS::Environment::ClassEntry
|
454
|
+
AST::Builtin::Class.instance_type
|
455
|
+
else
|
456
|
+
raise
|
457
|
+
end
|
458
|
+
|
450
459
|
when AST::Types::Name::Instance
|
451
|
-
instance_type.
|
460
|
+
instance_type.to_module
|
452
461
|
else
|
453
|
-
|
462
|
+
return
|
454
463
|
end
|
455
464
|
|
456
465
|
instance_definition = case instance_type
|
457
|
-
when AST::Types::Name::
|
466
|
+
when AST::Types::Name::Singleton
|
458
467
|
type_name = checker.factory.type_name_1(instance_type.name)
|
459
468
|
checker.factory.definition_builder.build_singleton(type_name)
|
460
469
|
when AST::Types::Name::Instance
|
@@ -463,7 +472,7 @@ module Steep
|
|
463
472
|
end
|
464
473
|
|
465
474
|
module_definition = case module_type
|
466
|
-
when AST::Types::Name::
|
475
|
+
when AST::Types::Name::Singleton
|
467
476
|
type_name = checker.factory.type_name_1(instance_type.name)
|
468
477
|
checker.factory.definition_builder.build_singleton(type_name)
|
469
478
|
else
|
@@ -638,7 +647,10 @@ module Steep
|
|
638
647
|
when :__skip__
|
639
648
|
add_typing(node, type: AST::Builtin.any_type)
|
640
649
|
else
|
641
|
-
hint
|
650
|
+
if !hint || hint.is_a?(AST::Types::Void)
|
651
|
+
hint = context.lvar_env.declared_types[name]&.type
|
652
|
+
end
|
653
|
+
|
642
654
|
rhs_result = synthesize(rhs, hint: hint)
|
643
655
|
|
644
656
|
constr = rhs_result.constr.update_lvar_env do |lvar_env|
|
@@ -683,8 +695,8 @@ module Steep
|
|
683
695
|
yield_self do
|
684
696
|
if self_class?(node)
|
685
697
|
module_type = expand_alias(module_context.module_type)
|
686
|
-
type = if module_type.is_a?(AST::Types::Name::
|
687
|
-
AST::Types::Name::
|
698
|
+
type = if module_type.is_a?(AST::Types::Name::Singleton)
|
699
|
+
AST::Types::Name::Singleton.new(name: module_type.name)
|
688
700
|
else
|
689
701
|
module_type
|
690
702
|
end
|
@@ -699,8 +711,8 @@ module Steep
|
|
699
711
|
yield_self do
|
700
712
|
pair = if self_class?(node)
|
701
713
|
module_type = expand_alias(module_context.module_type)
|
702
|
-
type = if module_type.is_a?(AST::Types::Name::
|
703
|
-
AST::Types::Name::
|
714
|
+
type = if module_type.is_a?(AST::Types::Name::Singleton)
|
715
|
+
AST::Types::Name::Singleton.new(name: module_type.name)
|
704
716
|
else
|
705
717
|
module_type
|
706
718
|
end
|
@@ -777,11 +789,10 @@ module Steep
|
|
777
789
|
synthesize(child)
|
778
790
|
end
|
779
791
|
|
780
|
-
super_method = Interface::Interface::
|
781
|
-
method_context.super_method.method_types.map {|method_type|
|
792
|
+
super_method = Interface::Interface::Entry.new(
|
793
|
+
method_types: method_context.super_method.method_types.map {|method_type|
|
782
794
|
checker.factory.method_type(method_type, self_type: self_type)
|
783
|
-
}
|
784
|
-
incompatible: false
|
795
|
+
}
|
785
796
|
)
|
786
797
|
args = TypeInference::SendArgs.from_nodes(node.children.dup)
|
787
798
|
|
@@ -879,7 +890,7 @@ module Steep
|
|
879
890
|
when AST::Types::Name::Instance
|
880
891
|
name = checker.factory.type_name_1(self_type.name)
|
881
892
|
checker.factory.definition_builder.build_singleton(name)
|
882
|
-
when AST::Types::Name::
|
893
|
+
when AST::Types::Name::Singleton
|
883
894
|
name = checker.factory.type_name_1(self_type.name)
|
884
895
|
checker.factory.definition_builder.build_singleton(name)
|
885
896
|
end
|
@@ -1034,12 +1045,13 @@ module Steep
|
|
1034
1045
|
var = node.children[0]
|
1035
1046
|
rhs = node.children[1]
|
1036
1047
|
|
1037
|
-
|
1048
|
+
var_type = context.lvar_env[var.name]
|
1049
|
+
node_type, constr = synthesize(rhs, hint: var_type)
|
1038
1050
|
|
1039
|
-
|
1051
|
+
type = AST::Types::Union.build(types: [var_type, node_type])
|
1040
1052
|
|
1041
1053
|
constr_ = constr.update_lvar_env do |env|
|
1042
|
-
env.assign(var.name, node: node, type:
|
1054
|
+
env.assign(var.name, node: node, type: type) do |declared_type, type, result|
|
1043
1055
|
typing.add_error(
|
1044
1056
|
Errors::IncompatibleAssignment.new(node: node,
|
1045
1057
|
lhs_type: declared_type,
|
@@ -1049,7 +1061,7 @@ module Steep
|
|
1049
1061
|
end
|
1050
1062
|
end
|
1051
1063
|
|
1052
|
-
add_typing(node, type:
|
1064
|
+
add_typing(node, type: type, constr: constr_)
|
1053
1065
|
end
|
1054
1066
|
|
1055
1067
|
when :restarg
|
@@ -1229,6 +1241,17 @@ module Steep
|
|
1229
1241
|
type, constr = synthesize(node.children[0])
|
1230
1242
|
constructor = constr.for_sclass(node, type)
|
1231
1243
|
|
1244
|
+
unless constructor
|
1245
|
+
typing.add_error(
|
1246
|
+
Errors::UnsupportedSyntax.new(
|
1247
|
+
node: node,
|
1248
|
+
message: "sclass receiver must be instance type or singleton type, but type given `#{type}`"
|
1249
|
+
)
|
1250
|
+
)
|
1251
|
+
constr.add_typing(node, type: AST::Builtin.nil_type)
|
1252
|
+
return
|
1253
|
+
end
|
1254
|
+
|
1232
1255
|
constructor.typing.add_context_for_node(node, context: constructor.context)
|
1233
1256
|
constructor.typing.add_context_for_body(node, context: constructor.context)
|
1234
1257
|
|
@@ -1240,7 +1263,7 @@ module Steep
|
|
1240
1263
|
end
|
1241
1264
|
end
|
1242
1265
|
|
1243
|
-
add_typing(node, type: AST::Builtin.nil_type)
|
1266
|
+
constr.add_typing(node, type: AST::Builtin.nil_type)
|
1244
1267
|
end
|
1245
1268
|
|
1246
1269
|
when :self
|
@@ -1509,38 +1532,53 @@ module Steep
|
|
1509
1532
|
test_types << expand_alias(type)
|
1510
1533
|
end
|
1511
1534
|
|
1512
|
-
if
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
var_type.is_a?(AST::Types::Name::Base) && var_type.name == test_type.name
|
1517
|
-
end
|
1518
|
-
if filtered_types.empty?
|
1519
|
-
to_instance_type(test_type)
|
1520
|
-
else
|
1521
|
-
filtered_types
|
1522
|
-
end
|
1535
|
+
if var_names && var_types && test_types.all? {|ty| ty.is_a?(AST::Types::Name::Singleton) }
|
1536
|
+
var_types_in_body = test_types.flat_map do |test_type|
|
1537
|
+
filtered_types = var_types.select do |var_type|
|
1538
|
+
var_type.is_a?(AST::Types::Name::Base) && var_type.name == test_type.name
|
1523
1539
|
end
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
end
|
1540
|
+
if filtered_types.empty?
|
1541
|
+
to_instance_type(test_type)
|
1542
|
+
else
|
1543
|
+
filtered_types
|
1529
1544
|
end
|
1545
|
+
end
|
1530
1546
|
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1547
|
+
var_types.reject! do |type|
|
1548
|
+
var_types_in_body.any? do |test_type|
|
1549
|
+
type.is_a?(AST::Types::Name::Base) && test_type.name == type.name
|
1534
1550
|
end
|
1551
|
+
end
|
1535
1552
|
|
1553
|
+
var_type_in_body = union_type(*var_types_in_body)
|
1554
|
+
type_case_override = var_names.each.with_object({}) do |var_name, hash|
|
1555
|
+
hash[var_name] = var_type_in_body
|
1556
|
+
end
|
1557
|
+
|
1558
|
+
if body
|
1536
1559
|
branch_pairs << clause_constr
|
1537
1560
|
.for_branch(body, type_case_override: type_case_override)
|
1538
1561
|
.synthesize(body, hint: hint)
|
1539
1562
|
else
|
1540
|
-
branch_pairs <<
|
1563
|
+
branch_pairs << Pair.new(type: AST::Builtin.nil_type, constr: clause_constr)
|
1541
1564
|
end
|
1542
1565
|
else
|
1543
|
-
|
1566
|
+
logic = TypeInference::Logic.new(subtyping: checker)
|
1567
|
+
|
1568
|
+
truthys, falseys = logic.nodes(node: ::AST::Node.new(:begin, tests))
|
1569
|
+
truthy_env, _ = logic.environments(truthy_vars: truthys.vars,
|
1570
|
+
falsey_vars: falseys.vars,
|
1571
|
+
lvar_env: clause_constr.context.lvar_env)
|
1572
|
+
|
1573
|
+
if body
|
1574
|
+
branch_pairs << constr
|
1575
|
+
.update_lvar_env { truthy_env }
|
1576
|
+
.for_branch(body)
|
1577
|
+
.tap {|constr| typing.add_context_for_node(body, context: constr.context) }
|
1578
|
+
.synthesize(body, hint: hint)
|
1579
|
+
else
|
1580
|
+
branch_pairs << Pair.new(type: AST::Builtin.nil_type, constr: clause_constr)
|
1581
|
+
end
|
1544
1582
|
end
|
1545
1583
|
end
|
1546
1584
|
|
@@ -1620,7 +1658,7 @@ module Steep
|
|
1620
1658
|
instance_types = exn_types.map do |type|
|
1621
1659
|
type = expand_alias(type)
|
1622
1660
|
case
|
1623
|
-
when type.is_a?(AST::Types::Name::
|
1661
|
+
when type.is_a?(AST::Types::Name::Singleton)
|
1624
1662
|
to_instance_type(type)
|
1625
1663
|
else
|
1626
1664
|
AST::Builtin.any_type
|
@@ -1687,7 +1725,7 @@ module Steep
|
|
1687
1725
|
AST::Types::Any.new
|
1688
1726
|
else
|
1689
1727
|
each = checker.factory.interface(collection_type, private: true).methods[:each]
|
1690
|
-
method_type = (each&.
|
1728
|
+
method_type = (each&.method_types || []).find {|type| type.block && type.block.type.params.first_param }
|
1691
1729
|
method_type&.yield_self do |method_type|
|
1692
1730
|
method_type.block.type.params.first_param&.type
|
1693
1731
|
end
|
@@ -1779,9 +1817,22 @@ module Steep
|
|
1779
1817
|
end
|
1780
1818
|
|
1781
1819
|
when :irange, :erange
|
1782
|
-
|
1783
|
-
|
1784
|
-
|
1820
|
+
begin_node, end_node = node.children
|
1821
|
+
|
1822
|
+
constr = self
|
1823
|
+
begin_type, constr = if begin_node
|
1824
|
+
constr.synthesize(begin_node)
|
1825
|
+
else
|
1826
|
+
[AST::Builtin.nil_type, constr]
|
1827
|
+
end
|
1828
|
+
end_type, constr = if end_node
|
1829
|
+
constr.synthesize(end_node)
|
1830
|
+
else
|
1831
|
+
[AST::Builtin.nil_type, constr]
|
1832
|
+
end
|
1833
|
+
|
1834
|
+
type = AST::Builtin::Range.instance_type(union_type(begin_type, end_type))
|
1835
|
+
add_typing(node, type: type, constr: constr)
|
1785
1836
|
|
1786
1837
|
when :regexp
|
1787
1838
|
each_child_node(node) do |child|
|
@@ -1875,8 +1926,8 @@ module Steep
|
|
1875
1926
|
param_type = hint.params.required[0]
|
1876
1927
|
interface = checker.factory.interface(param_type, private: true)
|
1877
1928
|
method = interface.methods[value.children[0]]
|
1878
|
-
if method
|
1879
|
-
return_types = method.
|
1929
|
+
if method
|
1930
|
+
return_types = method.method_types.select {|method_type|
|
1880
1931
|
method_type.params.each_type.count == 0
|
1881
1932
|
}.map(&:return_type)
|
1882
1933
|
|
@@ -2384,139 +2435,68 @@ module Steep
|
|
2384
2435
|
def type_method_call(node, method_name:, receiver_type:, method:, args:, block_params:, block_body:, topdown_hint:)
|
2385
2436
|
node_range = node.loc.expression.yield_self {|l| l.begin_pos..l.end_pos }
|
2386
2437
|
|
2387
|
-
|
2388
|
-
|
2389
|
-
|
2390
|
-
|
2438
|
+
results = method.method_types.flat_map do |method_type|
|
2439
|
+
Steep.logger.tagged method_type.to_s do
|
2440
|
+
zips = args.zips(method_type.params, method_type.block&.type)
|
2441
|
+
|
2442
|
+
zips.map do |arg_pairs|
|
2391
2443
|
typing.new_child(node_range) do |child_typing|
|
2392
|
-
with_new_typing(child_typing).
|
2393
|
-
|
2394
|
-
|
2395
|
-
|
2396
|
-
|
2397
|
-
|
2398
|
-
|
2399
|
-
|
2400
|
-
|
2401
|
-
|
2444
|
+
ret = self.with_new_typing(child_typing).try_method_type(
|
2445
|
+
node,
|
2446
|
+
receiver_type: receiver_type,
|
2447
|
+
method_type: method_type,
|
2448
|
+
args: args,
|
2449
|
+
arg_pairs: arg_pairs,
|
2450
|
+
block_params: block_params,
|
2451
|
+
block_body: block_body,
|
2452
|
+
child_typing: child_typing,
|
2453
|
+
topdown_hint: topdown_hint
|
2454
|
+
)
|
2402
2455
|
|
2403
|
-
|
2404
|
-
constr.typing.save!
|
2405
|
-
[type,
|
2406
|
-
update_lvar_env { constr.context.lvar_env },
|
2407
|
-
error]
|
2408
|
-
else
|
2409
|
-
types = results.map(&:first)
|
2456
|
+
raise unless ret.is_a?(Array) && ret[1].is_a?(TypeConstruction)
|
2410
2457
|
|
2411
|
-
|
2412
|
-
constr.typing.save!
|
2458
|
+
result, constr = ret
|
2413
2459
|
|
2414
|
-
|
2415
|
-
|
2416
|
-
nil]
|
2460
|
+
[result, constr, method_type]
|
2461
|
+
end
|
2417
2462
|
end
|
2418
2463
|
end
|
2464
|
+
end
|
2419
2465
|
|
2420
|
-
|
2421
|
-
|
2422
|
-
|
2423
|
-
|
2424
|
-
|
2466
|
+
unless results.empty?
|
2467
|
+
result, constr, method_type = results.find {|result, _, _| !result.is_a?(Errors::Base) } || results.last
|
2468
|
+
else
|
2469
|
+
method_type = method.method_types.last
|
2470
|
+
constr = self.with_new_typing(typing.new_child(node_range))
|
2471
|
+
result = Errors::IncompatibleArguments.new(node: node, receiver_type: receiver_type, method_type: method_type)
|
2472
|
+
end
|
2473
|
+
constr.typing.save!
|
2474
|
+
|
2475
|
+
case result
|
2476
|
+
when Errors::Base
|
2477
|
+
if method.method_types.size == 1
|
2478
|
+
typing.add_error result
|
2479
|
+
type = case method_type.return_type
|
2480
|
+
when AST::Types::Var
|
2481
|
+
AST::Builtin.any_type
|
2482
|
+
else
|
2483
|
+
method_type.return_type
|
2484
|
+
end
|
2485
|
+
else
|
2486
|
+
typing.add_error Errors::UnresolvedOverloading.new(node: node,
|
2487
|
+
receiver_type: expand_self(receiver_type),
|
2425
2488
|
method_name: method_name,
|
2426
|
-
|
2427
|
-
|
2428
|
-
args: args,
|
2429
|
-
block_params: block_params,
|
2430
|
-
block_body: block_body,
|
2431
|
-
topdown_hint: false)
|
2432
|
-
end
|
2433
|
-
end
|
2434
|
-
|
2435
|
-
successes = results.reject {|_, _, error| error }
|
2436
|
-
unless successes.empty?
|
2437
|
-
types = successes.map(&:first)
|
2438
|
-
constr = successes[0][1]
|
2439
|
-
constr.typing.save!
|
2440
|
-
|
2441
|
-
[AST::Types::Intersection.build(types: types),
|
2442
|
-
update_lvar_env { constr.context.lvar_env },
|
2443
|
-
nil]
|
2444
|
-
else
|
2445
|
-
type, constr, error = results.first
|
2446
|
-
constr.typing.save!
|
2447
|
-
|
2448
|
-
[type,
|
2449
|
-
update_lvar_env { constr.context.lvar_env },
|
2450
|
-
error]
|
2451
|
-
end
|
2489
|
+
method_types: method.method_types)
|
2490
|
+
type = AST::Builtin.any_type
|
2452
2491
|
end
|
2453
2492
|
|
2454
|
-
|
2455
|
-
|
2456
|
-
|
2457
|
-
|
2458
|
-
|
2459
|
-
|
2460
|
-
|
2461
|
-
typing.new_child(node_range) do |child_typing|
|
2462
|
-
ret = self.with_new_typing(child_typing).try_method_type(
|
2463
|
-
node,
|
2464
|
-
receiver_type: receiver_type,
|
2465
|
-
method_type: method_type,
|
2466
|
-
args: args,
|
2467
|
-
arg_pairs: arg_pairs,
|
2468
|
-
block_params: block_params,
|
2469
|
-
block_body: block_body,
|
2470
|
-
child_typing: child_typing,
|
2471
|
-
topdown_hint: topdown_hint
|
2472
|
-
)
|
2473
|
-
|
2474
|
-
raise unless ret.is_a?(Array) && ret[1].is_a?(TypeConstruction)
|
2475
|
-
|
2476
|
-
result, constr = ret
|
2477
|
-
|
2478
|
-
[result, constr, method_type]
|
2479
|
-
end
|
2480
|
-
end
|
2481
|
-
end
|
2482
|
-
end
|
2483
|
-
|
2484
|
-
unless results.empty?
|
2485
|
-
result, constr, method_type = results.find {|result, _, _| !result.is_a?(Errors::Base) } || results.last
|
2486
|
-
else
|
2487
|
-
method_type = method.types.last
|
2488
|
-
constr = self.with_new_typing(typing.new_child(node_range))
|
2489
|
-
result = Errors::IncompatibleArguments.new(node: node, receiver_type: receiver_type, method_type: method_type)
|
2490
|
-
end
|
2491
|
-
constr.typing.save!
|
2492
|
-
|
2493
|
-
case result
|
2494
|
-
when Errors::Base
|
2495
|
-
if method.types.size == 1
|
2496
|
-
typing.add_error result
|
2497
|
-
type = case method_type.return_type
|
2498
|
-
when AST::Types::Var
|
2499
|
-
AST::Builtin.any_type
|
2500
|
-
else
|
2501
|
-
method_type.return_type
|
2502
|
-
end
|
2503
|
-
else
|
2504
|
-
typing.add_error Errors::UnresolvedOverloading.new(node: node,
|
2505
|
-
receiver_type: expand_self(receiver_type),
|
2506
|
-
method_name: method_name,
|
2507
|
-
method_types: method.types)
|
2508
|
-
type = AST::Builtin.any_type
|
2509
|
-
end
|
2510
|
-
|
2511
|
-
[type,
|
2512
|
-
update_lvar_env { constr.context.lvar_env },
|
2513
|
-
result]
|
2514
|
-
else # Type
|
2515
|
-
[result,
|
2516
|
-
update_lvar_env { constr.context.lvar_env },
|
2517
|
-
nil]
|
2518
|
-
end
|
2519
|
-
end
|
2493
|
+
[type,
|
2494
|
+
update_lvar_env { constr.context.lvar_env },
|
2495
|
+
result]
|
2496
|
+
else # Type
|
2497
|
+
[result,
|
2498
|
+
update_lvar_env { constr.context.lvar_env },
|
2499
|
+
nil]
|
2520
2500
|
end
|
2521
2501
|
end
|
2522
2502
|
|
@@ -2703,16 +2683,40 @@ module Steep
|
|
2703
2683
|
if block_params && method_type.block
|
2704
2684
|
block_annotations = source.annotations(block: node, factory: checker.factory, current_module: current_namespace)
|
2705
2685
|
block_params_ = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations)
|
2706
|
-
block_param_hint = block_params_.params_type(
|
2707
|
-
hint: topdown_hint ? method_type.block.type.params : nil
|
2708
|
-
)
|
2709
2686
|
|
2710
|
-
|
2711
|
-
|
2712
|
-
|
2713
|
-
|
2714
|
-
|
2715
|
-
|
2687
|
+
unless block_params_
|
2688
|
+
return [
|
2689
|
+
Errors::UnsupportedSyntax.new(
|
2690
|
+
node: block_params,
|
2691
|
+
message: "Unsupported block params pattern, probably masgn?"
|
2692
|
+
),
|
2693
|
+
constr
|
2694
|
+
]
|
2695
|
+
end
|
2696
|
+
|
2697
|
+
pairs = block_params_.zip(method_type.block.type.params)
|
2698
|
+
|
2699
|
+
unless pairs
|
2700
|
+
return [
|
2701
|
+
Errors::IncompatibleBlockParameters.new(node: node, method_type: method_type),
|
2702
|
+
constr
|
2703
|
+
]
|
2704
|
+
end
|
2705
|
+
|
2706
|
+
pairs.each do |param, type|
|
2707
|
+
if param.type
|
2708
|
+
check_relation(sub_type: type, super_type: param.type, constraints: constraints).else do |result|
|
2709
|
+
return [
|
2710
|
+
Errors::IncompatibleAssignment.new(
|
2711
|
+
node: param.node,
|
2712
|
+
lhs_type: param.type,
|
2713
|
+
rhs_type: type,
|
2714
|
+
result: result
|
2715
|
+
),
|
2716
|
+
constr
|
2717
|
+
]
|
2718
|
+
end
|
2719
|
+
end
|
2716
2720
|
end
|
2717
2721
|
end
|
2718
2722
|
|
@@ -3272,7 +3276,7 @@ module Steep
|
|
3272
3276
|
|
3273
3277
|
def to_instance_type(type, args: nil)
|
3274
3278
|
args = args || case type
|
3275
|
-
when AST::Types::Name::
|
3279
|
+
when AST::Types::Name::Singleton
|
3276
3280
|
checker.factory.env.class_decls[checker.factory.type_name_1(type.name)].type_params.each.map { AST::Builtin.any_type }
|
3277
3281
|
else
|
3278
3282
|
raise "unexpected type to to_instance_type: #{type}"
|