steep 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/bin/smoke_runner.rb +1 -1
- data/lib/steep.rb +6 -4
- data/lib/steep/ast/builtin.rb +96 -0
- data/lib/steep/ast/location.rb +9 -5
- data/lib/steep/ast/namespace.rb +80 -0
- data/lib/steep/ast/signature/env.rb +37 -31
- data/lib/steep/ast/types/boolean.rb +2 -2
- data/lib/steep/ast/types/hash.rb +50 -0
- data/lib/steep/ast/types/literal.rb +12 -10
- data/lib/steep/ast/types/name.rb +135 -94
- data/lib/steep/ast/types/nil.rb +3 -1
- data/lib/steep/ast/types/proc.rb +3 -1
- data/lib/steep/drivers/check.rb +4 -4
- data/lib/steep/drivers/utils/validator.rb +11 -16
- data/lib/steep/interface/builder.rb +201 -146
- data/lib/steep/interface/instantiated.rb +8 -0
- data/lib/steep/names.rb +86 -0
- data/lib/steep/parser.y +1093 -668
- data/lib/steep/source.rb +2 -2
- data/lib/steep/subtyping/check.rb +199 -63
- data/lib/steep/subtyping/constraints.rb +2 -5
- data/lib/steep/subtyping/variable_variance.rb +2 -2
- data/lib/steep/type_construction.rb +194 -175
- data/lib/steep/type_inference/block_params.rb +9 -21
- data/lib/steep/type_inference/constant_env.rb +26 -30
- data/lib/steep/type_inference/send_args.rb +4 -7
- data/lib/steep/type_inference/type_env.rb +3 -3
- data/lib/steep/version.rb +1 -1
- data/smoke/alias/a.rb +1 -1
- data/smoke/alias/b.rb +1 -1
- data/smoke/class/i.rbi +1 -1
- data/smoke/hash/a.rbi +8 -0
- data/smoke/hash/c.rb +18 -0
- data/smoke/hash/d.rb +6 -0
- data/smoke/hello/hello.rb +2 -2
- data/smoke/interface/a.rb +14 -0
- data/smoke/interface/a.rbi +12 -0
- data/smoke/module/a.rb +1 -1
- data/smoke/module/a.rbi +3 -3
- data/smoke/module/b.rb +1 -1
- data/smoke/stdout/a.rb +2 -2
- data/stdlib/builtin.rbi +6 -7
- data/steep.gemspec +1 -1
- metadata +14 -7
- data/lib/steep/module_name.rb +0 -116
- data/lib/steep/type_name.rb +0 -93
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::Ruby25.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::Ruby25.new
|
64
64
|
|
65
65
|
parser.tokenize(buffer)
|
66
66
|
end
|
@@ -40,7 +40,7 @@ module Steep
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def alias?(type)
|
43
|
-
type.is_a?(AST::Types::Name
|
43
|
+
type.is_a?(AST::Types::Name::Alias)
|
44
44
|
end
|
45
45
|
|
46
46
|
def cacheable?(relation)
|
@@ -75,7 +75,7 @@ module Steep
|
|
75
75
|
when relation.super_type.is_a?(AST::Types::Boolean)
|
76
76
|
success(constraints: constraints)
|
77
77
|
|
78
|
-
when
|
78
|
+
when alias?(relation.sub_type)
|
79
79
|
check0(
|
80
80
|
Relation.new(sub_type: expand_alias(relation.sub_type), super_type: relation.super_type),
|
81
81
|
assumption: assumption,
|
@@ -83,7 +83,7 @@ module Steep
|
|
83
83
|
constraints: constraints
|
84
84
|
)
|
85
85
|
|
86
|
-
when
|
86
|
+
when alias?(relation.super_type)
|
87
87
|
check0(
|
88
88
|
Relation.new(super_type: expand_alias(relation.super_type), sub_type: relation.sub_type),
|
89
89
|
assumption: assumption,
|
@@ -165,10 +165,9 @@ module Steep
|
|
165
165
|
trace: trace)
|
166
166
|
end
|
167
167
|
|
168
|
-
when relation.sub_type.is_a?(AST::Types::Name) && relation.super_type.is_a?(AST::Types::Name)
|
169
|
-
|
170
|
-
|
171
|
-
results = relation.sub_type.args.zip(relation.super_type.args).flat_map do |(sub, sup)|
|
168
|
+
when relation.sub_type.is_a?(AST::Types::Name::Base) && relation.super_type.is_a?(AST::Types::Name::Base)
|
169
|
+
if (pairs = extract_nominal_pairs(relation))
|
170
|
+
results = pairs.flat_map do |(sub, sup)|
|
172
171
|
Relation.new(sub_type: sub, super_type: sup).yield_self do |rel|
|
173
172
|
[rel, rel.flip]
|
174
173
|
end
|
@@ -185,8 +184,8 @@ module Steep
|
|
185
184
|
results.find(&:failure?)
|
186
185
|
end
|
187
186
|
else
|
188
|
-
sub_interface = resolve(relation.sub_type
|
189
|
-
super_interface = resolve(relation.super_type
|
187
|
+
sub_interface = resolve(relation.sub_type)
|
188
|
+
super_interface = resolve(relation.super_type)
|
190
189
|
|
191
190
|
check_interface(sub_interface, super_interface, assumption: assumption, trace: trace, constraints: constraints)
|
192
191
|
end
|
@@ -223,8 +222,8 @@ module Steep
|
|
223
222
|
end
|
224
223
|
|
225
224
|
when relation.sub_type.is_a?(AST::Types::Tuple)
|
226
|
-
sub_interface = resolve(relation.sub_type
|
227
|
-
super_interface = resolve(relation.super_type
|
225
|
+
sub_interface = resolve(relation.sub_type)
|
226
|
+
super_interface = resolve(relation.super_type)
|
228
227
|
|
229
228
|
check_interface(sub_interface,
|
230
229
|
super_interface,
|
@@ -237,6 +236,34 @@ module Steep
|
|
237
236
|
trace: trace)
|
238
237
|
end
|
239
238
|
end
|
239
|
+
|
240
|
+
def extract_nominal_pairs(relation)
|
241
|
+
sub_type = relation.sub_type
|
242
|
+
super_type = relation.super_type
|
243
|
+
|
244
|
+
case
|
245
|
+
when sub_type.is_a?(AST::Types::Name::Instance) && super_type.is_a?(AST::Types::Name::Instance)
|
246
|
+
if sub_type.name == super_type.name && sub_type.args.size == super_type.args.size
|
247
|
+
sub_type.args.zip(super_type.args)
|
248
|
+
end
|
249
|
+
when sub_type.is_a?(AST::Types::Name::Interface) && super_type.is_a?(AST::Types::Name::Interface)
|
250
|
+
if sub_type.name == super_type.name && sub_type.args.size == super_type.args.size
|
251
|
+
sub_type.args.zip(super_type.args)
|
252
|
+
end
|
253
|
+
when sub_type.is_a?(AST::Types::Name::Alias) && super_type.is_a?(AST::Types::Name::Alias)
|
254
|
+
if sub_type.name == super_type.name && sub_type.args.size == super_type.args.size
|
255
|
+
sub_type.args.zip(super_type.args)
|
256
|
+
end
|
257
|
+
when sub_type.is_a?(AST::Types::Name::Class) && super_type.is_a?(AST::Types::Name::Class)
|
258
|
+
if sub_type.name == super_type.name
|
259
|
+
[]
|
260
|
+
end
|
261
|
+
when sub_type.is_a?(AST::Types::Name::Module) && super_type.is_a?(AST::Types::Name::Module)
|
262
|
+
if sub_type.name == super_type.name
|
263
|
+
[]
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
240
267
|
|
241
268
|
def same_type?(relation, assumption:)
|
242
269
|
if assumption.include?(relation) && assumption.include?(relation.flip)
|
@@ -246,11 +273,13 @@ module Steep
|
|
246
273
|
case
|
247
274
|
when relation.sub_type == relation.super_type
|
248
275
|
true
|
249
|
-
when relation.sub_type.is_a?(AST::Types::Name) && relation.super_type.is_a?(AST::Types::Name)
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
276
|
+
when relation.sub_type.is_a?(AST::Types::Name::Base) && relation.super_type.is_a?(AST::Types::Name::Base)
|
277
|
+
if (pairs = extract_nominal_pairs(relation))
|
278
|
+
pairs.all? do |(s, t)|
|
279
|
+
same_type?(Relation.new(sub_type: s, super_type: t), assumption: assumption)
|
280
|
+
end
|
281
|
+
else
|
282
|
+
false
|
254
283
|
end
|
255
284
|
else
|
256
285
|
false
|
@@ -553,11 +582,16 @@ module Steep
|
|
553
582
|
end
|
554
583
|
|
555
584
|
def module_type(type)
|
556
|
-
case
|
557
|
-
when
|
558
|
-
|
559
|
-
|
560
|
-
|
585
|
+
case type.name
|
586
|
+
when TypeName::Instance
|
587
|
+
case
|
588
|
+
when builder.signatures.class_name?(type.name.name)
|
589
|
+
type.class_type(constructor: nil)
|
590
|
+
when builder.signatures.module_name?(type.name.name)
|
591
|
+
type.module_type
|
592
|
+
end
|
593
|
+
else
|
594
|
+
nil
|
561
595
|
end
|
562
596
|
end
|
563
597
|
|
@@ -601,48 +635,107 @@ module Steep
|
|
601
635
|
end
|
602
636
|
end
|
603
637
|
|
604
|
-
def
|
638
|
+
def resolve_instance(type, self_type:, instance_type:, module_type:, with_initialize: false)
|
639
|
+
abstract_interface = builder.build_instance(type.name, with_initialize: with_initialize)
|
640
|
+
|
641
|
+
module_type = module_type || case builder.signatures.find_class_or_module(type.name)
|
642
|
+
when AST::Signature::Class
|
643
|
+
AST::Types::Name::Class.new(name: type.name, constructor: nil)
|
644
|
+
when AST::Signature::Module
|
645
|
+
AST::Types::Name::Module.new(name: type.name)
|
646
|
+
end
|
647
|
+
|
648
|
+
abstract_interface.instantiate(
|
649
|
+
type: self_type,
|
650
|
+
args: type.args,
|
651
|
+
instance_type: instance_type || type,
|
652
|
+
module_type: module_type
|
653
|
+
)
|
654
|
+
end
|
655
|
+
|
656
|
+
def resolve(type, self_type: type, instance_type: nil, module_type: nil)
|
605
657
|
Steep.logger.debug("Check#resolve: type=#{type}")
|
606
658
|
case type
|
607
659
|
when AST::Types::Any, AST::Types::Var, AST::Types::Class, AST::Types::Instance
|
608
660
|
raise CannotResolveError.new(type: type)
|
661
|
+
|
609
662
|
when AST::Types::Nil, AST::Types::Literal, AST::Types::Boolean
|
610
663
|
resolve(type.back_type,
|
611
664
|
self_type: self_type,
|
612
665
|
instance_type: instance_type,
|
613
|
-
module_type: module_type
|
614
|
-
|
615
|
-
when AST::Types::Name
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
builder.
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
666
|
+
module_type: module_type)
|
667
|
+
|
668
|
+
when AST::Types::Name::Instance
|
669
|
+
resolve_instance(type, self_type: self_type, instance_type: instance_type, module_type: module_type)
|
670
|
+
|
671
|
+
when AST::Types::Name::Class
|
672
|
+
yield_self do
|
673
|
+
abstract_interface = builder.build_class(type.name, constructor: type.constructor)
|
674
|
+
|
675
|
+
unless instance_type
|
676
|
+
type_params = builder.signatures.find_class(type.name).params&.variables || []
|
677
|
+
instance_type = AST::Types::Name::Instance.new(name: type.name,
|
678
|
+
args: type_params.map {|var| AST::Types::Var.new(name: var) })
|
679
|
+
end
|
680
|
+
|
681
|
+
interface = abstract_interface.instantiate(
|
682
|
+
type: self_type,
|
683
|
+
args: [],
|
684
|
+
instance_type: instance_type,
|
685
|
+
module_type: AST::Builtin::Class.class_type
|
686
|
+
)
|
687
|
+
|
688
|
+
if type_params
|
689
|
+
interface.subst(Interface::Substitution.build(type_params, type_params.map { AST::Builtin.any_type }))
|
690
|
+
else
|
691
|
+
interface
|
692
|
+
end
|
693
|
+
end
|
694
|
+
|
695
|
+
when AST::Types::Name::Module
|
696
|
+
yield_self do
|
697
|
+
abstract_interface = builder.build_module(type.name)
|
698
|
+
|
699
|
+
unless instance_type
|
700
|
+
type_params = builder.signatures.find_module(type.name).params&.variables || []
|
701
|
+
instance_type = AST::Types::Name::Instance.new(name: type.name,
|
702
|
+
args: type_params.map {|var| AST::Types::Var.new(name: var) })
|
703
|
+
end
|
704
|
+
|
705
|
+
interface = abstract_interface.instantiate(
|
706
|
+
type: self_type,
|
707
|
+
args: [],
|
708
|
+
instance_type: instance_type,
|
709
|
+
module_type: AST::Builtin::Module.class_type
|
710
|
+
)
|
711
|
+
|
712
|
+
if type_params
|
713
|
+
interface.subst(Interface::Substitution.build(type_params, type_params.map { AST::Builtin.any_type }))
|
714
|
+
else
|
715
|
+
interface
|
639
716
|
end
|
640
717
|
end
|
718
|
+
|
719
|
+
when AST::Types::Name::Interface
|
720
|
+
yield_self do
|
721
|
+
abstract_interface = builder.build_interface(type.name)
|
722
|
+
|
723
|
+
abstract_interface.instantiate(
|
724
|
+
type: self_type,
|
725
|
+
args: type.args,
|
726
|
+
instance_type: nil,
|
727
|
+
module_type: nil
|
728
|
+
)
|
729
|
+
end
|
730
|
+
|
731
|
+
when AST::Types::Name::Alias
|
732
|
+
resolve(expand_alias(type), self_type: self_type, instance_type: instance_type, module_type: module_type)
|
733
|
+
|
641
734
|
when AST::Types::Union
|
642
735
|
interfaces = type.types.map do |member_type|
|
643
736
|
fresh = AST::Types::Var.fresh(:___)
|
644
737
|
|
645
|
-
resolve(member_type, self_type: type, instance_type: fresh, module_type: fresh
|
738
|
+
resolve(member_type, self_type: type, instance_type: fresh, module_type: fresh).select_method_type do |method_type|
|
646
739
|
!method_type.each_type.include?(fresh)
|
647
740
|
end
|
648
741
|
end
|
@@ -711,7 +804,7 @@ module Steep
|
|
711
804
|
ivar_chains: {})
|
712
805
|
|
713
806
|
when AST::Types::Intersection
|
714
|
-
interfaces = type.types.map do |type| resolve(type
|
807
|
+
interfaces = type.types.map do |type| resolve(type) end
|
715
808
|
|
716
809
|
methods = interfaces.inject(nil) do |methods, i|
|
717
810
|
if methods
|
@@ -763,9 +856,8 @@ module Steep
|
|
763
856
|
when AST::Types::Tuple
|
764
857
|
yield_self do
|
765
858
|
element_type = AST::Types::Union.build(types: type.types)
|
766
|
-
array_type = AST::
|
767
|
-
|
768
|
-
array_interface = resolve(array_type, self_type: self_type, with_initialize: with_initialize)
|
859
|
+
array_type = AST::Builtin::Array.instance_type(element_type)
|
860
|
+
array_interface = resolve(array_type, self_type: self_type)
|
769
861
|
|
770
862
|
array_interface.methods[:[]] = array_interface.methods[:[]].yield_self do |aref|
|
771
863
|
types = type.types.map.with_index {|elem_type, index|
|
@@ -806,9 +898,56 @@ module Steep
|
|
806
898
|
array_interface
|
807
899
|
end
|
808
900
|
|
901
|
+
when AST::Types::Hash
|
902
|
+
yield_self do
|
903
|
+
key_type = AST::Types::Union.build(types: type.elements.keys.map {|val| AST::Types::Literal.new(value: val) })
|
904
|
+
value_type = AST::Types::Union.build(types: type.elements.values)
|
905
|
+
hash_interface = resolve(AST::Builtin::Hash.instance_type(key_type, value_type), self_type: self_type)
|
906
|
+
|
907
|
+
hash_interface.methods[:[]] = hash_interface.methods[:[]].yield_self do |ref|
|
908
|
+
types = type.elements.map do |key, value_type|
|
909
|
+
Interface::MethodType.new(
|
910
|
+
type_params: [],
|
911
|
+
params: Interface::Params.new(required: [AST::Types::Literal.new(value: key)],
|
912
|
+
optional: [],
|
913
|
+
rest: nil,
|
914
|
+
required_keywords: {},
|
915
|
+
optional_keywords: {},
|
916
|
+
rest_keywords: nil),
|
917
|
+
return_type: value_type,
|
918
|
+
block: nil,
|
919
|
+
location: nil
|
920
|
+
)
|
921
|
+
end
|
922
|
+
|
923
|
+
ref.with_types(types + ref.types)
|
924
|
+
end
|
925
|
+
|
926
|
+
hash_interface.methods[:[]=] = hash_interface.methods[:[]=].yield_self do |method|
|
927
|
+
types = type.elements.map do |key, value_type|
|
928
|
+
Interface::MethodType.new(
|
929
|
+
type_params: [],
|
930
|
+
params: Interface::Params.new(required: [AST::Types::Literal.new(value: key), value_type],
|
931
|
+
optional: [],
|
932
|
+
rest: nil,
|
933
|
+
required_keywords: {},
|
934
|
+
optional_keywords: {},
|
935
|
+
rest_keywords: nil),
|
936
|
+
return_type: value_type,
|
937
|
+
block: nil,
|
938
|
+
location: nil
|
939
|
+
)
|
940
|
+
end
|
941
|
+
|
942
|
+
method.with_types(types + method.types)
|
943
|
+
end
|
944
|
+
|
945
|
+
hash_interface
|
946
|
+
end
|
947
|
+
|
809
948
|
when AST::Types::Proc
|
810
949
|
yield_self do
|
811
|
-
proc_interface = resolve(type.back_type, self_type: self_type
|
950
|
+
proc_interface = resolve(type.back_type, self_type: self_type)
|
812
951
|
apply_type = Interface::MethodType.new(
|
813
952
|
type_params: [],
|
814
953
|
params: type.params,
|
@@ -842,15 +981,12 @@ module Steep
|
|
842
981
|
types: type.types.map {|ty| expand_alias(ty) },
|
843
982
|
location: type.location
|
844
983
|
)
|
845
|
-
when AST::Types::Name
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
else
|
852
|
-
type
|
853
|
-
end
|
984
|
+
when AST::Types::Name::Alias
|
985
|
+
alias_sig = builder.signatures.find_alias(type.name, namespace: AST::Namespace.root)
|
986
|
+
expanded_alias = builder.absolute_type(alias_sig.type, current: alias_sig.name.namespace)
|
987
|
+
args = type.args.map {|ty| expand_alias(ty) }
|
988
|
+
s = Interface::Substitution.build(alias_sig.params&.variables || [], args)
|
989
|
+
expand_alias(expanded_alias.subst(s))
|
854
990
|
else
|
855
991
|
type
|
856
992
|
end
|
@@ -133,14 +133,11 @@ module Steep
|
|
133
133
|
|
134
134
|
def eliminate_variable(type, to:)
|
135
135
|
case type
|
136
|
-
when AST::Types::Name
|
136
|
+
when AST::Types::Name::Instance, AST::Types::Name::Alias, AST::Types::Name::Interface
|
137
137
|
type.args.map do |ty|
|
138
138
|
eliminate_variable(ty, to: AST::Types::Any.new)
|
139
139
|
end.yield_self do |args|
|
140
|
-
|
141
|
-
name: type.name,
|
142
|
-
args: args
|
143
|
-
)
|
140
|
+
type.class.new(name: type.name, args: args, location: type.location)
|
144
141
|
end
|
145
142
|
when AST::Types::Union
|
146
143
|
type.types.map do |ty|
|
@@ -54,11 +54,11 @@ module Steep
|
|
54
54
|
covariants << type.name
|
55
55
|
contravariants << type.name
|
56
56
|
end
|
57
|
-
when AST::Types::Union, AST::Types::Intersection
|
57
|
+
when AST::Types::Union, AST::Types::Intersection, AST::Types::Tuple
|
58
58
|
type.types.each do |ty|
|
59
59
|
add_type(ty, variance: variance, covariants: covariants, contravariants: contravariants)
|
60
60
|
end
|
61
|
-
when AST::Types::Name
|
61
|
+
when AST::Types::Name::Interface, AST::Types::Name::Instance, AST::Types::Name::Alias
|
62
62
|
type.args.each do |arg|
|
63
63
|
add_type(arg, variance: :invariant, covariants: covariants, contravariants: contravariants)
|
64
64
|
end
|
@@ -1,58 +1,5 @@
|
|
1
1
|
module Steep
|
2
2
|
class TypeConstruction
|
3
|
-
module Types
|
4
|
-
module_function
|
5
|
-
|
6
|
-
def any
|
7
|
-
AST::Types::Any.new
|
8
|
-
end
|
9
|
-
|
10
|
-
def symbol_instance
|
11
|
-
AST::Types::Name.new_instance(name: "::Symbol")
|
12
|
-
end
|
13
|
-
|
14
|
-
def nil_instance
|
15
|
-
AST::Types::Nil.new
|
16
|
-
end
|
17
|
-
|
18
|
-
def string_instance
|
19
|
-
AST::Types::Name.new_instance(name: "::String")
|
20
|
-
end
|
21
|
-
|
22
|
-
def array_instance(type)
|
23
|
-
AST::Types::Name.new_instance(name: "::Array", args: [type])
|
24
|
-
end
|
25
|
-
|
26
|
-
def hash_instance(key, value)
|
27
|
-
AST::Types::Name.new_instance(name: "::Hash", args: [key, value])
|
28
|
-
end
|
29
|
-
|
30
|
-
def range_instance(type)
|
31
|
-
AST::Types::Name.new_instance(name: "::Range", args: [type])
|
32
|
-
end
|
33
|
-
|
34
|
-
def boolean?(type)
|
35
|
-
type.is_a?(AST::Types::Boolean)
|
36
|
-
end
|
37
|
-
|
38
|
-
def optional(type)
|
39
|
-
AST::Types::Union.build(types: [type, nil_instance])
|
40
|
-
end
|
41
|
-
|
42
|
-
def hash_instance?(type)
|
43
|
-
case type
|
44
|
-
when AST::Types::Name
|
45
|
-
type.name.is_a?(TypeName::Instance) && type.name.name == ModuleName.new(name: "Hash", absolute: true)
|
46
|
-
else
|
47
|
-
false
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def array_instance?(type)
|
52
|
-
type.is_a?(AST::Types::Name) && type.name.is_a?(TypeName::Instance) && type.name.name == ModuleName.new(name: "Array", absolute: true)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
3
|
class MethodContext
|
57
4
|
attr_reader :name
|
58
5
|
attr_reader :method
|
@@ -113,6 +60,10 @@ module Steep
|
|
113
60
|
@current_namespace = current_namespace
|
114
61
|
@const_env = const_env
|
115
62
|
end
|
63
|
+
|
64
|
+
def const_context
|
65
|
+
const_env.context
|
66
|
+
end
|
116
67
|
end
|
117
68
|
|
118
69
|
attr_reader :checker
|
@@ -165,10 +116,22 @@ module Steep
|
|
165
116
|
|
166
117
|
self_type = expand_alias(annots.self_type || self_type)
|
167
118
|
|
168
|
-
self_interface = self_type && (self_type !=
|
119
|
+
self_interface = self_type && (self_type != AST::Builtin.any_type || nil) && case self_type
|
120
|
+
when AST::Types::Name::Instance
|
121
|
+
yield_self do
|
122
|
+
class_type = AST::Types::Name::Class.new(name: self_type.name, constructor: false)
|
123
|
+
checker.resolve_instance(self_type,
|
124
|
+
self_type: self_type,
|
125
|
+
instance_type: self_type,
|
126
|
+
module_type: class_type,
|
127
|
+
with_initialize: true)
|
128
|
+
end
|
129
|
+
else
|
130
|
+
checker.resolve(self_type)
|
131
|
+
end
|
169
132
|
interface_method = self_interface&.yield_self do |interface|
|
170
133
|
interface.methods[method_name]&.yield_self do |method|
|
171
|
-
if self_type.is_a?(AST::Types::Name) && method.type_name == self_type.name
|
134
|
+
if self_type.is_a?(AST::Types::Name::Base) && method.type_name == self_type.name
|
172
135
|
method
|
173
136
|
else
|
174
137
|
Interface::Method.new(type_name: self_type,
|
@@ -243,7 +206,7 @@ module Steep
|
|
243
206
|
if (block_arg = args.find {|arg| arg.type == :blockarg })
|
244
207
|
if method_type.block
|
245
208
|
block_type = if method_type.block.optional?
|
246
|
-
AST::Types::Union.build(types: [method_type.block.type,
|
209
|
+
AST::Types::Union.build(types: [method_type.block.type, AST::Builtin.nil_type])
|
247
210
|
else
|
248
211
|
method_type.block.type
|
249
212
|
end
|
@@ -313,11 +276,11 @@ module Steep
|
|
313
276
|
end
|
314
277
|
|
315
278
|
def for_module(node)
|
316
|
-
new_module_name =
|
317
|
-
new_namespace =
|
279
|
+
new_module_name = Names::Module.from_node(node.children.first) or raise "Unexpected module name: #{node.children.first}"
|
280
|
+
new_namespace = nested_namespace_for_module(new_module_name)
|
318
281
|
|
319
282
|
annots = source.annotations(block: node, builder: checker.builder, current_module: new_namespace)
|
320
|
-
module_type = AST::
|
283
|
+
module_type = AST::Builtin::Module.instance_type
|
321
284
|
|
322
285
|
implement_module_name = yield_self do
|
323
286
|
if (annotation = annots.implement_module_annotation)
|
@@ -345,19 +308,19 @@ module Steep
|
|
345
308
|
module_name = implement_module_name.name
|
346
309
|
module_args = implement_module_name.args.map {|x| AST::Types::Var.new(name: x) }
|
347
310
|
|
348
|
-
abstract = checker.builder.
|
311
|
+
abstract = checker.builder.build_module(module_name)
|
349
312
|
|
350
|
-
instance_type = AST::Types::Name.
|
313
|
+
instance_type = AST::Types::Name::Instance.new(name: module_name, args: module_args)
|
351
314
|
|
352
315
|
unless abstract.supers.empty?
|
353
316
|
instance_type = AST::Types::Intersection.build(
|
354
|
-
types: [instance_type, AST::
|
317
|
+
types: [instance_type, AST::Builtin::Object.instance_type] + abstract.supers.map {|x| absolute_type(x) }
|
355
318
|
)
|
356
319
|
end
|
357
320
|
|
358
321
|
module_type = AST::Types::Intersection.build(types: [
|
359
|
-
AST::
|
360
|
-
|
322
|
+
AST::Builtin::Module.instance_type,
|
323
|
+
AST::Types::Name::Module.new(name: module_name)
|
361
324
|
])
|
362
325
|
end
|
363
326
|
|
@@ -369,7 +332,12 @@ module Steep
|
|
369
332
|
module_type = annots.module_type
|
370
333
|
end
|
371
334
|
|
372
|
-
|
335
|
+
const_context = if new_namespace.empty?
|
336
|
+
nil
|
337
|
+
else
|
338
|
+
Names::Module.new(name: new_namespace.path.last, namespace: new_namespace.parent)
|
339
|
+
end
|
340
|
+
module_const_env = TypeInference::ConstantEnv.new(builder: checker.builder, context: const_context)
|
373
341
|
|
374
342
|
module_context_ = ModuleContext.new(
|
375
343
|
instance_type: instance_type,
|
@@ -399,8 +367,8 @@ module Steep
|
|
399
367
|
end
|
400
368
|
|
401
369
|
def for_class(node)
|
402
|
-
new_class_name =
|
403
|
-
new_namespace =
|
370
|
+
new_class_name = Names::Module.from_node(node.children.first) or raise "Unexpected class name: #{node.children.first}"
|
371
|
+
new_namespace = nested_namespace_for_module(new_class_name)
|
404
372
|
|
405
373
|
annots = source.annotations(block: node, builder: checker.builder, current_module: new_namespace)
|
406
374
|
|
@@ -430,13 +398,18 @@ module Steep
|
|
430
398
|
class_name = implement_module_name.name
|
431
399
|
class_args = implement_module_name.args.map {|x| AST::Types::Var.new(name: x) }
|
432
400
|
|
433
|
-
_ = checker.builder.
|
401
|
+
_ = checker.builder.build_instance(class_name, with_initialize: true)
|
434
402
|
|
435
|
-
instance_type = AST::Types::Name.
|
436
|
-
module_type = AST::Types::Name.
|
403
|
+
instance_type = AST::Types::Name::Instance.new(name: class_name, args: class_args)
|
404
|
+
module_type = AST::Types::Name::Class.new(name: class_name, constructor: true)
|
437
405
|
end
|
438
406
|
|
439
|
-
|
407
|
+
const_context = if new_namespace.empty?
|
408
|
+
nil
|
409
|
+
else
|
410
|
+
Names::Module.new(name: new_namespace.path.last, namespace: new_namespace.parent)
|
411
|
+
end
|
412
|
+
class_const_env = TypeInference::ConstantEnv.new(builder: checker.builder, context: const_context)
|
440
413
|
|
441
414
|
module_context = ModuleContext.new(
|
442
415
|
instance_type: annots.instance_type || instance_type,
|
@@ -541,7 +514,7 @@ module Steep
|
|
541
514
|
if last_node
|
542
515
|
type = synthesize(last_node, hint: hint)
|
543
516
|
else
|
544
|
-
type =
|
517
|
+
type = AST::Builtin.nil_type
|
545
518
|
end
|
546
519
|
|
547
520
|
typing.add_typing(node, type)
|
@@ -553,8 +526,8 @@ module Steep
|
|
553
526
|
rhs = node.children[1]
|
554
527
|
|
555
528
|
if var.name == :_
|
556
|
-
synthesize(rhs, hint:
|
557
|
-
typing.add_typing(node,
|
529
|
+
synthesize(rhs, hint: AST::Builtin.any_type)
|
530
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
558
531
|
else
|
559
532
|
type_assignment(var, rhs, node, hint: hint)
|
560
533
|
end
|
@@ -589,9 +562,8 @@ module Steep
|
|
589
562
|
yield_self do
|
590
563
|
if self_class?(node)
|
591
564
|
module_type = expand_alias(module_context.module_type)
|
592
|
-
type = if module_type.is_a?(AST::Types::Name
|
593
|
-
AST::Types::Name.new(name: module_type.name
|
594
|
-
args: module_type.args)
|
565
|
+
type = if module_type.is_a?(AST::Types::Name::Class)
|
566
|
+
AST::Types::Name::Class.new(name: module_type.name, constructor: method_context.constructor)
|
595
567
|
else
|
596
568
|
module_type
|
597
569
|
end
|
@@ -605,9 +577,8 @@ module Steep
|
|
605
577
|
yield_self do
|
606
578
|
type = if self_class?(node)
|
607
579
|
module_type = expand_alias(module_context.module_type)
|
608
|
-
type = if module_type.is_a?(AST::Types::Name)
|
609
|
-
AST::Types::Name.new(name: module_type.name
|
610
|
-
args: module_type.args)
|
580
|
+
type = if module_type.is_a?(AST::Types::Name::Class)
|
581
|
+
AST::Types::Name::Class.new(name: module_type.name, constructor: method_context.constructor)
|
611
582
|
else
|
612
583
|
module_type
|
613
584
|
end
|
@@ -616,14 +587,14 @@ module Steep
|
|
616
587
|
type_send(node, send_node: node, block_params: nil, block_body: nil, unwrap: true)
|
617
588
|
end
|
618
589
|
|
619
|
-
union_type(type,
|
590
|
+
union_type(type, AST::Builtin.nil_type)
|
620
591
|
end
|
621
592
|
|
622
593
|
when :match_with_lvasgn
|
623
594
|
each_child_node(node) do |child|
|
624
595
|
synthesize(child)
|
625
596
|
end
|
626
|
-
typing.add_typing(node,
|
597
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
627
598
|
|
628
599
|
when :op_asgn
|
629
600
|
yield_self do
|
@@ -646,12 +617,12 @@ module Steep
|
|
646
617
|
end
|
647
618
|
|
648
619
|
case
|
649
|
-
when lhs_type ==
|
620
|
+
when lhs_type == AST::Builtin.any_type
|
650
621
|
typing.add_typing(node, lhs_type)
|
651
622
|
when !lhs_type
|
652
623
|
fallback_to_any(node)
|
653
624
|
else
|
654
|
-
lhs_interface = checker.resolve(lhs_type
|
625
|
+
lhs_interface = checker.resolve(lhs_type)
|
655
626
|
op_method = lhs_interface.methods[op]
|
656
627
|
|
657
628
|
if op_method
|
@@ -718,7 +689,7 @@ module Steep
|
|
718
689
|
end
|
719
690
|
end
|
720
691
|
else
|
721
|
-
typing.add_typing node,
|
692
|
+
typing.add_typing node, AST::Builtin.any_type
|
722
693
|
end
|
723
694
|
end
|
724
695
|
|
@@ -758,13 +729,13 @@ module Steep
|
|
758
729
|
return_type = expand_alias(new.method_context&.return_type)
|
759
730
|
if return_type && !return_type.is_a?(AST::Types::Void)
|
760
731
|
result = checker.check(
|
761
|
-
Subtyping::Relation.new(sub_type:
|
732
|
+
Subtyping::Relation.new(sub_type: AST::Builtin.nil_type, super_type: return_type),
|
762
733
|
constraints: Subtyping::Constraints.empty
|
763
734
|
)
|
764
735
|
if result.failure?
|
765
736
|
typing.add_error(Errors::MethodBodyTypeMismatch.new(node: node,
|
766
737
|
expected: new.method_context&.return_type,
|
767
|
-
actual:
|
738
|
+
actual: AST::Builtin.nil_type,
|
768
739
|
result: result))
|
769
740
|
end
|
770
741
|
end
|
@@ -774,7 +745,7 @@ module Steep
|
|
774
745
|
module_context.defined_instance_methods << node.children[0]
|
775
746
|
end
|
776
747
|
|
777
|
-
typing.add_typing(node,
|
748
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
778
749
|
|
779
750
|
when :defs
|
780
751
|
synthesize(node.children[0]).tap do |self_type|
|
@@ -808,7 +779,7 @@ module Steep
|
|
808
779
|
end
|
809
780
|
end
|
810
781
|
|
811
|
-
typing.add_typing(node,
|
782
|
+
typing.add_typing(node, AST::Builtin::Symbol.instance_type)
|
812
783
|
|
813
784
|
when :return
|
814
785
|
yield_self do
|
@@ -820,7 +791,7 @@ module Steep
|
|
820
791
|
value_type = if return_types.size == 1
|
821
792
|
return_types.first
|
822
793
|
else
|
823
|
-
|
794
|
+
AST::Builtin::Array.instance_type(union_type(*return_types))
|
824
795
|
end
|
825
796
|
|
826
797
|
if (ret_type = expand_alias(method_context&.return_type))
|
@@ -841,7 +812,7 @@ module Steep
|
|
841
812
|
end
|
842
813
|
end
|
843
814
|
|
844
|
-
typing.add_typing(node,
|
815
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
845
816
|
end
|
846
817
|
|
847
818
|
when :break
|
@@ -867,7 +838,7 @@ module Steep
|
|
867
838
|
typing.add_error Errors::UnexpectedJump.new(node: node)
|
868
839
|
end
|
869
840
|
|
870
|
-
typing.add_typing(node,
|
841
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
871
842
|
|
872
843
|
when :next
|
873
844
|
value = node.children[0]
|
@@ -892,13 +863,13 @@ module Steep
|
|
892
863
|
typing.add_error Errors::UnexpectedJump.new(node: node)
|
893
864
|
end
|
894
865
|
|
895
|
-
typing.add_typing(node,
|
866
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
896
867
|
|
897
868
|
when :retry
|
898
869
|
unless break_context
|
899
870
|
typing.add_error Errors::UnexpectedJump.new(node: node)
|
900
871
|
end
|
901
|
-
typing.add_typing(node,
|
872
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
902
873
|
|
903
874
|
when :arg, :kwarg, :procarg0
|
904
875
|
yield_self do
|
@@ -921,7 +892,7 @@ module Steep
|
|
921
892
|
var = node.children[0]
|
922
893
|
type = type_env.get(lvar: var.name) do
|
923
894
|
typing.add_error Errors::FallbackAny.new(node: node)
|
924
|
-
|
895
|
+
AST::Builtin::Array.instance_type(AST::Builtin.any_type)
|
925
896
|
end
|
926
897
|
|
927
898
|
typing.add_typing(node, type)
|
@@ -932,17 +903,17 @@ module Steep
|
|
932
903
|
var = node.children[0]
|
933
904
|
type = type_env.get(lvar: var.name) do
|
934
905
|
typing.add_error Errors::FallbackAny.new(node: node)
|
935
|
-
|
906
|
+
AST::Builtin::Hash.instance_type(AST::Builtin::Symbol.instance_type, AST::Builtin.any_type)
|
936
907
|
end
|
937
908
|
|
938
909
|
typing.add_typing(node, type)
|
939
910
|
end
|
940
911
|
|
941
912
|
when :float
|
942
|
-
typing.add_typing(node, AST::
|
913
|
+
typing.add_typing(node, AST::Builtin::Float.instance_type)
|
943
914
|
|
944
915
|
when :nil
|
945
|
-
typing.add_typing(node,
|
916
|
+
typing.add_typing(node, AST::Builtin.nil_type)
|
946
917
|
|
947
918
|
when :int
|
948
919
|
yield_self do
|
@@ -951,7 +922,7 @@ module Steep
|
|
951
922
|
if literal_type
|
952
923
|
typing.add_typing(node, literal_type)
|
953
924
|
else
|
954
|
-
typing.add_typing(node, AST::
|
925
|
+
typing.add_typing(node, AST::Builtin::Integer.instance_type)
|
955
926
|
end
|
956
927
|
end
|
957
928
|
|
@@ -962,7 +933,7 @@ module Steep
|
|
962
933
|
if literal_type
|
963
934
|
typing.add_typing(node, literal_type)
|
964
935
|
else
|
965
|
-
typing.add_typing(node,
|
936
|
+
typing.add_typing(node, AST::Builtin::Symbol.instance_type)
|
966
937
|
end
|
967
938
|
end
|
968
939
|
|
@@ -973,7 +944,7 @@ module Steep
|
|
973
944
|
if literal_type
|
974
945
|
typing.add_typing(node, literal_type)
|
975
946
|
else
|
976
|
-
typing.add_typing(node,
|
947
|
+
typing.add_typing(node, AST::Builtin::String.instance_type)
|
977
948
|
end
|
978
949
|
end
|
979
950
|
|
@@ -982,7 +953,9 @@ module Steep
|
|
982
953
|
|
983
954
|
when :hash
|
984
955
|
yield_self do
|
985
|
-
|
956
|
+
ty = try_hash_type(node, hint) and return ty
|
957
|
+
|
958
|
+
if AST::Builtin::Hash.instance_type?(hint)
|
986
959
|
key_hint = hint.args[0]
|
987
960
|
value_hint = hint.args[1]
|
988
961
|
end
|
@@ -1002,13 +975,13 @@ module Steep
|
|
1002
975
|
end
|
1003
976
|
when :kwsplat
|
1004
977
|
expand_alias(synthesize(child.children[0])) do |splat_type, original_type|
|
1005
|
-
if
|
978
|
+
if AST::Builtin::Hash.instance_type?(splat_type)
|
1006
979
|
key_types << splat_type.args[0]
|
1007
980
|
value_types << splat_type.args[1]
|
1008
981
|
else
|
1009
982
|
typing.add_error Errors::UnexpectedSplat.new(node: child, type: original_type)
|
1010
|
-
key_types <<
|
1011
|
-
value_types <<
|
983
|
+
key_types << AST::Builtin.any_type
|
984
|
+
value_types << AST::Builtin.any_type
|
1012
985
|
end
|
1013
986
|
end
|
1014
987
|
else
|
@@ -1016,14 +989,14 @@ module Steep
|
|
1016
989
|
end
|
1017
990
|
end
|
1018
991
|
|
1019
|
-
key_type = key_types.empty? ?
|
1020
|
-
value_type = value_types.empty? ?
|
992
|
+
key_type = key_types.empty? ? AST::Builtin.any_type : AST::Types::Union.build(types: key_types)
|
993
|
+
value_type = value_types.empty? ? AST::Builtin.any_type : AST::Types::Union.build(types: value_types)
|
1021
994
|
|
1022
995
|
if key_types.empty? && value_types.empty? && !hint
|
1023
996
|
typing.add_error Errors::FallbackAny.new(node: node)
|
1024
997
|
end
|
1025
998
|
|
1026
|
-
typing.add_typing(node,
|
999
|
+
typing.add_typing(node, AST::Builtin::Hash.instance_type(key_type, value_type))
|
1027
1000
|
end
|
1028
1001
|
|
1029
1002
|
when :dstr, :xstr
|
@@ -1031,14 +1004,14 @@ module Steep
|
|
1031
1004
|
synthesize(child)
|
1032
1005
|
end
|
1033
1006
|
|
1034
|
-
typing.add_typing(node,
|
1007
|
+
typing.add_typing(node, AST::Builtin::String.instance_type)
|
1035
1008
|
|
1036
1009
|
when :dsym
|
1037
1010
|
each_child_node(node) do |child|
|
1038
1011
|
synthesize(child)
|
1039
1012
|
end
|
1040
1013
|
|
1041
|
-
typing.add_typing(node,
|
1014
|
+
typing.add_typing(node, AST::Builtin::Symbol.instance_type)
|
1042
1015
|
|
1043
1016
|
when :class
|
1044
1017
|
yield_self do
|
@@ -1050,7 +1023,7 @@ module Steep
|
|
1050
1023
|
end
|
1051
1024
|
end
|
1052
1025
|
|
1053
|
-
typing.add_typing(node,
|
1026
|
+
typing.add_typing(node, AST::Builtin.nil_type)
|
1054
1027
|
end
|
1055
1028
|
|
1056
1029
|
when :module
|
@@ -1063,7 +1036,7 @@ module Steep
|
|
1063
1036
|
end
|
1064
1037
|
end
|
1065
1038
|
|
1066
|
-
typing.add_typing(node,
|
1039
|
+
typing.add_typing(node, AST::Builtin.nil_type)
|
1067
1040
|
end
|
1068
1041
|
|
1069
1042
|
when :self
|
@@ -1074,7 +1047,7 @@ module Steep
|
|
1074
1047
|
end
|
1075
1048
|
|
1076
1049
|
when :const
|
1077
|
-
const_name =
|
1050
|
+
const_name = Names::Module.from_node(node)
|
1078
1051
|
if const_name
|
1079
1052
|
type = type_env.get(const: const_name) do
|
1080
1053
|
fallback_to_any node
|
@@ -1086,7 +1059,7 @@ module Steep
|
|
1086
1059
|
|
1087
1060
|
when :casgn
|
1088
1061
|
yield_self do
|
1089
|
-
const_name =
|
1062
|
+
const_name = Names::Module.from_node(node)
|
1090
1063
|
if const_name
|
1091
1064
|
value_type = synthesize(node.children.last)
|
1092
1065
|
type = type_env.assign(const: const_name, type: value_type) do |error|
|
@@ -1139,7 +1112,7 @@ module Steep
|
|
1139
1112
|
if method_context.super_method
|
1140
1113
|
if method_context.method.incompatible?
|
1141
1114
|
typing.add_error Errors::IncompatibleZuper.new(node: node, method: method_context.name)
|
1142
|
-
typing.add_typing node,
|
1115
|
+
typing.add_typing node, AST::Builtin.any_type
|
1143
1116
|
else
|
1144
1117
|
types = method_context.super_method.types.map(&:return_type)
|
1145
1118
|
typing.add_typing(node, union_type(*types))
|
@@ -1159,7 +1132,7 @@ module Steep
|
|
1159
1132
|
unless hint
|
1160
1133
|
typing.add_error Errors::FallbackAny.new(node: node)
|
1161
1134
|
end
|
1162
|
-
typing.add_typing(node,
|
1135
|
+
typing.add_typing(node, AST::Builtin::Array.instance_type(AST::Builtin.any_type))
|
1163
1136
|
else
|
1164
1137
|
is_tuple = nil
|
1165
1138
|
|
@@ -1180,7 +1153,7 @@ module Steep
|
|
1180
1153
|
array_type = hint
|
1181
1154
|
else
|
1182
1155
|
element_hint = expand_alias(hint) do |hint|
|
1183
|
-
|
1156
|
+
AST::Builtin::Array.instance_type?(hint) && hint.args[0]
|
1184
1157
|
end
|
1185
1158
|
|
1186
1159
|
element_types = node.children.flat_map do |e|
|
@@ -1197,9 +1170,9 @@ module Steep
|
|
1197
1170
|
end
|
1198
1171
|
end.map do |type|
|
1199
1172
|
case
|
1200
|
-
when
|
1173
|
+
when AST::Builtin::Array.instance_type?(type)
|
1201
1174
|
type.args.first
|
1202
|
-
when
|
1175
|
+
when AST::Builtin::Range.instance_type?(type)
|
1203
1176
|
type.args.first
|
1204
1177
|
else
|
1205
1178
|
type
|
@@ -1209,7 +1182,7 @@ module Steep
|
|
1209
1182
|
[select_super_type(synthesize(e), element_hint)]
|
1210
1183
|
end
|
1211
1184
|
end
|
1212
|
-
array_type =
|
1185
|
+
array_type = AST::Builtin::Array.instance_type(AST::Types::Union.build(types: element_types))
|
1213
1186
|
end
|
1214
1187
|
|
1215
1188
|
typing.add_typing(node, array_type)
|
@@ -1230,10 +1203,10 @@ module Steep
|
|
1230
1203
|
type_env.join!([right_env, TypeInference::TypeEnv.new(subtyping: checker,
|
1231
1204
|
const_env: nil)])
|
1232
1205
|
|
1233
|
-
if
|
1206
|
+
if left_type.is_a?(AST::Types::Boolean)
|
1234
1207
|
typing.add_typing(node, union_type(left_type, right_type))
|
1235
1208
|
else
|
1236
|
-
typing.add_typing(node, union_type(right_type,
|
1209
|
+
typing.add_typing(node, union_type(right_type, AST::Builtin.nil_type))
|
1237
1210
|
end
|
1238
1211
|
end
|
1239
1212
|
|
@@ -1285,18 +1258,18 @@ module Steep
|
|
1285
1258
|
end
|
1286
1259
|
|
1287
1260
|
if (body = clause.children.last)
|
1288
|
-
if var_names && var_types && test_types.all? {|type| type.is_a?(AST::Types::Name
|
1261
|
+
if var_names && var_types && test_types.all? {|type| type.is_a?(AST::Types::Name::Class) }
|
1289
1262
|
var_types_in_body = test_types.flat_map {|test_type|
|
1290
|
-
filtered_types = var_types.select {|var_type| var_type.name
|
1263
|
+
filtered_types = var_types.select {|var_type| var_type.name == test_type.name }
|
1291
1264
|
if filtered_types.empty?
|
1292
|
-
test_type
|
1265
|
+
to_instance_type(test_type)
|
1293
1266
|
else
|
1294
1267
|
filtered_types
|
1295
1268
|
end
|
1296
1269
|
}
|
1297
1270
|
var_types.reject! {|type|
|
1298
1271
|
var_types_in_body.any? {|test_type|
|
1299
|
-
test_type.name
|
1272
|
+
test_type.name == type.name
|
1300
1273
|
}
|
1301
1274
|
}
|
1302
1275
|
|
@@ -1312,7 +1285,7 @@ module Steep
|
|
1312
1285
|
pairs << [type, body_construction.type_env]
|
1313
1286
|
end
|
1314
1287
|
else
|
1315
|
-
pairs << [
|
1288
|
+
pairs << [AST::Builtin.nil_type, nil]
|
1316
1289
|
end
|
1317
1290
|
else
|
1318
1291
|
if clause
|
@@ -1325,7 +1298,7 @@ module Steep
|
|
1325
1298
|
else
|
1326
1299
|
typing.add_error Errors::ElseOnExhaustiveCase.new(node: node, type: cond_type)
|
1327
1300
|
type_case_override = var_names.each.with_object({}) do |var_name, hash|
|
1328
|
-
hash[var_name] =
|
1301
|
+
hash[var_name] = AST::Builtin.any_type
|
1329
1302
|
end
|
1330
1303
|
end
|
1331
1304
|
end
|
@@ -1343,7 +1316,7 @@ module Steep
|
|
1343
1316
|
|
1344
1317
|
if var_types
|
1345
1318
|
unless var_types.empty? || whens.last
|
1346
|
-
types.push
|
1319
|
+
types.push AST::Builtin.nil_type
|
1347
1320
|
end
|
1348
1321
|
end
|
1349
1322
|
|
@@ -1384,15 +1357,15 @@ module Steep
|
|
1384
1357
|
instance_types = exn_types.map do |type|
|
1385
1358
|
type = expand_alias(type)
|
1386
1359
|
case
|
1387
|
-
when type.is_a?(AST::Types::Name
|
1388
|
-
type
|
1360
|
+
when type.is_a?(AST::Types::Name::Class)
|
1361
|
+
to_instance_type(type)
|
1389
1362
|
else
|
1390
|
-
|
1363
|
+
AST::Builtin.any_type
|
1391
1364
|
end
|
1392
1365
|
end
|
1393
1366
|
type_override[var_name] = AST::Types::Union.build(types: instance_types)
|
1394
1367
|
when var_name
|
1395
|
-
type_override[var_name] =
|
1368
|
+
type_override[var_name] = AST::Builtin.any_type
|
1396
1369
|
end
|
1397
1370
|
|
1398
1371
|
resbody_construction = for_branch(resbody, type_case_override: type_override)
|
@@ -1400,7 +1373,7 @@ module Steep
|
|
1400
1373
|
type = if body
|
1401
1374
|
resbody_construction.synthesize(body)
|
1402
1375
|
else
|
1403
|
-
|
1376
|
+
AST::Builtin.nil_type
|
1404
1377
|
end
|
1405
1378
|
[type, resbody_construction.type_env]
|
1406
1379
|
end
|
@@ -1451,12 +1424,12 @@ module Steep
|
|
1451
1424
|
type_env.join!([for_loop.type_env])
|
1452
1425
|
end
|
1453
1426
|
|
1454
|
-
typing.add_typing(node,
|
1427
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
1455
1428
|
end
|
1456
1429
|
|
1457
1430
|
when :irange, :erange
|
1458
1431
|
types = node.children.map {|n| synthesize(n) }
|
1459
|
-
type =
|
1432
|
+
type = AST::Builtin::Range.instance_type(union_type(*types))
|
1460
1433
|
typing.add_typing(node, type)
|
1461
1434
|
|
1462
1435
|
when :regexp
|
@@ -1464,14 +1437,14 @@ module Steep
|
|
1464
1437
|
synthesize(child)
|
1465
1438
|
end
|
1466
1439
|
|
1467
|
-
typing.add_typing(node, AST::
|
1440
|
+
typing.add_typing(node, AST::Builtin::Regexp.instance_type)
|
1468
1441
|
|
1469
1442
|
when :regopt
|
1470
1443
|
# ignore
|
1471
|
-
typing.add_typing(node,
|
1444
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
1472
1445
|
|
1473
1446
|
when :nth_ref, :back_ref
|
1474
|
-
typing.add_typing(node,
|
1447
|
+
typing.add_typing(node, AST::Builtin::String.instance_type)
|
1475
1448
|
|
1476
1449
|
when :or_asgn, :and_asgn
|
1477
1450
|
yield_self do
|
@@ -1485,12 +1458,13 @@ module Steep
|
|
1485
1458
|
synthesize(child)
|
1486
1459
|
end
|
1487
1460
|
|
1488
|
-
typing.add_typing(node,
|
1461
|
+
typing.add_typing(node, AST::Builtin.any_type)
|
1489
1462
|
|
1490
1463
|
when :gvasgn
|
1491
1464
|
yield_self do
|
1492
1465
|
name, rhs = node.children
|
1493
|
-
type = checker.builder.signatures.find_gvar(name)&.type
|
1466
|
+
type = checker.builder.absolute_type(checker.builder.signatures.find_gvar(name)&.type,
|
1467
|
+
current: AST::Namespace.root)
|
1494
1468
|
|
1495
1469
|
if type
|
1496
1470
|
check(rhs, type) do |_, rhs_type, result|
|
@@ -1508,13 +1482,11 @@ module Steep
|
|
1508
1482
|
when :gvar
|
1509
1483
|
yield_self do
|
1510
1484
|
name = node.children.first
|
1511
|
-
type =
|
1512
|
-
|
1513
|
-
if type
|
1514
|
-
typing.add_typing(node, type)
|
1515
|
-
else
|
1516
|
-
fallback_to_any node
|
1485
|
+
type = type_env.get(gvar: name) do
|
1486
|
+
typing.add_error Errors::FallbackAny.new(node: node)
|
1517
1487
|
end
|
1488
|
+
|
1489
|
+
typing.add_typing(node, type)
|
1518
1490
|
end
|
1519
1491
|
|
1520
1492
|
when :block_pass
|
@@ -1525,7 +1497,7 @@ module Steep
|
|
1525
1497
|
if hint.one_arg?
|
1526
1498
|
# Assumes Symbol#to_proc implementation
|
1527
1499
|
param_type = hint.params.required[0]
|
1528
|
-
interface = checker.resolve(param_type
|
1500
|
+
interface = checker.resolve(param_type)
|
1529
1501
|
method = interface.methods[value.children[0]]
|
1530
1502
|
if method
|
1531
1503
|
return_types = method.types.flat_map do |method_type|
|
@@ -1554,7 +1526,7 @@ module Steep
|
|
1554
1526
|
synthesize(child)
|
1555
1527
|
end
|
1556
1528
|
|
1557
|
-
typing.add_typing node,
|
1529
|
+
typing.add_typing node, AST::Builtin.any_type
|
1558
1530
|
end
|
1559
1531
|
|
1560
1532
|
when :splat, :sclass
|
@@ -1565,7 +1537,7 @@ module Steep
|
|
1565
1537
|
synthesize(child)
|
1566
1538
|
end
|
1567
1539
|
|
1568
|
-
typing.add_typing node,
|
1540
|
+
typing.add_typing node, AST::Builtin.any_type
|
1569
1541
|
end
|
1570
1542
|
|
1571
1543
|
else
|
@@ -1658,7 +1630,7 @@ module Steep
|
|
1658
1630
|
|
1659
1631
|
case
|
1660
1632
|
when asgn.type == :lvasgn && asgn.children[0].name != :_
|
1661
|
-
type ||=
|
1633
|
+
type ||= AST::Builtin.nil_type
|
1662
1634
|
type_env.assign(lvar: asgn.children[0].name, type: type) do |result|
|
1663
1635
|
var_type = type_env.get(lvar: asgn.children[0].name)
|
1664
1636
|
typing.add_error(Errors::IncompatibleAssignment.new(node: node,
|
@@ -1667,7 +1639,7 @@ module Steep
|
|
1667
1639
|
result: result))
|
1668
1640
|
end
|
1669
1641
|
when asgn.type == :ivasgn
|
1670
|
-
type ||=
|
1642
|
+
type ||= AST::Builtin.nil_type
|
1671
1643
|
type_env.assign(ivar: asgn.children[0], type: type) do |result|
|
1672
1644
|
var_type = type_env.get(ivar: asgn.children[0])
|
1673
1645
|
typing.add_error(Errors::IncompatibleAssignment.new(node: node,
|
@@ -1683,7 +1655,7 @@ module Steep
|
|
1683
1655
|
when rhs_type.is_a?(AST::Types::Any)
|
1684
1656
|
fallback_to_any(node)
|
1685
1657
|
|
1686
|
-
when
|
1658
|
+
when AST::Builtin::Array.instance_type?(rhs_type)
|
1687
1659
|
element_type = rhs_type.args.first
|
1688
1660
|
|
1689
1661
|
lhs.children.each do |assignment|
|
@@ -1711,7 +1683,7 @@ module Steep
|
|
1711
1683
|
typing.add_typing node, rhs_type
|
1712
1684
|
|
1713
1685
|
when rhs_type.is_a?(AST::Types::Union) &&
|
1714
|
-
rhs_type.types.all? {|type|
|
1686
|
+
rhs_type.types.all? {|type| AST::Builtin::Array.instance_type?(type) }
|
1715
1687
|
|
1716
1688
|
types = rhs_type.types.flat_map do |type|
|
1717
1689
|
type.args.first
|
@@ -1779,14 +1751,15 @@ module Steep
|
|
1779
1751
|
|
1780
1752
|
case receiver_type
|
1781
1753
|
when AST::Types::Any
|
1782
|
-
typing.add_typing node,
|
1754
|
+
typing.add_typing node, AST::Builtin.any_type
|
1783
1755
|
|
1784
1756
|
when nil
|
1785
1757
|
fallback_to_any node
|
1786
1758
|
|
1787
1759
|
else
|
1788
1760
|
begin
|
1789
|
-
interface = checker.resolve(receiver_type
|
1761
|
+
interface = checker.resolve(receiver_type)
|
1762
|
+
|
1790
1763
|
method = interface.methods[method_name]
|
1791
1764
|
|
1792
1765
|
if method
|
@@ -1820,7 +1793,7 @@ module Steep
|
|
1820
1793
|
unless typing.has_type?(arg)
|
1821
1794
|
if arg.type == :splat
|
1822
1795
|
type = synthesize(arg.children[0])
|
1823
|
-
typing.add_typing(arg,
|
1796
|
+
typing.add_typing(arg, AST::Builtin::Array.instance_type(type))
|
1824
1797
|
else
|
1825
1798
|
synthesize(arg)
|
1826
1799
|
end
|
@@ -1831,11 +1804,11 @@ module Steep
|
|
1831
1804
|
unless typing.has_type?(block_body)
|
1832
1805
|
block_annotations = source.annotations(block: node, builder: checker.builder, current_module: current_namespace)
|
1833
1806
|
params = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations)
|
1834
|
-
pairs = params.each.map {|param| [param,
|
1807
|
+
pairs = params.each.map {|param| [param, AST::Builtin.any_type] }
|
1835
1808
|
|
1836
1809
|
for_block, _ = for_block(block_annotations: block_annotations,
|
1837
1810
|
param_pairs: pairs,
|
1838
|
-
method_return_type:
|
1811
|
+
method_return_type: AST::Builtin.any_type,
|
1839
1812
|
typing: typing)
|
1840
1813
|
|
1841
1814
|
for_block.synthesize(block_body)
|
@@ -1928,7 +1901,7 @@ module Steep
|
|
1928
1901
|
|
1929
1902
|
case method_type.return_type
|
1930
1903
|
when AST::Types::Var
|
1931
|
-
|
1904
|
+
AST::Builtin.any_type
|
1932
1905
|
else
|
1933
1906
|
method_type.return_type
|
1934
1907
|
end
|
@@ -2052,7 +2025,7 @@ module Steep
|
|
2052
2025
|
relation = Subtyping::Relation.new(sub_type: block_type,
|
2053
2026
|
super_type: method_type.block.yield_self do |expected_block|
|
2054
2027
|
if expected_block.optional?
|
2055
|
-
|
2028
|
+
AST::Builtin.optional(expected_block.type)
|
2056
2029
|
else
|
2057
2030
|
expected_block.type
|
2058
2031
|
end
|
@@ -2114,7 +2087,7 @@ module Steep
|
|
2114
2087
|
else
|
2115
2088
|
block_params.each do |param|
|
2116
2089
|
var_name = param.var.name
|
2117
|
-
param_types_hash[var_name] = param.type ||
|
2090
|
+
param_types_hash[var_name] = param.type || AST::Builtin.any_type
|
2118
2091
|
end
|
2119
2092
|
end
|
2120
2093
|
|
@@ -2171,7 +2144,7 @@ module Steep
|
|
2171
2144
|
for_block_body.synthesize(block_body, hint: block_type_hint)
|
2172
2145
|
end
|
2173
2146
|
else
|
2174
|
-
return_type =
|
2147
|
+
return_type = AST::Builtin.any_type
|
2175
2148
|
end
|
2176
2149
|
|
2177
2150
|
AST::Types::Proc.new(
|
@@ -2340,7 +2313,7 @@ module Steep
|
|
2340
2313
|
if type.params.rest
|
2341
2314
|
a = nodes.first
|
2342
2315
|
if a&.type == :restarg
|
2343
|
-
env[a.children.first] =
|
2316
|
+
env[a.children.first] = AST::Builtin::Array.instance_type(type.params.rest)
|
2344
2317
|
nodes.shift
|
2345
2318
|
end
|
2346
2319
|
end
|
@@ -2361,7 +2334,7 @@ module Steep
|
|
2361
2334
|
if node.type == :kwrestarg
|
2362
2335
|
ty = type.params.rest_keywords
|
2363
2336
|
if ty
|
2364
|
-
env[node.children[0]] =
|
2337
|
+
env[node.children[0]] = AST::Builtin::Hash.instance_type(AST::Builtin::Symbol.instance_type, ty)
|
2365
2338
|
end
|
2366
2339
|
end
|
2367
2340
|
end
|
@@ -2374,23 +2347,20 @@ module Steep
|
|
2374
2347
|
end
|
2375
2348
|
|
2376
2349
|
def current_namespace
|
2377
|
-
module_context&.current_namespace
|
2350
|
+
module_context&.current_namespace || AST::Namespace.root
|
2378
2351
|
end
|
2379
2352
|
|
2380
|
-
def
|
2381
|
-
|
2382
|
-
|
2383
|
-
current_namespace
|
2384
|
-
when current_namespace
|
2385
|
-
current_namespace + new
|
2353
|
+
def nested_namespace_for_module(module_name)
|
2354
|
+
if module_name.relative? && module_name.namespace.empty?
|
2355
|
+
current_namespace.append(module_name.name)
|
2386
2356
|
else
|
2387
|
-
|
2357
|
+
current_namespace
|
2388
2358
|
end
|
2389
2359
|
end
|
2390
2360
|
|
2391
2361
|
def absolute_name(module_name)
|
2392
2362
|
if current_namespace
|
2393
|
-
current_namespace
|
2363
|
+
module_name.in_namespace(current_namespace)
|
2394
2364
|
else
|
2395
2365
|
module_name.absolute!
|
2396
2366
|
end
|
@@ -2495,7 +2465,7 @@ module Steep
|
|
2495
2465
|
typing.add_error Errors::FallbackAny.new(node: node)
|
2496
2466
|
end
|
2497
2467
|
|
2498
|
-
typing.add_typing node,
|
2468
|
+
typing.add_typing node, AST::Builtin.any_type
|
2499
2469
|
end
|
2500
2470
|
|
2501
2471
|
def self_class?(node)
|
@@ -2614,5 +2584,54 @@ module Steep
|
|
2614
2584
|
sub_type
|
2615
2585
|
end
|
2616
2586
|
end
|
2587
|
+
|
2588
|
+
def to_instance_type(type, args: nil)
|
2589
|
+
args = args || case type
|
2590
|
+
when AST::Types::Name::Class
|
2591
|
+
checker.builder.signatures.find_class(type.name).params&.variables || []
|
2592
|
+
when AST::Types::Name::Module
|
2593
|
+
checker.builder.signatures.find_module(type.name).params&.variables || []
|
2594
|
+
end
|
2595
|
+
|
2596
|
+
AST::Types::Name::Instance.new(name: type.name, args: args)
|
2597
|
+
end
|
2598
|
+
|
2599
|
+
def try_hash_type(node, hint)
|
2600
|
+
if hint.is_a?(AST::Types::Hash)
|
2601
|
+
typing.new_child do |child_typing|
|
2602
|
+
new_construction = with_new_typing(child_typing)
|
2603
|
+
elem_types = hint.elements.dup
|
2604
|
+
|
2605
|
+
each_child_node(node) do |child|
|
2606
|
+
case child.type
|
2607
|
+
when :pair
|
2608
|
+
key, value = child.children
|
2609
|
+
|
2610
|
+
key_value = case key.type
|
2611
|
+
when :str, :int, :sym
|
2612
|
+
key.children[0]
|
2613
|
+
else
|
2614
|
+
return nil
|
2615
|
+
end
|
2616
|
+
|
2617
|
+
return nil unless elem_types.key?(key_value)
|
2618
|
+
|
2619
|
+
key_hint = AST::Types::Literal.new(value: key_value)
|
2620
|
+
value_hint = elem_types.delete(key_value)
|
2621
|
+
|
2622
|
+
new_construction.check(key, key_hint) { return nil }
|
2623
|
+
new_construction.check(value, value_hint) { return nil }
|
2624
|
+
else
|
2625
|
+
return nil
|
2626
|
+
end
|
2627
|
+
end
|
2628
|
+
|
2629
|
+
return nil unless elem_types.empty?
|
2630
|
+
|
2631
|
+
child_typing.save!
|
2632
|
+
return typing.add_typing(node, hint)
|
2633
|
+
end
|
2634
|
+
end
|
2635
|
+
end
|
2617
2636
|
end
|
2618
2637
|
end
|