steep 0.11.1 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +27 -0
- data/.gitmodules +3 -0
- data/CHANGELOG.md +5 -0
- data/README.md +48 -90
- data/Rakefile +10 -6
- data/Steepfile +1 -0
- data/bin/setup +1 -0
- data/bin/smoke_runner.rb +9 -14
- data/exe/rbs +3 -0
- data/exe/ruby-signature +3 -0
- data/exe/steep +1 -0
- data/lib/steep.rb +32 -26
- data/lib/steep/annotation_parser.rb +167 -0
- data/lib/steep/ast/annotation/collection.rb +7 -7
- data/lib/steep/ast/types.rb +60 -0
- data/lib/steep/ast/types/any.rb +1 -1
- data/lib/steep/ast/types/factory.rb +535 -0
- data/lib/steep/ast/types/name.rb +3 -3
- data/lib/steep/ast/types/var.rb +1 -1
- data/lib/steep/cli.rb +56 -240
- data/lib/steep/drivers/annotations.rb +36 -19
- data/lib/steep/drivers/check.rb +55 -91
- data/lib/steep/drivers/init.rb +54 -0
- data/lib/steep/drivers/langserver.rb +241 -150
- data/lib/steep/drivers/print_project.rb +56 -0
- data/lib/steep/drivers/signature_error_printer.rb +25 -0
- data/lib/steep/drivers/trace_printer.rb +25 -0
- data/lib/steep/drivers/utils/driver_helper.rb +26 -0
- data/lib/steep/drivers/validate.rb +18 -38
- data/lib/steep/drivers/vendor.rb +46 -0
- data/lib/steep/drivers/watch.rb +78 -140
- data/lib/steep/errors.rb +22 -13
- data/lib/steep/interface/interface.rb +91 -0
- data/lib/steep/interface/method.rb +0 -4
- data/lib/steep/interface/method_type.rb +362 -2
- data/lib/steep/interface/substitution.rb +22 -0
- data/lib/steep/project.rb +25 -233
- data/lib/steep/project/dsl.rb +132 -0
- data/lib/steep/project/file.rb +93 -76
- data/lib/steep/project/file_loader.rb +63 -0
- data/lib/steep/project/options.rb +7 -0
- data/lib/steep/project/target.rb +190 -0
- data/lib/steep/signature/errors.rb +25 -77
- data/lib/steep/signature/validator.rb +122 -0
- data/lib/steep/source.rb +12 -7
- data/lib/steep/subtyping/check.rb +357 -633
- data/lib/steep/subtyping/constraints.rb +2 -2
- data/lib/steep/subtyping/trace.rb +23 -0
- data/lib/steep/type_construction.rb +509 -455
- data/lib/steep/type_inference/constant_env.rb +16 -24
- data/lib/steep/type_inference/type_env.rb +26 -18
- data/lib/steep/version.rb +1 -1
- data/sample/Steepfile +6 -0
- data/sample/lib/conference.rb +12 -0
- data/sample/sig/conference.rbs +6 -0
- data/smoke/alias/Steepfile +4 -0
- data/smoke/alias/a.rb +2 -2
- data/smoke/alias/{a.rbi → a.rbs} +1 -1
- data/smoke/and/Steepfile +4 -0
- data/smoke/array/Steepfile +4 -0
- data/smoke/array/a.rb +2 -2
- data/smoke/array/b.rb +4 -4
- data/smoke/array/c.rb +2 -2
- data/smoke/block/Steepfile +5 -0
- data/smoke/block/{a.rbi → a.rbs} +1 -1
- data/smoke/block/{c.rbi → c.rbs} +0 -0
- data/smoke/block/d.rb +6 -6
- data/smoke/case/Steepfile +4 -0
- data/smoke/case/a.rb +4 -3
- data/smoke/class/Steepfile +4 -0
- data/smoke/class/a.rb +1 -4
- data/smoke/class/a.rbs +24 -0
- data/smoke/class/h.rb +6 -2
- data/smoke/class/{h.rbi → h.rbs} +1 -2
- data/smoke/class/i.rb +1 -2
- data/smoke/class/i.rbs +9 -0
- data/smoke/const/Steepfile +4 -0
- data/smoke/dstr/Steepfile +4 -0
- data/smoke/ensure/Steepfile +4 -0
- data/smoke/ensure/a.rb +1 -1
- data/smoke/enumerator/Steepfile +4 -0
- data/smoke/enumerator/a.rb +7 -7
- data/smoke/enumerator/b.rb +6 -6
- data/smoke/extension/Steepfile +4 -0
- data/smoke/extension/{a.rbi → a.rbs} +2 -2
- data/smoke/extension/{e.rbi → e.rbs} +2 -2
- data/smoke/hash/Steepfile +4 -0
- data/smoke/hash/{a.rbi → a.rbs} +0 -0
- data/smoke/hash/b.rb +2 -2
- data/smoke/hash/c.rb +1 -1
- data/smoke/hash/e.rbs +3 -0
- data/smoke/hash/f.rb +1 -1
- data/smoke/hello/Steepfile +4 -0
- data/smoke/hello/hello.rbs +7 -0
- data/smoke/if/Steepfile +4 -0
- data/smoke/implements/Steepfile +4 -0
- data/smoke/implements/a.rbs +6 -0
- data/smoke/initialize/Steepfile +4 -0
- data/smoke/initialize/a.rbs +3 -0
- data/smoke/integer/Steepfile +4 -0
- data/smoke/integer/a.rb +5 -3
- data/smoke/interface/Steepfile +4 -0
- data/smoke/interface/{a.rbi → a.rbs} +0 -0
- data/smoke/kwbegin/Steepfile +4 -0
- data/smoke/lambda/Steepfile +4 -0
- data/smoke/lambda/a.rb +9 -2
- data/smoke/literal/Steepfile +4 -0
- data/smoke/literal/{literal_methods.rbi → literal_methods.rbs} +0 -0
- data/smoke/map/Steepfile +4 -0
- data/smoke/map/a.rb +1 -1
- data/smoke/method/Steepfile +4 -0
- data/smoke/method/{a.rbi → a.rbs} +0 -0
- data/smoke/method/b.rb +1 -4
- data/smoke/method/d.rb +1 -0
- data/smoke/method/d.rbs +3 -0
- data/smoke/module/Steepfile +4 -0
- data/smoke/module/a.rb +1 -1
- data/smoke/module/a.rbs +16 -0
- data/smoke/module/c.rb +1 -1
- data/smoke/regexp/Steepfile +4 -0
- data/smoke/regexp/a.rb +2 -2
- data/smoke/regexp/b.rb +16 -16
- data/smoke/regression/Steepfile +5 -0
- data/smoke/regression/array.rb +2 -2
- data/smoke/regression/hash.rb +2 -2
- data/smoke/regression/poly_new.rb +2 -0
- data/smoke/regression/poly_new.rbs +4 -0
- data/smoke/regression/set_divide.rb +2 -2
- data/smoke/rescue/Steepfile +4 -0
- data/smoke/rescue/a.rb +1 -1
- data/smoke/self/Steepfile +4 -0
- data/smoke/self/a.rbs +4 -0
- data/smoke/skip/Steepfile +4 -0
- data/smoke/stdout/Steepfile +4 -0
- data/smoke/stdout/{a.rbi → a.rbs} +1 -1
- data/smoke/super/Steepfile +4 -0
- data/smoke/super/a.rbs +10 -0
- data/smoke/type_case/Steepfile +4 -0
- data/smoke/type_case/a.rb +1 -1
- data/smoke/yield/Steepfile +4 -0
- data/smoke/yield/a.rb +2 -2
- data/steep.gemspec +14 -7
- data/vendor/ruby-signature/.github/workflows/ruby.yml +27 -0
- data/vendor/ruby-signature/.gitignore +12 -0
- data/vendor/ruby-signature/.rubocop.yml +15 -0
- data/vendor/ruby-signature/BSDL +22 -0
- data/vendor/ruby-signature/COPYING +56 -0
- data/vendor/ruby-signature/Gemfile +6 -0
- data/vendor/ruby-signature/README.md +93 -0
- data/vendor/ruby-signature/Rakefile +66 -0
- data/vendor/ruby-signature/bin/annotate-with-rdoc +156 -0
- data/vendor/ruby-signature/bin/console +14 -0
- data/vendor/ruby-signature/bin/query-rdoc +103 -0
- data/vendor/ruby-signature/bin/setup +10 -0
- data/vendor/ruby-signature/bin/sort +88 -0
- data/vendor/ruby-signature/bin/test_runner.rb +17 -0
- data/vendor/ruby-signature/docs/CONTRIBUTING.md +97 -0
- data/vendor/ruby-signature/docs/sigs.md +148 -0
- data/vendor/ruby-signature/docs/stdlib.md +152 -0
- data/vendor/ruby-signature/docs/syntax.md +528 -0
- data/vendor/ruby-signature/exe/rbs +3 -0
- data/vendor/ruby-signature/exe/ruby-signature +7 -0
- data/vendor/ruby-signature/lib/ruby/signature.rb +64 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +29 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +29 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +391 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +364 -0
- data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +52 -0
- data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +54 -0
- data/vendor/ruby-signature/lib/ruby/signature/cli.rb +534 -0
- data/vendor/ruby-signature/lib/ruby/signature/constant.rb +28 -0
- data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +152 -0
- data/vendor/ruby-signature/lib/ruby/signature/definition.rb +172 -0
- data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +921 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment.rb +283 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +138 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +126 -0
- data/vendor/ruby-signature/lib/ruby/signature/errors.rb +189 -0
- data/vendor/ruby-signature/lib/ruby/signature/location.rb +104 -0
- data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +125 -0
- data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +93 -0
- data/vendor/ruby-signature/lib/ruby/signature/parser.y +1343 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +441 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +579 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +383 -0
- data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +48 -0
- data/vendor/ruby-signature/lib/ruby/signature/test.rb +28 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +63 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +290 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +58 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +324 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +185 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +256 -0
- data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +72 -0
- data/vendor/ruby-signature/lib/ruby/signature/types.rb +932 -0
- data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +140 -0
- data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +49 -0
- data/vendor/ruby-signature/lib/ruby/signature/version.rb +5 -0
- data/vendor/ruby-signature/lib/ruby/signature/writer.rb +271 -0
- data/vendor/ruby-signature/ruby-signature.gemspec +45 -0
- data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +3 -0
- data/vendor/ruby-signature/stdlib/base64/base64.rbs +15 -0
- data/vendor/ruby-signature/stdlib/builtin/array.rbs +1997 -0
- data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +280 -0
- data/vendor/ruby-signature/stdlib/builtin/binding.rbs +177 -0
- data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +35 -0
- data/vendor/ruby-signature/stdlib/builtin/class.rbs +145 -0
- data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +116 -0
- data/vendor/ruby-signature/stdlib/builtin/complex.rbs +400 -0
- data/vendor/ruby-signature/stdlib/builtin/constants.rbs +37 -0
- data/vendor/ruby-signature/stdlib/builtin/data.rbs +5 -0
- data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +2 -0
- data/vendor/ruby-signature/stdlib/builtin/dir.rbs +419 -0
- data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +606 -0
- data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +404 -0
- data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +260 -0
- data/vendor/ruby-signature/stdlib/builtin/errno.rbs +781 -0
- data/vendor/ruby-signature/stdlib/builtin/errors.rbs +582 -0
- data/vendor/ruby-signature/stdlib/builtin/exception.rbs +193 -0
- data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +40 -0
- data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +68 -0
- data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +12 -0
- data/vendor/ruby-signature/stdlib/builtin/file.rbs +476 -0
- data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +59 -0
- data/vendor/ruby-signature/stdlib/builtin/float.rbs +696 -0
- data/vendor/ruby-signature/stdlib/builtin/gc.rbs +121 -0
- data/vendor/ruby-signature/stdlib/builtin/hash.rbs +1029 -0
- data/vendor/ruby-signature/stdlib/builtin/integer.rbs +710 -0
- data/vendor/ruby-signature/stdlib/builtin/io.rbs +683 -0
- data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +574 -0
- data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +135 -0
- data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +141 -0
- data/vendor/ruby-signature/stdlib/builtin/math.rbs +66 -0
- data/vendor/ruby-signature/stdlib/builtin/method.rbs +182 -0
- data/vendor/ruby-signature/stdlib/builtin/module.rbs +248 -0
- data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +82 -0
- data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +409 -0
- data/vendor/ruby-signature/stdlib/builtin/object.rbs +824 -0
- data/vendor/ruby-signature/stdlib/builtin/proc.rbs +426 -0
- data/vendor/ruby-signature/stdlib/builtin/process.rbs +354 -0
- data/vendor/ruby-signature/stdlib/builtin/random.rbs +93 -0
- data/vendor/ruby-signature/stdlib/builtin/range.rbs +226 -0
- data/vendor/ruby-signature/stdlib/builtin/rational.rbs +424 -0
- data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +10 -0
- data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +131 -0
- data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +14 -0
- data/vendor/ruby-signature/stdlib/builtin/signal.rbs +55 -0
- data/vendor/ruby-signature/stdlib/builtin/string.rbs +770 -0
- data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +13 -0
- data/vendor/ruby-signature/stdlib/builtin/struct.rbs +40 -0
- data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +230 -0
- data/vendor/ruby-signature/stdlib/builtin/thread.rbs +1112 -0
- data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +23 -0
- data/vendor/ruby-signature/stdlib/builtin/time.rbs +739 -0
- data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +91 -0
- data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +46 -0
- data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +159 -0
- data/vendor/ruby-signature/stdlib/builtin/warning.rbs +17 -0
- data/vendor/ruby-signature/stdlib/erb/erb.rbs +18 -0
- data/vendor/ruby-signature/stdlib/find/find.rbs +44 -0
- data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +21 -0
- data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +23 -0
- data/vendor/ruby-signature/stdlib/prime/prime.rbs +188 -0
- data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +9 -0
- data/vendor/ruby-signature/stdlib/set/set.rbs +77 -0
- data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +244 -54
- data/.travis.yml +0 -7
- data/lib/steep/ast/signature/alias.rb +0 -19
- data/lib/steep/ast/signature/class.rb +0 -33
- data/lib/steep/ast/signature/const.rb +0 -17
- data/lib/steep/ast/signature/env.rb +0 -138
- data/lib/steep/ast/signature/extension.rb +0 -21
- data/lib/steep/ast/signature/gvar.rb +0 -17
- data/lib/steep/ast/signature/interface.rb +0 -31
- data/lib/steep/ast/signature/members.rb +0 -115
- data/lib/steep/ast/signature/module.rb +0 -21
- data/lib/steep/drivers/print_interface.rb +0 -94
- data/lib/steep/drivers/scaffold.rb +0 -321
- data/lib/steep/drivers/utils/each_signature.rb +0 -31
- data/lib/steep/interface/abstract.rb +0 -68
- data/lib/steep/interface/builder.rb +0 -637
- data/lib/steep/interface/instantiated.rb +0 -163
- data/lib/steep/interface/ivar_chain.rb +0 -26
- data/lib/steep/parser.y +0 -1278
- data/lib/steep/project/listener.rb +0 -53
- data/smoke/class/a.rbi +0 -24
- data/smoke/class/d.rb +0 -9
- data/smoke/class/e.rb +0 -12
- data/smoke/class/i.rbi +0 -9
- data/smoke/hash/e.rbi +0 -3
- data/smoke/hello/hello.rbi +0 -7
- data/smoke/implements/a.rbi +0 -6
- data/smoke/initialize/a.rbi +0 -3
- data/smoke/module/a.rbi +0 -16
- data/smoke/self/a.rbi +0 -4
- data/smoke/super/a.rbi +0 -10
- data/stdlib/builtin.rbi +0 -787
data/lib/steep/source.rb
CHANGED
@@ -44,7 +44,7 @@ module Steep
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
def self.parse(source_code, path:, labeling: ASTUtils::Labeling.new)
|
47
|
+
def self.parse(source_code, path:, factory:, labeling: ASTUtils::Labeling.new)
|
48
48
|
buffer = ::Parser::Source::Buffer.new(path.to_s, 1)
|
49
49
|
buffer.source = source_code
|
50
50
|
node = parser.parse(buffer).yield_self do |n|
|
@@ -68,10 +68,11 @@ module Steep
|
|
68
68
|
buffer = AST::Buffer.new(name: path, content: source_code)
|
69
69
|
|
70
70
|
comments.each do |comment|
|
71
|
-
src = comment.text.gsub(/\A
|
72
|
-
|
73
|
-
|
74
|
-
|
71
|
+
src = comment.text.gsub(/\A#\s*/, '')
|
72
|
+
location = AST::Location.new(buffer: buffer,
|
73
|
+
start_pos: comment.location.expression.begin_pos + 1,
|
74
|
+
end_pos: comment.location.expression.end_pos)
|
75
|
+
annotation = AnnotationParser.new(factory: factory).parse(src, location: location)
|
75
76
|
if annotation
|
76
77
|
annotations << LocatedAnnotation.new(line: comment.location.line, source: src, annotation: annotation)
|
77
78
|
end
|
@@ -274,8 +275,12 @@ module Steep
|
|
274
275
|
end
|
275
276
|
end
|
276
277
|
|
277
|
-
def annotations(block:,
|
278
|
-
AST::Annotation::Collection.new(
|
278
|
+
def annotations(block:, factory:, current_module:)
|
279
|
+
AST::Annotation::Collection.new(
|
280
|
+
annotations: mapping[block.__id__] || [],
|
281
|
+
factory: factory,
|
282
|
+
current_module: current_module
|
283
|
+
)
|
279
284
|
end
|
280
285
|
|
281
286
|
def each_annotation
|
@@ -1,18 +1,18 @@
|
|
1
1
|
module Steep
|
2
2
|
module Subtyping
|
3
3
|
class Check
|
4
|
-
attr_reader :
|
4
|
+
attr_reader :factory
|
5
5
|
attr_reader :cache
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@
|
7
|
+
def initialize(factory:)
|
8
|
+
@factory = factory
|
9
9
|
@cache = {}
|
10
10
|
end
|
11
11
|
|
12
|
-
def check(relation, constraints:, assumption: Set.new, trace: Trace.new)
|
12
|
+
def check(relation, constraints:, self_type:, assumption: Set.new, trace: Trace.new)
|
13
13
|
Steep.logger.tagged "#{relation.sub_type} <: #{relation.super_type}" do
|
14
14
|
prefix = trace.size
|
15
|
-
cached = cache[relation]
|
15
|
+
cached = cache[[relation, self_type]]
|
16
16
|
if cached && constraints.empty?
|
17
17
|
if cached.success?
|
18
18
|
cached
|
@@ -23,16 +23,14 @@ module Steep
|
|
23
23
|
if assumption.member?(relation)
|
24
24
|
success(constraints: constraints)
|
25
25
|
else
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
failure.drop(prefix)
|
31
|
-
end
|
32
|
-
|
33
|
-
Steep.logger.debug "result=#{result.class}"
|
34
|
-
cache[relation] = result if cacheable?(relation)
|
26
|
+
assumption = assumption + Set[relation]
|
27
|
+
check0(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints).tap do |result|
|
28
|
+
result = result.else do |failure|
|
29
|
+
failure.drop(prefix)
|
35
30
|
end
|
31
|
+
|
32
|
+
Steep.logger.debug "result=#{result.class}"
|
33
|
+
cache[[relation, self_type]] = result if cacheable?(relation)
|
36
34
|
end
|
37
35
|
end
|
38
36
|
end
|
@@ -55,118 +53,105 @@ module Steep
|
|
55
53
|
Result::Failure.new(error: error, trace: trace)
|
56
54
|
end
|
57
55
|
|
58
|
-
def check0(relation, assumption:, trace:, constraints:)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
success(constraints: constraints)
|
65
|
-
|
66
|
-
when relation.super_type.is_a?(AST::Types::Void)
|
67
|
-
success(constraints: constraints)
|
68
|
-
|
69
|
-
when relation.super_type.is_a?(AST::Types::Top)
|
70
|
-
success(constraints: constraints)
|
71
|
-
|
72
|
-
when relation.sub_type.is_a?(AST::Types::Bot)
|
73
|
-
success(constraints: constraints)
|
56
|
+
def check0(relation, self_type:, assumption:, trace:, constraints:)
|
57
|
+
# puts relation
|
58
|
+
trace.type(relation.sub_type, relation.super_type) do
|
59
|
+
case
|
60
|
+
when same_type?(relation, assumption: assumption)
|
61
|
+
success(constraints: constraints)
|
74
62
|
|
75
|
-
|
76
|
-
|
63
|
+
when relation.sub_type.is_a?(AST::Types::Any) || relation.super_type.is_a?(AST::Types::Any)
|
64
|
+
success(constraints: constraints)
|
77
65
|
|
78
|
-
|
79
|
-
|
80
|
-
Relation.new(sub_type: expand_alias(relation.sub_type), super_type: relation.super_type),
|
81
|
-
assumption: assumption,
|
82
|
-
trace: trace,
|
83
|
-
constraints: constraints
|
84
|
-
)
|
66
|
+
when relation.super_type.is_a?(AST::Types::Void)
|
67
|
+
success(constraints: constraints)
|
85
68
|
|
86
|
-
|
87
|
-
|
88
|
-
Relation.new(super_type: expand_alias(relation.super_type), sub_type: relation.sub_type),
|
89
|
-
assumption: assumption,
|
90
|
-
trace: trace,
|
91
|
-
constraints: constraints
|
92
|
-
)
|
69
|
+
when relation.super_type.is_a?(AST::Types::Top)
|
70
|
+
success(constraints: constraints)
|
93
71
|
|
94
|
-
|
95
|
-
|
96
|
-
Relation.new(sub_type: relation.sub_type.back_type, super_type: relation.super_type),
|
97
|
-
assumption: assumption,
|
98
|
-
trace: trace,
|
99
|
-
constraints: constraints
|
100
|
-
)
|
72
|
+
when relation.sub_type.is_a?(AST::Types::Bot)
|
73
|
+
success(constraints: constraints)
|
101
74
|
|
102
|
-
|
103
|
-
|
104
|
-
check0(Relation.new(sub_type: sub_type, super_type: relation.super_type),
|
105
|
-
assumption: assumption,
|
106
|
-
trace: trace,
|
107
|
-
constraints: constraints)
|
108
|
-
end
|
75
|
+
when relation.super_type.is_a?(AST::Types::Boolean)
|
76
|
+
success(constraints: constraints)
|
109
77
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
78
|
+
when relation.sub_type.is_a?(AST::Types::Self) && !self_type.is_a?(AST::Types::Self)
|
79
|
+
check(
|
80
|
+
Relation.new(sub_type: self_type, super_type: relation.super_type),
|
81
|
+
self_type: self_type,
|
82
|
+
assumption: assumption,
|
83
|
+
trace: trace,
|
84
|
+
constraints: constraints
|
85
|
+
)
|
115
86
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
87
|
+
when alias?(relation.sub_type)
|
88
|
+
check(
|
89
|
+
Relation.new(sub_type: expand_alias(relation.sub_type), super_type: relation.super_type),
|
90
|
+
self_type: self_type,
|
91
|
+
assumption: assumption,
|
92
|
+
trace: trace,
|
93
|
+
constraints: constraints
|
94
|
+
)
|
123
95
|
|
124
|
-
|
96
|
+
when alias?(relation.super_type)
|
97
|
+
check(
|
98
|
+
Relation.new(super_type: expand_alias(relation.super_type), sub_type: relation.sub_type),
|
99
|
+
self_type: self_type,
|
100
|
+
assumption: assumption,
|
101
|
+
trace: trace,
|
102
|
+
constraints: constraints
|
103
|
+
)
|
125
104
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
105
|
+
when relation.sub_type.is_a?(AST::Types::Literal)
|
106
|
+
check(
|
107
|
+
Relation.new(sub_type: relation.sub_type.back_type, super_type: relation.super_type),
|
108
|
+
self_type: self_type,
|
109
|
+
assumption: assumption,
|
110
|
+
trace: trace,
|
111
|
+
constraints: constraints
|
112
|
+
)
|
133
113
|
|
134
|
-
|
114
|
+
when relation.sub_type.is_a?(AST::Types::Union)
|
115
|
+
results = relation.sub_type.types.map do |sub_type|
|
116
|
+
check(Relation.new(sub_type: sub_type, super_type: relation.super_type),
|
117
|
+
self_type: self_type,
|
118
|
+
assumption: assumption,
|
119
|
+
trace: trace,
|
120
|
+
constraints: constraints)
|
121
|
+
end
|
135
122
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
constraints: constraints)
|
142
|
-
end
|
123
|
+
if results.all?(&:success?)
|
124
|
+
results.first
|
125
|
+
else
|
126
|
+
results.find(&:failure?)
|
127
|
+
end
|
143
128
|
|
144
|
-
|
145
|
-
results.
|
146
|
-
|
147
|
-
|
148
|
-
|
129
|
+
when relation.super_type.is_a?(AST::Types::Union)
|
130
|
+
results = relation.super_type.types.map do |super_type|
|
131
|
+
check(Relation.new(sub_type: relation.sub_type, super_type: super_type),
|
132
|
+
self_type: self_type,
|
133
|
+
assumption: assumption,
|
134
|
+
trace: trace,
|
135
|
+
constraints: constraints)
|
136
|
+
end
|
149
137
|
|
150
|
-
|
151
|
-
constraints.add(relation.super_type.name, sub_type: relation.sub_type)
|
152
|
-
success(constraints: constraints)
|
138
|
+
results.find(&:success?) || results.first
|
153
139
|
|
154
|
-
|
155
|
-
|
156
|
-
|
140
|
+
when relation.sub_type.is_a?(AST::Types::Intersection)
|
141
|
+
results = relation.sub_type.types.map do |sub_type|
|
142
|
+
check(Relation.new(sub_type: sub_type, super_type: relation.super_type),
|
143
|
+
self_type: self_type,
|
144
|
+
assumption: assumption,
|
145
|
+
trace: trace,
|
146
|
+
constraints: constraints)
|
147
|
+
end
|
157
148
|
|
158
|
-
|
159
|
-
failure(error: Result::Failure::UnknownPairError.new(relation: relation),
|
160
|
-
trace: trace)
|
149
|
+
results.find(&:success?) || results.first
|
161
150
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
[rel, rel.flip]
|
167
|
-
end
|
168
|
-
end.map do |relation|
|
169
|
-
check0(relation,
|
151
|
+
when relation.super_type.is_a?(AST::Types::Intersection)
|
152
|
+
results = relation.super_type.types.map do |super_type|
|
153
|
+
check(Relation.new(sub_type: relation.sub_type, super_type: super_type),
|
154
|
+
self_type: self_type,
|
170
155
|
assumption: assumption,
|
171
156
|
trace: trace,
|
172
157
|
constraints: constraints)
|
@@ -177,83 +162,131 @@ module Steep
|
|
177
162
|
else
|
178
163
|
results.find(&:failure?)
|
179
164
|
end
|
180
|
-
else
|
181
|
-
sub_interface = resolve(relation.sub_type)
|
182
|
-
super_interface = resolve(relation.super_type)
|
183
165
|
|
184
|
-
|
185
|
-
|
166
|
+
when relation.super_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.super_type.name)
|
167
|
+
constraints.add(relation.super_type.name, sub_type: relation.sub_type)
|
168
|
+
success(constraints: constraints)
|
186
169
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
assumption: assumption,
|
191
|
-
trace: trace,
|
192
|
-
constraints: constraints).then do
|
193
|
-
check0(Relation.new(sub_type: relation.sub_type.return_type, super_type: relation.super_type.return_type),
|
194
|
-
assumption: assumption,
|
195
|
-
trace: trace,
|
196
|
-
constraints: constraints)
|
197
|
-
end
|
170
|
+
when relation.sub_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.sub_type.name)
|
171
|
+
constraints.add(relation.sub_type.name, super_type: relation.super_type)
|
172
|
+
success(constraints: constraints)
|
198
173
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
results = pairs.flat_map do |t1, t2|
|
203
|
-
relation = Relation.new(sub_type: t1, super_type: t2)
|
204
|
-
[check0(relation, assumption: assumption, trace: trace, constraints: constraints),
|
205
|
-
check0(relation.flip, assumption: assumption, trace: trace, constraints: constraints)]
|
206
|
-
end
|
174
|
+
when relation.super_type.is_a?(AST::Types::Var) || relation.sub_type.is_a?(AST::Types::Var)
|
175
|
+
failure(error: Result::Failure::UnknownPairError.new(relation: relation),
|
176
|
+
trace: trace)
|
207
177
|
|
208
|
-
|
209
|
-
|
178
|
+
when relation.sub_type.is_a?(AST::Types::Name::Base) && relation.super_type.is_a?(AST::Types::Name::Base)
|
179
|
+
if (pairs = extract_nominal_pairs(relation))
|
180
|
+
results = pairs.flat_map do |(sub, sup)|
|
181
|
+
Relation.new(sub_type: sub, super_type: sup).yield_self do |rel|
|
182
|
+
[rel, rel.flip]
|
183
|
+
end
|
184
|
+
end.map do |relation|
|
185
|
+
check(relation,
|
186
|
+
self_type: self_type,
|
187
|
+
assumption: assumption,
|
188
|
+
trace: trace,
|
189
|
+
constraints: constraints)
|
190
|
+
end
|
191
|
+
|
192
|
+
if results.all?(&:success?)
|
193
|
+
results.first
|
194
|
+
else
|
195
|
+
results.find(&:failure?)
|
196
|
+
end
|
210
197
|
else
|
211
|
-
|
198
|
+
sub_interface = factory.interface(relation.sub_type, private: false)
|
199
|
+
super_interface = factory.interface(relation.super_type, private: false)
|
200
|
+
|
201
|
+
check_interface(sub_interface, super_interface, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)
|
212
202
|
end
|
213
|
-
else
|
214
|
-
failure(error: Result::Failure::UnknownPairError.new(relation: relation),
|
215
|
-
trace: trace)
|
216
|
-
end
|
217
203
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
keys = relation.super_type.elements.keys
|
231
|
-
type_pairs = keys.map {|key| [relation.sub_type.elements[key], relation.super_type.elements[key]] }
|
232
|
-
results = type_pairs.flat_map do |t1, t2|
|
233
|
-
relation = Relation.new(sub_type: t1, super_type: t2)
|
234
|
-
[check0(relation, assumption: assumption, trace: trace, constraints: constraints),
|
235
|
-
check0(relation.flip, assumption: assumption, trace: trace, constraints: constraints)]
|
204
|
+
when relation.sub_type.is_a?(AST::Types::Proc) && relation.super_type.is_a?(AST::Types::Proc)
|
205
|
+
check_method_params(:__proc__,
|
206
|
+
relation.sub_type.params, relation.super_type.params,
|
207
|
+
self_type: self_type,
|
208
|
+
assumption: assumption,
|
209
|
+
trace: trace,
|
210
|
+
constraints: constraints).then do
|
211
|
+
check(Relation.new(sub_type: relation.sub_type.return_type, super_type: relation.super_type.return_type),
|
212
|
+
self_type: self_type,
|
213
|
+
assumption: assumption,
|
214
|
+
trace: trace,
|
215
|
+
constraints: constraints)
|
236
216
|
end
|
237
217
|
|
238
|
-
|
239
|
-
|
218
|
+
when relation.sub_type.is_a?(AST::Types::Tuple) && relation.super_type.is_a?(AST::Types::Tuple)
|
219
|
+
if relation.sub_type.types.size >= relation.super_type.types.size
|
220
|
+
pairs = relation.sub_type.types.take(relation.super_type.types.size).zip(relation.super_type.types)
|
221
|
+
results = pairs.flat_map do |t1, t2|
|
222
|
+
relation = Relation.new(sub_type: t1, super_type: t2)
|
223
|
+
[check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints),
|
224
|
+
check(relation.flip, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)]
|
225
|
+
end
|
226
|
+
|
227
|
+
if results.all?(&:success?)
|
228
|
+
success(constraints: constraints)
|
229
|
+
else
|
230
|
+
results.find(&:failure?)
|
231
|
+
end
|
240
232
|
else
|
241
|
-
|
233
|
+
failure(error: Result::Failure::UnknownPairError.new(relation: relation),
|
234
|
+
trace: trace)
|
235
|
+
end
|
236
|
+
|
237
|
+
when relation.sub_type.is_a?(AST::Types::Tuple) && relation.super_type.is_a?(AST::Types::Name::Base)
|
238
|
+
tuple_interface = factory.interface(relation.sub_type, private: false)
|
239
|
+
type_interface = factory.interface(relation.super_type, private: false)
|
240
|
+
|
241
|
+
check_interface(tuple_interface,
|
242
|
+
type_interface,
|
243
|
+
self_type: self_type,
|
244
|
+
assumption: assumption,
|
245
|
+
trace: trace,
|
246
|
+
constraints: constraints)
|
247
|
+
|
248
|
+
when relation.sub_type.is_a?(AST::Types::Record) && relation.super_type.is_a?(AST::Types::Record)
|
249
|
+
if Set.new(relation.sub_type.elements.keys).superset?(Set.new(relation.super_type.elements.keys))
|
250
|
+
keys = relation.super_type.elements.keys
|
251
|
+
type_pairs = keys.map {|key| [relation.sub_type.elements[key], relation.super_type.elements[key]] }
|
252
|
+
results = type_pairs.flat_map do |t1, t2|
|
253
|
+
relation = Relation.new(sub_type: t1, super_type: t2)
|
254
|
+
[check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints),
|
255
|
+
check(relation.flip, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)]
|
256
|
+
end
|
257
|
+
|
258
|
+
if results.all?(&:success?)
|
259
|
+
success(constraints: constraints)
|
260
|
+
else
|
261
|
+
results.find(&:failure?)
|
262
|
+
end
|
263
|
+
else
|
264
|
+
failure(error: Result::Failure::UnknownPairError.new(relation: relation),
|
265
|
+
trace: trace)
|
242
266
|
end
|
267
|
+
|
268
|
+
when relation.sub_type.is_a?(AST::Types::Record) && relation.super_type.is_a?(AST::Types::Name::Base)
|
269
|
+
record_interface = factory.interface(relation.sub_type, private: false)
|
270
|
+
type_interface = factory.interface(relation.super_type, private: false)
|
271
|
+
|
272
|
+
check_interface(record_interface,
|
273
|
+
type_interface,
|
274
|
+
self_type: self_type,
|
275
|
+
assumption: assumption,
|
276
|
+
trace: trace,
|
277
|
+
constraints: constraints)
|
278
|
+
|
243
279
|
else
|
244
280
|
failure(error: Result::Failure::UnknownPairError.new(relation: relation),
|
245
281
|
trace: trace)
|
246
282
|
end
|
247
|
-
else
|
248
|
-
failure(error: Result::Failure::UnknownPairError.new(relation: relation),
|
249
|
-
trace: trace)
|
250
283
|
end
|
251
284
|
end
|
252
|
-
|
285
|
+
|
253
286
|
def extract_nominal_pairs(relation)
|
254
287
|
sub_type = relation.sub_type
|
255
288
|
super_type = relation.super_type
|
256
|
-
|
289
|
+
|
257
290
|
case
|
258
291
|
when sub_type.is_a?(AST::Types::Name::Instance) && super_type.is_a?(AST::Types::Name::Instance)
|
259
292
|
if sub_type.name == super_type.name && sub_type.args.size == super_type.args.size
|
@@ -299,119 +332,188 @@ module Steep
|
|
299
332
|
end
|
300
333
|
end
|
301
334
|
|
302
|
-
def check_interface(
|
303
|
-
|
335
|
+
def check_interface(sub_interface, super_interface, self_type:, assumption:, trace:, constraints:)
|
336
|
+
trace.interface sub_interface, super_interface do
|
337
|
+
method_triples = []
|
304
338
|
|
305
|
-
|
306
|
-
|
339
|
+
super_interface.methods.each do |name, sup_method|
|
340
|
+
sub_method = sub_interface.methods[name]
|
307
341
|
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
342
|
+
if sub_method
|
343
|
+
method_triples << [name, sub_method, sup_method]
|
344
|
+
else
|
345
|
+
return failure(error: Result::Failure::MethodMissingError.new(name: name),
|
346
|
+
trace: trace)
|
347
|
+
end
|
313
348
|
end
|
314
|
-
end
|
315
349
|
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
350
|
+
method_triples.each do |(method_name, sub_method, sup_method)|
|
351
|
+
result = check_method(method_name,
|
352
|
+
sub_method,
|
353
|
+
sup_method,
|
354
|
+
self_type: self_type,
|
355
|
+
assumption: assumption,
|
356
|
+
trace: trace,
|
357
|
+
constraints: constraints)
|
358
|
+
return result if result.failure?
|
359
|
+
end
|
320
360
|
|
321
|
-
|
361
|
+
success(constraints: constraints)
|
362
|
+
end
|
322
363
|
end
|
323
364
|
|
324
|
-
def check_method(name, sub_method, super_method, assumption:, trace:, constraints:)
|
325
|
-
trace.
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
sub_type,
|
333
|
-
super_type,
|
334
|
-
assumption: assumption,
|
335
|
-
trace: trace,
|
336
|
-
constraints: constraints)
|
337
|
-
|
338
|
-
when super_type.type_params.empty?
|
339
|
-
yield_self do
|
340
|
-
sub_args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
|
341
|
-
sub_type = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params,
|
342
|
-
sub_args))
|
343
|
-
|
344
|
-
constraints.add_var(*sub_args)
|
345
|
-
|
346
|
-
match_method_type(name, sub_type, super_type, trace: trace).yield_self do |pairs|
|
347
|
-
case pairs
|
348
|
-
when Array
|
349
|
-
subst = pairs.each.with_object(Interface::Substitution.empty) do |(sub, sup), subst|
|
350
|
-
case
|
351
|
-
when sub.is_a?(AST::Types::Var) && sub_args.include?(sub)
|
352
|
-
subst.add!(sub.name, sup)
|
353
|
-
when sup.is_a?(AST::Types::Var) && sub_args.include?(sup)
|
354
|
-
subst.add!(sup.name, sub)
|
355
|
-
end
|
356
|
-
end
|
357
|
-
|
358
|
-
check_method_type(name,
|
359
|
-
sub_type.subst(subst),
|
365
|
+
def check_method(name, sub_method, super_method, self_type:, assumption:, trace:, constraints:)
|
366
|
+
trace.method name, sub_method, super_method do
|
367
|
+
case
|
368
|
+
when sub_method.overload? && super_method.overload?
|
369
|
+
super_method.types.map do |super_type|
|
370
|
+
sub_method.types.map do |sub_type|
|
371
|
+
check_generic_method_type name,
|
372
|
+
sub_type,
|
360
373
|
super_type,
|
374
|
+
self_type: self_type,
|
361
375
|
assumption: assumption,
|
362
376
|
trace: trace,
|
363
|
-
constraints: constraints
|
377
|
+
constraints: constraints
|
378
|
+
end.yield_self do |results|
|
379
|
+
results.find(&:success?) || results[0]
|
380
|
+
end
|
381
|
+
end.yield_self do |results|
|
382
|
+
if results.all?(&:success?) || sub_method.incompatible?
|
383
|
+
success constraints: constraints
|
384
|
+
else
|
385
|
+
results.select(&:failure?).last
|
386
|
+
end
|
387
|
+
end
|
388
|
+
else
|
389
|
+
raise "aaaaaaaaaaaaaa"
|
390
|
+
end
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
def check_generic_method_type(name, sub_type, super_type, self_type:, assumption:, trace:, constraints:)
|
395
|
+
trace.method_type name, sub_type, super_type do
|
396
|
+
case
|
397
|
+
when sub_type.type_params.empty? && super_type.type_params.empty?
|
398
|
+
check_method_type name,
|
399
|
+
sub_type,
|
400
|
+
super_type,
|
401
|
+
self_type: self_type,
|
402
|
+
assumption: assumption,
|
403
|
+
trace: trace,
|
404
|
+
constraints: constraints
|
405
|
+
|
406
|
+
when !sub_type.type_params.empty? && super_type.type_params.empty?
|
407
|
+
# Check if super_type is an instance of sub_type.
|
408
|
+
yield_self do
|
409
|
+
sub_args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
|
410
|
+
sub_type = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params, sub_args))
|
411
|
+
|
412
|
+
constraints.add_var(*sub_args)
|
413
|
+
|
414
|
+
match_method_type(name, sub_type, super_type, trace: trace).yield_self do |pairs|
|
415
|
+
case pairs
|
416
|
+
when Array
|
417
|
+
subst = pairs.each.with_object(Interface::Substitution.empty) do |(sub, sup), subst|
|
418
|
+
case
|
419
|
+
when sub.is_a?(AST::Types::Var) && sub_args.include?(sub)
|
420
|
+
if subst.key?(sub.name) && subst[sub.name] != sup
|
421
|
+
return failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
|
422
|
+
trace: trace)
|
364
423
|
else
|
365
|
-
|
424
|
+
subst.add!(sub.name, sup)
|
425
|
+
end
|
426
|
+
when sup.is_a?(AST::Types::Var) && sub_args.include?(sup)
|
427
|
+
if subst.key?(sup.name) && subst[sup.name] != sub
|
428
|
+
return failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
|
429
|
+
trace: trace)
|
430
|
+
else
|
431
|
+
subst.add!(sup.name, sub)
|
366
432
|
end
|
367
433
|
end
|
368
434
|
end
|
369
435
|
|
370
|
-
|
371
|
-
|
372
|
-
|
436
|
+
check_method_type(name,
|
437
|
+
sub_type.subst(subst),
|
438
|
+
super_type,
|
439
|
+
self_type: self_type,
|
440
|
+
assumption: assumption,
|
441
|
+
trace: trace,
|
442
|
+
constraints: constraints)
|
443
|
+
else
|
444
|
+
pairs
|
445
|
+
end
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
when sub_type.type_params.empty? && !super_type.type_params.empty?
|
450
|
+
# Check if sub_type is an instance of super_type && no constraints on type variables (any).
|
451
|
+
yield_self do
|
452
|
+
sub_args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
|
453
|
+
sub_type = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params, sub_args))
|
373
454
|
|
374
|
-
|
375
|
-
super_type_ = super_type.instantiate(Interface::Substitution.build(super_type.type_params, args))
|
455
|
+
constraints.add_var(*sub_args)
|
376
456
|
|
377
|
-
|
457
|
+
match_method_type(name, sub_type, super_type, trace: trace).yield_self do |pairs|
|
458
|
+
case pairs
|
459
|
+
when Array
|
460
|
+
result = check_method_type(name,
|
461
|
+
sub_type,
|
462
|
+
super_type,
|
463
|
+
self_type: self_type,
|
464
|
+
assumption: assumption,
|
465
|
+
trace: trace,
|
466
|
+
constraints: constraints)
|
378
467
|
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
constraints: constraints)
|
468
|
+
if result.success? && sub_args.map(&:name).none? {|var| constraints.has_constraint?(var) }
|
469
|
+
result
|
470
|
+
else
|
471
|
+
failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
|
472
|
+
trace: trace)
|
385
473
|
end
|
474
|
+
|
386
475
|
else
|
387
|
-
|
388
|
-
trace: trace)
|
476
|
+
pairs
|
389
477
|
end
|
390
478
|
end
|
391
479
|
end
|
392
|
-
end
|
393
480
|
|
394
|
-
|
395
|
-
if
|
396
|
-
|
397
|
-
|
398
|
-
|
481
|
+
when super_type.type_params.size == sub_type.type_params.size
|
482
|
+
# Check if they have the same shape
|
483
|
+
yield_self do
|
484
|
+
args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
|
485
|
+
|
486
|
+
sub_type_ = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params, args))
|
487
|
+
super_type_ = super_type.instantiate(Interface::Substitution.build(super_type.type_params, args))
|
488
|
+
|
489
|
+
constraints.add_var(*args)
|
490
|
+
|
491
|
+
check_method_type(name,
|
492
|
+
sub_type_,
|
493
|
+
super_type_,
|
494
|
+
self_type: self_type,
|
495
|
+
assumption: assumption,
|
496
|
+
trace: trace,
|
497
|
+
constraints: constraints)
|
399
498
|
end
|
400
|
-
end
|
401
499
|
|
402
|
-
|
500
|
+
else
|
501
|
+
# Or error
|
502
|
+
failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
|
503
|
+
trace: trace)
|
504
|
+
end
|
403
505
|
end
|
404
506
|
end
|
405
507
|
|
406
|
-
def check_method_type(name, sub_type, super_type, assumption:, trace:, constraints:)
|
508
|
+
def check_method_type(name, sub_type, super_type, self_type:, assumption:, trace:, constraints:)
|
407
509
|
Steep.logger.tagged("#{name}: #{sub_type} <: #{super_type}") do
|
408
|
-
check_method_params(name, sub_type.params, super_type.params, assumption: assumption, trace: trace, constraints: constraints).then do
|
510
|
+
check_method_params(name, sub_type.params, super_type.params, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints).then do
|
409
511
|
check_block_given(name, sub_type.block, super_type.block, trace: trace, constraints: constraints).then do
|
410
|
-
check_block_params(name, sub_type.block, super_type.block, assumption: assumption, trace: trace, constraints: constraints).then do
|
411
|
-
check_block_return(sub_type.block, super_type.block, assumption: assumption, trace: trace, constraints:constraints).then do
|
512
|
+
check_block_params(name, sub_type.block, super_type.block, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints).then do
|
513
|
+
check_block_return(sub_type.block, super_type.block, self_type: self_type, assumption: assumption, trace: trace, constraints:constraints).then do
|
412
514
|
relation = Relation.new(super_type: super_type.return_type,
|
413
515
|
sub_type: sub_type.return_type)
|
414
|
-
check(relation, assumption: assumption, trace: trace, constraints: constraints)
|
516
|
+
check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)
|
415
517
|
end
|
416
518
|
end
|
417
519
|
end
|
@@ -435,14 +537,14 @@ module Steep
|
|
435
537
|
end
|
436
538
|
end
|
437
539
|
|
438
|
-
def check_method_params(name, sub_params, super_params, assumption:, trace:, constraints:)
|
540
|
+
def check_method_params(name, sub_params, super_params, self_type:, assumption:, trace:, constraints:)
|
439
541
|
match_params(name, sub_params, super_params, trace: trace).yield_self do |pairs|
|
440
542
|
case pairs
|
441
543
|
when Array
|
442
544
|
pairs.each do |(sub_type, super_type)|
|
443
545
|
relation = Relation.new(super_type: sub_type, sub_type: super_type)
|
444
546
|
|
445
|
-
result = check(relation, assumption: assumption, trace: trace, constraints: constraints)
|
547
|
+
result = check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)
|
446
548
|
return result if result.failure?
|
447
549
|
end
|
448
550
|
|
@@ -571,11 +673,12 @@ module Steep
|
|
571
673
|
pairs
|
572
674
|
end
|
573
675
|
|
574
|
-
def check_block_params(name, sub_block, super_block, assumption:, trace:, constraints:)
|
676
|
+
def check_block_params(name, sub_block, super_block, self_type:, assumption:, trace:, constraints:)
|
575
677
|
if sub_block && super_block
|
576
678
|
check_method_params(name,
|
577
679
|
super_block.type.params,
|
578
680
|
sub_block.type.params,
|
681
|
+
self_type: self_type,
|
579
682
|
assumption: assumption,
|
580
683
|
trace: trace,
|
581
684
|
constraints: constraints)
|
@@ -584,397 +687,18 @@ module Steep
|
|
584
687
|
end
|
585
688
|
end
|
586
689
|
|
587
|
-
def check_block_return(sub_block, super_block, assumption:, trace:, constraints:)
|
690
|
+
def check_block_return(sub_block, super_block, self_type:, assumption:, trace:, constraints:)
|
588
691
|
if sub_block && super_block
|
589
692
|
relation = Relation.new(sub_type: super_block.type.return_type,
|
590
693
|
super_type: sub_block.type.return_type)
|
591
|
-
check(relation, assumption: assumption, trace: trace, constraints: constraints)
|
694
|
+
check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)
|
592
695
|
else
|
593
696
|
success(constraints: constraints)
|
594
697
|
end
|
595
698
|
end
|
596
699
|
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
def initialize(type:)
|
601
|
-
@type = type
|
602
|
-
super "Type #{type} cannot resolve to interface"
|
603
|
-
end
|
604
|
-
end
|
605
|
-
|
606
|
-
def resolve_instance(type, self_type:, instance_type:, module_type:, with_private: false)
|
607
|
-
abstract_interface = builder.build_instance(type.name).without_private(!with_private)
|
608
|
-
|
609
|
-
module_type = module_type || case builder.signatures.find_class_or_module(type.name)
|
610
|
-
when AST::Signature::Class
|
611
|
-
AST::Types::Name::Class.new(name: type.name, constructor: nil)
|
612
|
-
when AST::Signature::Module
|
613
|
-
AST::Types::Name::Module.new(name: type.name)
|
614
|
-
end
|
615
|
-
|
616
|
-
abstract_interface.instantiate(
|
617
|
-
type: self_type,
|
618
|
-
args: type.args,
|
619
|
-
instance_type: instance_type || type,
|
620
|
-
module_type: module_type
|
621
|
-
)
|
622
|
-
end
|
623
|
-
|
624
|
-
def resolve(type, self_type: type, instance_type: nil, module_type: nil, with_private: false)
|
625
|
-
Steep.logger.debug("Check#resolve: type=#{type}")
|
626
|
-
case type
|
627
|
-
when AST::Types::Any, AST::Types::Var, AST::Types::Class, AST::Types::Instance
|
628
|
-
raise CannotResolveError.new(type: type)
|
629
|
-
|
630
|
-
when AST::Types::Nil, AST::Types::Literal, AST::Types::Boolean
|
631
|
-
resolve(type.back_type,
|
632
|
-
self_type: self_type,
|
633
|
-
instance_type: instance_type,
|
634
|
-
module_type: module_type,
|
635
|
-
with_private: with_private)
|
636
|
-
|
637
|
-
when AST::Types::Name::Instance
|
638
|
-
resolve_instance(type,
|
639
|
-
self_type: self_type,
|
640
|
-
instance_type: instance_type,
|
641
|
-
module_type: module_type,
|
642
|
-
with_private: with_private)
|
643
|
-
|
644
|
-
when AST::Types::Name::Class
|
645
|
-
yield_self do
|
646
|
-
abstract_interface = builder.build_class(type.name, constructor: type.constructor).without_private(!with_private)
|
647
|
-
|
648
|
-
unless instance_type
|
649
|
-
type_params = builder.signatures.find_class(type.name).params&.variables || []
|
650
|
-
instance_type = AST::Types::Name::Instance.new(name: type.name,
|
651
|
-
args: type_params.map {|var| AST::Types::Var.new(name: var) })
|
652
|
-
end
|
653
|
-
|
654
|
-
interface = abstract_interface.instantiate(
|
655
|
-
type: self_type,
|
656
|
-
args: [],
|
657
|
-
instance_type: instance_type,
|
658
|
-
module_type: AST::Builtin::Class.class_type
|
659
|
-
)
|
660
|
-
|
661
|
-
if type_params
|
662
|
-
interface.subst(Interface::Substitution.build(type_params, type_params.map { AST::Builtin.any_type }))
|
663
|
-
else
|
664
|
-
interface
|
665
|
-
end
|
666
|
-
end
|
667
|
-
|
668
|
-
when AST::Types::Name::Module
|
669
|
-
yield_self do
|
670
|
-
abstract_interface = builder.build_module(type.name).without_private(!with_private)
|
671
|
-
|
672
|
-
unless instance_type
|
673
|
-
type_params = builder.signatures.find_module(type.name).params&.variables || []
|
674
|
-
instance_type = AST::Types::Name::Instance.new(name: type.name,
|
675
|
-
args: type_params.map {|var| AST::Types::Var.new(name: var) })
|
676
|
-
end
|
677
|
-
|
678
|
-
interface = abstract_interface.instantiate(
|
679
|
-
type: self_type,
|
680
|
-
args: [],
|
681
|
-
instance_type: instance_type,
|
682
|
-
module_type: AST::Builtin::Module.class_type
|
683
|
-
)
|
684
|
-
|
685
|
-
if type_params
|
686
|
-
interface.subst(Interface::Substitution.build(type_params, type_params.map { AST::Builtin.any_type }))
|
687
|
-
else
|
688
|
-
interface
|
689
|
-
end
|
690
|
-
end
|
691
|
-
|
692
|
-
when AST::Types::Name::Interface
|
693
|
-
yield_self do
|
694
|
-
abstract_interface = builder.build_interface(type.name)
|
695
|
-
|
696
|
-
abstract_interface.instantiate(
|
697
|
-
type: self_type,
|
698
|
-
args: type.args,
|
699
|
-
instance_type: nil,
|
700
|
-
module_type: nil
|
701
|
-
)
|
702
|
-
end
|
703
|
-
|
704
|
-
when AST::Types::Name::Alias
|
705
|
-
resolve(expand_alias(type),
|
706
|
-
self_type: self_type,
|
707
|
-
instance_type: instance_type,
|
708
|
-
module_type: module_type,
|
709
|
-
with_private: with_private)
|
710
|
-
|
711
|
-
when AST::Types::Union
|
712
|
-
interfaces = type.types.map do |member_type|
|
713
|
-
fresh = AST::Types::Var.fresh(:___)
|
714
|
-
|
715
|
-
resolve(member_type, self_type: type, instance_type: fresh, module_type: fresh, with_private: with_private).select_method_type do |method_type|
|
716
|
-
!method_type.each_type.include?(fresh)
|
717
|
-
end
|
718
|
-
end
|
719
|
-
|
720
|
-
methods = interfaces.inject(nil) do |methods, i|
|
721
|
-
if methods
|
722
|
-
intersection = {}
|
723
|
-
i.methods.each do |name, new_method|
|
724
|
-
existing_method = methods[name]
|
725
|
-
|
726
|
-
if existing_method
|
727
|
-
case
|
728
|
-
when new_method == existing_method
|
729
|
-
intersection[name] = new_method
|
730
|
-
when check_method(name, new_method, existing_method,
|
731
|
-
assumption: Set.new,
|
732
|
-
trace: Trace.new,
|
733
|
-
constraints: Constraints.empty).success?
|
734
|
-
intersection[name] = existing_method
|
735
|
-
when check_method(name, existing_method, new_method,
|
736
|
-
assumption: Set.new,
|
737
|
-
trace: Trace.new,
|
738
|
-
constraints: Constraints.empty).success?
|
739
|
-
intersection[name] = new_method
|
740
|
-
else
|
741
|
-
merged_method_types = []
|
742
|
-
|
743
|
-
existing_method.types.each do |existing_method_type|
|
744
|
-
new_method.types.each do |new_method_type|
|
745
|
-
if existing_method_type.params == new_method_type.params &&
|
746
|
-
existing_method_type.block == new_method_type.block &&
|
747
|
-
existing_method_type.type_params == new_method_type.type_params
|
748
|
-
merged_method_types << existing_method_type.with(
|
749
|
-
return_type: AST::Types::Union.build(
|
750
|
-
types: [
|
751
|
-
existing_method_type.return_type,
|
752
|
-
new_method_type.return_type
|
753
|
-
]
|
754
|
-
),
|
755
|
-
location: nil
|
756
|
-
)
|
757
|
-
end
|
758
|
-
end
|
759
|
-
end
|
760
|
-
|
761
|
-
unless merged_method_types.empty?
|
762
|
-
intersection[name] = Interface::Method.new(
|
763
|
-
type_name: nil,
|
764
|
-
name: name,
|
765
|
-
types: merged_method_types,
|
766
|
-
super_method: nil,
|
767
|
-
attributes: []
|
768
|
-
)
|
769
|
-
end
|
770
|
-
end
|
771
|
-
end
|
772
|
-
end
|
773
|
-
intersection
|
774
|
-
else
|
775
|
-
i.methods
|
776
|
-
end
|
777
|
-
end
|
778
|
-
|
779
|
-
Interface::Instantiated.new(type: type,
|
780
|
-
methods: methods,
|
781
|
-
ivar_chains: {})
|
782
|
-
|
783
|
-
when AST::Types::Intersection
|
784
|
-
interfaces = type.types.map do |type|
|
785
|
-
resolve(type, with_private: with_private)
|
786
|
-
end
|
787
|
-
|
788
|
-
methods = interfaces.inject(nil) do |methods, i|
|
789
|
-
if methods
|
790
|
-
i.methods.each do |name, method|
|
791
|
-
if methods.key?(name)
|
792
|
-
case
|
793
|
-
when method == methods[name]
|
794
|
-
when check_method(name, method, methods[name],
|
795
|
-
assumption: Set.new,
|
796
|
-
trace: Trace.new,
|
797
|
-
constraints: Constraints.empty).success?
|
798
|
-
methods[name] = method
|
799
|
-
when check_method(name, methods[name], method,
|
800
|
-
assumption: Set.new,
|
801
|
-
trace: Trace.new,
|
802
|
-
constraints: Constraints.empty).success?
|
803
|
-
methods[name] = methods[name]
|
804
|
-
else
|
805
|
-
methods[name] = Interface::Method.new(
|
806
|
-
type_name: nil,
|
807
|
-
name: name,
|
808
|
-
types: methods[name].types + method.types,
|
809
|
-
super_method: nil,
|
810
|
-
attributes: []
|
811
|
-
)
|
812
|
-
end
|
813
|
-
else
|
814
|
-
methods[name] = i.methods[name]
|
815
|
-
end
|
816
|
-
end
|
817
|
-
methods
|
818
|
-
else
|
819
|
-
i.methods
|
820
|
-
end
|
821
|
-
end
|
822
|
-
|
823
|
-
ivar_chains = interfaces.each.with_object({}) do |interface, chains|
|
824
|
-
chains.merge!(interface.ivar_chains)
|
825
|
-
end
|
826
|
-
|
827
|
-
Interface::Instantiated.new(type: type,
|
828
|
-
methods: methods,
|
829
|
-
ivar_chains: ivar_chains)
|
830
|
-
when AST::Types::Void
|
831
|
-
Interface::Instantiated.new(type: type,
|
832
|
-
methods: {},
|
833
|
-
ivar_chains: {})
|
834
|
-
|
835
|
-
when AST::Types::Tuple
|
836
|
-
yield_self do
|
837
|
-
element_type = AST::Types::Union.build(types: type.types)
|
838
|
-
array_type = AST::Builtin::Array.instance_type(element_type)
|
839
|
-
array_interface = resolve(array_type, self_type: self_type, with_private: with_private)
|
840
|
-
|
841
|
-
array_interface.methods[:[]] = array_interface.methods[:[]].yield_self do |aref|
|
842
|
-
types = type.types.map.with_index {|elem_type, index|
|
843
|
-
Interface::MethodType.new(
|
844
|
-
type_params: [],
|
845
|
-
params: Interface::Params.new(required: [AST::Types::Literal.new(value: index)],
|
846
|
-
optional: [],
|
847
|
-
rest: nil,
|
848
|
-
required_keywords: {},
|
849
|
-
optional_keywords: {},
|
850
|
-
rest_keywords: nil),
|
851
|
-
block: nil,
|
852
|
-
return_type: elem_type,
|
853
|
-
location: nil
|
854
|
-
)
|
855
|
-
} + aref.types
|
856
|
-
aref.with_types(types)
|
857
|
-
end
|
858
|
-
|
859
|
-
array_interface.methods[:[]=] = array_interface.methods[:[]=].yield_self do |aref|
|
860
|
-
types = type.types.map.with_index {|elem_type, index|
|
861
|
-
Interface::MethodType.new(
|
862
|
-
type_params: [],
|
863
|
-
params: Interface::Params.new(required: [AST::Types::Literal.new(value: index), elem_type],
|
864
|
-
optional: [],
|
865
|
-
rest: nil,
|
866
|
-
required_keywords: {},
|
867
|
-
optional_keywords: {},
|
868
|
-
rest_keywords: nil),
|
869
|
-
block: nil,
|
870
|
-
return_type: elem_type,
|
871
|
-
location: nil
|
872
|
-
)
|
873
|
-
} + aref.types
|
874
|
-
aref.with_types(types)
|
875
|
-
end
|
876
|
-
|
877
|
-
array_interface
|
878
|
-
end
|
879
|
-
|
880
|
-
when AST::Types::Record
|
881
|
-
yield_self do
|
882
|
-
key_type = AST::Types::Union.build(types: type.elements.keys.map {|val| AST::Types::Literal.new(value: val) })
|
883
|
-
value_type = AST::Types::Union.build(types: type.elements.values)
|
884
|
-
hash_interface = resolve(AST::Builtin::Hash.instance_type(key_type, value_type), self_type: self_type, with_private: with_private)
|
885
|
-
|
886
|
-
hash_interface.methods[:[]] = hash_interface.methods[:[]].yield_self do |ref|
|
887
|
-
types = type.elements.map do |key, value_type|
|
888
|
-
Interface::MethodType.new(
|
889
|
-
type_params: [],
|
890
|
-
params: Interface::Params.new(required: [AST::Types::Literal.new(value: key)],
|
891
|
-
optional: [],
|
892
|
-
rest: nil,
|
893
|
-
required_keywords: {},
|
894
|
-
optional_keywords: {},
|
895
|
-
rest_keywords: nil),
|
896
|
-
return_type: value_type,
|
897
|
-
block: nil,
|
898
|
-
location: nil
|
899
|
-
)
|
900
|
-
end
|
901
|
-
|
902
|
-
ref.with_types(types + ref.types)
|
903
|
-
end
|
904
|
-
|
905
|
-
hash_interface.methods[:[]=] = hash_interface.methods[:[]=].yield_self do |method|
|
906
|
-
types = type.elements.map do |key, value_type|
|
907
|
-
Interface::MethodType.new(
|
908
|
-
type_params: [],
|
909
|
-
params: Interface::Params.new(required: [AST::Types::Literal.new(value: key), value_type],
|
910
|
-
optional: [],
|
911
|
-
rest: nil,
|
912
|
-
required_keywords: {},
|
913
|
-
optional_keywords: {},
|
914
|
-
rest_keywords: nil),
|
915
|
-
return_type: value_type,
|
916
|
-
block: nil,
|
917
|
-
location: nil
|
918
|
-
)
|
919
|
-
end
|
920
|
-
|
921
|
-
method.with_types(types + method.types)
|
922
|
-
end
|
923
|
-
|
924
|
-
hash_interface
|
925
|
-
end
|
926
|
-
|
927
|
-
when AST::Types::Proc
|
928
|
-
yield_self do
|
929
|
-
proc_interface = resolve(type.back_type, self_type: self_type, with_private: with_private)
|
930
|
-
apply_type = Interface::MethodType.new(
|
931
|
-
type_params: [],
|
932
|
-
params: type.params,
|
933
|
-
block: nil,
|
934
|
-
return_type: type.return_type,
|
935
|
-
location: nil
|
936
|
-
)
|
937
|
-
|
938
|
-
proc_interface.methods[:[]] = proc_interface.methods[:[]].yield_self do |aref|
|
939
|
-
aref.with_types([apply_type])
|
940
|
-
end
|
941
|
-
proc_interface.methods[:call] = proc_interface.methods[:call].yield_self do |aref|
|
942
|
-
aref.with_types([apply_type])
|
943
|
-
end
|
944
|
-
|
945
|
-
proc_interface
|
946
|
-
end
|
947
|
-
|
948
|
-
end
|
949
|
-
end
|
950
|
-
|
951
|
-
def expand_alias(type)
|
952
|
-
expanded = case type
|
953
|
-
when AST::Types::Union
|
954
|
-
AST::Types::Union.build(
|
955
|
-
types: type.types.map {|ty| expand_alias(ty) },
|
956
|
-
location: type.location
|
957
|
-
)
|
958
|
-
when AST::Types::Intersection
|
959
|
-
AST::Types::Intersection.build(
|
960
|
-
types: type.types.map {|ty| expand_alias(ty) },
|
961
|
-
location: type.location
|
962
|
-
)
|
963
|
-
when AST::Types::Name::Alias
|
964
|
-
alias_sig = builder.signatures.find_alias(type.name, namespace: AST::Namespace.root)
|
965
|
-
expanded_alias = builder.absolute_type(alias_sig.type, current: alias_sig.name.namespace)
|
966
|
-
args = type.args.map {|ty| expand_alias(ty) }
|
967
|
-
s = Interface::Substitution.build(alias_sig.params&.variables || [], args)
|
968
|
-
expand_alias(expanded_alias.subst(s))
|
969
|
-
else
|
970
|
-
type
|
971
|
-
end
|
972
|
-
|
973
|
-
if block_given?
|
974
|
-
yield expanded
|
975
|
-
else
|
976
|
-
expanded
|
977
|
-
end
|
700
|
+
def expand_alias(type, &block)
|
701
|
+
factory.expand_alias(type, &block)
|
978
702
|
end
|
979
703
|
end
|
980
704
|
end
|