steep 0.12.0 → 0.13.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 +4 -0
- data/README.md +8 -0
- data/lib/steep.rb +2 -0
- data/lib/steep/drivers/langserver.rb +61 -10
- data/lib/steep/interface/substitution.rb +13 -1
- data/lib/steep/project.rb +1 -0
- data/lib/steep/project/file.rb +18 -13
- data/lib/steep/project/hover_content.rb +128 -0
- data/lib/steep/source.rb +5 -7
- data/lib/steep/type_construction.rb +161 -216
- data/lib/steep/type_inference/context.rb +107 -0
- data/lib/steep/typing.rb +25 -15
- data/lib/steep/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e95857ced793435c07c948b2a51ac914c52816811ae8e5d18337bcf946eb33d
|
4
|
+
data.tar.gz: 9038e63999222be3e0b7172aa423056ef7792b905e1d277271fa795b6ca06889
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2747b3e5f3303ae48a91e76b2256cd1e402e7022caa714083267da0820f1bce2d81188586e551a10158d4c6716ab7a8db8562aa84b31c98731872b08d05048c9
|
7
|
+
data.tar.gz: 9d5bd44685d8228ee304bdaf94ae52e5eef2a2871580e7190ef941ea510406eab28bdb3aa6849fd9234649da10cd9344e2c38ba309a11a73cca70c6ed7e946e3
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -204,6 +204,14 @@ Generally, these are by our design.
|
|
204
204
|
|
205
205
|
You can find examples in `smoke` directory.
|
206
206
|
|
207
|
+
## IDEs
|
208
|
+
|
209
|
+
Steep implements some of the Language Server Protocol features. You can use Steep with VSCode and its plugin.
|
210
|
+
|
211
|
+
- https://github.com/soutaro/steep-vscode
|
212
|
+
|
213
|
+
Other LSP supporting tools may work with Steep where it starts the server as `steep langserver`.
|
214
|
+
|
207
215
|
## Development
|
208
216
|
|
209
217
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/steep.rb
CHANGED
@@ -60,6 +60,7 @@ require "steep/annotation_parser"
|
|
60
60
|
require "steep/typing"
|
61
61
|
require "steep/errors"
|
62
62
|
require "steep/type_construction"
|
63
|
+
require "steep/type_inference/context"
|
63
64
|
require "steep/type_inference/send_args"
|
64
65
|
require "steep/type_inference/block_params"
|
65
66
|
require "steep/type_inference/constant_env"
|
@@ -72,6 +73,7 @@ require "steep/project/options"
|
|
72
73
|
require "steep/project/target"
|
73
74
|
require "steep/project/dsl"
|
74
75
|
require "steep/project/file_loader"
|
76
|
+
require "steep/project/hover_content"
|
75
77
|
require "steep/drivers/utils/driver_helper"
|
76
78
|
require "steep/drivers/check"
|
77
79
|
require "steep/drivers/validate"
|
@@ -187,6 +187,8 @@ module Steep
|
|
187
187
|
[diagnostics_raw(source.status.error.message, source.status.location)]
|
188
188
|
when Project::SourceFile::ParseErrorStatus
|
189
189
|
[]
|
190
|
+
when Project::SourceFile::TypeCheckErrorStatus
|
191
|
+
[]
|
190
192
|
end
|
191
193
|
|
192
194
|
report_diagnostics source.path, diagnostics
|
@@ -264,22 +266,71 @@ module Steep
|
|
264
266
|
def response_to_hover(path:, line:, column:)
|
265
267
|
Steep.logger.info { "path=#{path}, line=#{line}, column=#{column}" }
|
266
268
|
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
Steep.logger.warn { "range = #{range.inspect}" }
|
269
|
+
hover = Project::HoverContent.new(project: project)
|
270
|
+
content = hover.content_for(path: path, line: line+1, column: column+1)
|
271
|
+
if content
|
272
|
+
range = content.location.yield_self do |location|
|
273
|
+
start_position = { line: location.line - 1, character: location.column }
|
274
|
+
end_position = { line: location.last_line - 1, character: location.last_column }
|
275
|
+
{ start: start_position, end: end_position }
|
276
|
+
end
|
276
277
|
|
277
278
|
LanguageServer::Protocol::Interface::Hover.new(
|
278
|
-
contents: { kind: "markdown", value:
|
279
|
+
contents: { kind: "markdown", value: format_hover(content) },
|
279
280
|
range: range
|
280
281
|
)
|
281
282
|
end
|
282
283
|
end
|
284
|
+
|
285
|
+
def format_hover(content)
|
286
|
+
case content
|
287
|
+
when Project::HoverContent::VariableContent
|
288
|
+
"`#{content.name}`: `#{content.type.to_s}`"
|
289
|
+
when Project::HoverContent::MethodCallContent
|
290
|
+
method_name = case content.method_name
|
291
|
+
when Project::HoverContent::InstanceMethodName
|
292
|
+
"#{content.method_name.class_name}##{content.method_name.method_name}"
|
293
|
+
when Project::HoverContent::SingletonMethodName
|
294
|
+
"#{content.method_name.class_name}.#{content.method_name.method_name}"
|
295
|
+
else
|
296
|
+
nil
|
297
|
+
end
|
298
|
+
|
299
|
+
if method_name
|
300
|
+
string = <<HOVER
|
301
|
+
```
|
302
|
+
#{method_name} ~> #{content.type}
|
303
|
+
```
|
304
|
+
HOVER
|
305
|
+
if content.definition
|
306
|
+
if content.definition.comment
|
307
|
+
string << "\n----\n\n#{content.definition.comment.string}"
|
308
|
+
end
|
309
|
+
|
310
|
+
string << "\n----\n\n#{content.definition.method_types.map {|x| "- `#{x}`\n" }.join()}"
|
311
|
+
end
|
312
|
+
else
|
313
|
+
"`#{content.type}`"
|
314
|
+
end
|
315
|
+
when Project::HoverContent::DefinitionContent
|
316
|
+
string = <<HOVER
|
317
|
+
```
|
318
|
+
def #{content.method_name}: #{content.method_type}
|
319
|
+
```
|
320
|
+
HOVER
|
321
|
+
if (comment = content.definition.comment)
|
322
|
+
string << "\n----\n\n#{comment.string}\n"
|
323
|
+
end
|
324
|
+
|
325
|
+
if content.definition.method_types.size > 1
|
326
|
+
string << "\n----\n\n#{content.definition.method_types.map {|x| "- `#{x}`\n" }.join()}"
|
327
|
+
end
|
328
|
+
|
329
|
+
string
|
330
|
+
when Project::HoverContent::TypeContent
|
331
|
+
"`#{content.type}`"
|
332
|
+
end
|
333
|
+
end
|
283
334
|
end
|
284
335
|
end
|
285
336
|
end
|
@@ -1,6 +1,18 @@
|
|
1
1
|
module Steep
|
2
2
|
module Interface
|
3
3
|
class Substitution
|
4
|
+
class InvalidSubstitutionError < StandardError
|
5
|
+
attr_reader :vars_size
|
6
|
+
attr_reader :types_size
|
7
|
+
|
8
|
+
def initialize(vars_size:, types_size:)
|
9
|
+
@var_size = vars_size
|
10
|
+
@types_size = types_size
|
11
|
+
|
12
|
+
super "Invalid substitution: vars.size=#{vars_size}, types.size=#{types_size}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
4
16
|
attr_reader :dictionary
|
5
17
|
attr_reader :instance_type
|
6
18
|
attr_reader :module_type
|
@@ -42,7 +54,7 @@ module Steep
|
|
42
54
|
def self.build(vars, types = nil, instance_type: AST::Types::Instance.new, module_type: AST::Types::Class.new, self_type: AST::Types::Self.new)
|
43
55
|
types ||= vars.map {|var| AST::Types::Var.fresh(var) }
|
44
56
|
|
45
|
-
raise
|
57
|
+
raise InvalidSubstitutionError.new(vars_size: vars.size, types_size: types.size) unless vars.size == types.size
|
46
58
|
|
47
59
|
dic = vars.zip(types).each.with_object({}) do |(var, type), d|
|
48
60
|
d[var] = type
|
data/lib/steep/project.rb
CHANGED
data/lib/steep/project/file.rb
CHANGED
@@ -11,6 +11,7 @@ module Steep
|
|
11
11
|
ParseErrorStatus = Struct.new(:error, keyword_init: true)
|
12
12
|
AnnotationSyntaxErrorStatus = Struct.new(:error, :location, keyword_init: true)
|
13
13
|
TypeCheckStatus = Struct.new(:typing, :source, :timestamp, keyword_init: true)
|
14
|
+
TypeCheckErrorStatus = Struct.new(:error, keyword_init: true)
|
14
15
|
|
15
16
|
def initialize(path:)
|
16
17
|
@path = path
|
@@ -62,20 +63,22 @@ module Steep
|
|
62
63
|
checker: subtyping,
|
63
64
|
annotations: annotations,
|
64
65
|
source: source,
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
66
|
+
context: TypeInference::Context.new(
|
67
|
+
block_context: nil,
|
68
|
+
module_context: TypeInference::Context::ModuleContext.new(
|
69
|
+
instance_type: nil,
|
70
|
+
module_type: nil,
|
71
|
+
implement_name: nil,
|
72
|
+
current_namespace: AST::Namespace.root,
|
73
|
+
const_env: const_env,
|
74
|
+
class_name: nil
|
75
|
+
),
|
76
|
+
method_context: nil,
|
77
|
+
break_context: nil,
|
78
|
+
self_type: AST::Builtin::Object.instance_type,
|
79
|
+
type_env: type_env
|
74
80
|
),
|
75
|
-
|
76
|
-
typing: typing,
|
77
|
-
break_context: nil,
|
78
|
-
type_env: type_env
|
81
|
+
typing: typing
|
79
82
|
)
|
80
83
|
|
81
84
|
construction.synthesize(source.node)
|
@@ -86,6 +89,8 @@ module Steep
|
|
86
89
|
source: source,
|
87
90
|
timestamp: Time.now
|
88
91
|
)
|
92
|
+
rescue => exn
|
93
|
+
@status = TypeCheckErrorStatus.new(error: exn)
|
89
94
|
end
|
90
95
|
|
91
96
|
true
|
@@ -0,0 +1,128 @@
|
|
1
|
+
module Steep
|
2
|
+
class Project
|
3
|
+
class HoverContent
|
4
|
+
TypeContent = Struct.new(:node, :type, :location, keyword_init: true)
|
5
|
+
VariableContent = Struct.new(:node, :name, :type, :location, keyword_init: true)
|
6
|
+
MethodCallContent = Struct.new(:node, :method_name, :type, :definition, :location, keyword_init: true)
|
7
|
+
DefinitionContent = Struct.new(:node, :method_name, :method_type, :definition, :location, keyword_init: true)
|
8
|
+
|
9
|
+
InstanceMethodName = Struct.new(:class_name, :method_name)
|
10
|
+
SingletonMethodName = Struct.new(:class_name, :method_name)
|
11
|
+
|
12
|
+
attr_reader :project
|
13
|
+
|
14
|
+
def initialize(project:)
|
15
|
+
@project = project
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_definition_for(factory, module_name, singleton_method: nil, instance_method: nil)
|
19
|
+
type_name = factory.type_name_1(module_name)
|
20
|
+
|
21
|
+
case
|
22
|
+
when instance_method
|
23
|
+
factory.definition_builder.build_instance(type_name).methods[instance_method]
|
24
|
+
when singleton_method
|
25
|
+
methods = factory.definition_builder.build_singleton(type_name).methods
|
26
|
+
|
27
|
+
if singleton_method == :new
|
28
|
+
methods[:new] || methods[:initialize]
|
29
|
+
else
|
30
|
+
methods[singleton_method]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def content_for(path:, line:, column:)
|
36
|
+
source_file = project.targets.map {|target| target.source_files[path] }.compact[0]
|
37
|
+
|
38
|
+
if source_file
|
39
|
+
case (status = source_file.status)
|
40
|
+
when SourceFile::TypeCheckStatus
|
41
|
+
node, *parents = status.source.find_nodes(line: line, column: column)
|
42
|
+
|
43
|
+
if node
|
44
|
+
case node.type
|
45
|
+
when :lvar, :lvasgn
|
46
|
+
var_name = node.children[0]
|
47
|
+
context = status.typing.context_of(node: node)
|
48
|
+
var_type = context.type_env.get(lvar: var_name.name)
|
49
|
+
|
50
|
+
VariableContent.new(node: node, name: var_name.name, type: var_type, location: node.location.name)
|
51
|
+
when :send
|
52
|
+
receiver, method_name, *_ = node.children
|
53
|
+
|
54
|
+
|
55
|
+
result_node = if parents[0]&.type == :block
|
56
|
+
parents[0]
|
57
|
+
else
|
58
|
+
node
|
59
|
+
end
|
60
|
+
|
61
|
+
context = status.typing.context_of(node: result_node)
|
62
|
+
|
63
|
+
receiver_type = if receiver
|
64
|
+
status.typing.type_of(node: receiver)
|
65
|
+
else
|
66
|
+
context.self_type
|
67
|
+
end
|
68
|
+
|
69
|
+
factory = context.type_env.subtyping.factory
|
70
|
+
method_name, definition = case receiver_type
|
71
|
+
when AST::Types::Name::Instance
|
72
|
+
method_definition = method_definition_for(factory, receiver_type.name, instance_method: method_name)
|
73
|
+
if method_definition&.defined_in
|
74
|
+
owner_name = factory.type_name(method_definition.defined_in.name.absolute!)
|
75
|
+
[
|
76
|
+
InstanceMethodName.new(owner_name, method_name),
|
77
|
+
method_definition
|
78
|
+
]
|
79
|
+
end
|
80
|
+
when AST::Types::Name::Class
|
81
|
+
method_definition = method_definition_for(factory, receiver_type.name, singleton_method: method_name)
|
82
|
+
if method_definition&.defined_in
|
83
|
+
owner_name = factory.type_name(method_definition.defined_in.name.absolute!)
|
84
|
+
[
|
85
|
+
SingletonMethodName.new(owner_name, method_name),
|
86
|
+
method_definition
|
87
|
+
]
|
88
|
+
end
|
89
|
+
else
|
90
|
+
nil
|
91
|
+
end
|
92
|
+
|
93
|
+
MethodCallContent.new(
|
94
|
+
node: node,
|
95
|
+
method_name: method_name,
|
96
|
+
type: status.typing.type_of(node: result_node),
|
97
|
+
definition: definition,
|
98
|
+
location: result_node.location.expression
|
99
|
+
)
|
100
|
+
when :def, :defs
|
101
|
+
context = status.typing.context_of(node: node)
|
102
|
+
method_context = context.method_context
|
103
|
+
|
104
|
+
if method_context
|
105
|
+
DefinitionContent.new(
|
106
|
+
node: node,
|
107
|
+
method_name: method_context.name,
|
108
|
+
method_type: method_context.method_type,
|
109
|
+
definition: method_context.method,
|
110
|
+
location: node.loc.expression
|
111
|
+
)
|
112
|
+
end
|
113
|
+
else
|
114
|
+
type = status.typing.type_of(node: node)
|
115
|
+
|
116
|
+
TypeContent.new(
|
117
|
+
node: node,
|
118
|
+
type: type,
|
119
|
+
location: node.location.expression
|
120
|
+
)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
data/lib/steep/source.rb
CHANGED
@@ -294,8 +294,7 @@ module Steep
|
|
294
294
|
end
|
295
295
|
end
|
296
296
|
|
297
|
-
|
298
|
-
def find_node(line:, column:, node: self.node, position: nil)
|
297
|
+
def find_nodes(line:, column:, node: self.node, position: nil, parents: [])
|
299
298
|
position ||= (line-1).times.sum do |i|
|
300
299
|
node.location.expression.source_buffer.source_line(i+1).size + 1
|
301
300
|
end + column
|
@@ -306,14 +305,13 @@ module Steep
|
|
306
305
|
|
307
306
|
if range
|
308
307
|
if range === position
|
308
|
+
parents.unshift node
|
309
|
+
|
309
310
|
Source.each_child_node(node) do |child|
|
310
|
-
|
311
|
-
if n
|
312
|
-
return n
|
313
|
-
end
|
311
|
+
ns = find_nodes(line: line, column: column, node: child, position: position, parents: parents) and return ns
|
314
312
|
end
|
315
313
|
|
316
|
-
|
314
|
+
parents
|
317
315
|
end
|
318
316
|
end
|
319
317
|
end
|
@@ -1,97 +1,43 @@
|
|
1
1
|
module Steep
|
2
2
|
class TypeConstruction
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
attr_reader :constructor
|
9
|
-
attr_reader :super_method
|
10
|
-
|
11
|
-
def initialize(name:, method:, method_type:, return_type:, constructor:, super_method:)
|
12
|
-
@name = name
|
13
|
-
@method = method
|
14
|
-
@return_type = return_type
|
15
|
-
@method_type = method_type
|
16
|
-
@constructor = constructor
|
17
|
-
@super_method = super_method
|
18
|
-
end
|
19
|
-
|
20
|
-
def block_type
|
21
|
-
method_type&.block
|
22
|
-
end
|
23
|
-
end
|
3
|
+
attr_reader :checker
|
4
|
+
attr_reader :source
|
5
|
+
attr_reader :annotations
|
6
|
+
attr_reader :typing
|
7
|
+
attr_reader :type_env
|
24
8
|
|
25
|
-
|
26
|
-
attr_reader :body_type
|
9
|
+
attr_reader :context
|
27
10
|
|
28
|
-
|
29
|
-
|
30
|
-
end
|
11
|
+
def module_context
|
12
|
+
context.module_context
|
31
13
|
end
|
32
14
|
|
33
|
-
|
34
|
-
|
35
|
-
|
15
|
+
def method_context
|
16
|
+
context.method_context
|
17
|
+
end
|
36
18
|
|
37
|
-
|
38
|
-
|
39
|
-
@next_type = next_type
|
40
|
-
end
|
19
|
+
def block_context
|
20
|
+
context.block_context
|
41
21
|
end
|
42
22
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
attr_reader :defined_instance_methods
|
47
|
-
attr_reader :defined_module_methods
|
48
|
-
attr_reader :const_env
|
49
|
-
attr_reader :implement_name
|
50
|
-
attr_reader :current_namespace
|
51
|
-
attr_reader :class_name
|
52
|
-
attr_reader :instance_definition
|
53
|
-
attr_reader :module_definition
|
54
|
-
|
55
|
-
def initialize(instance_type:, module_type:, implement_name:, current_namespace:, const_env:, class_name:, instance_definition: nil, module_definition: nil)
|
56
|
-
@instance_type = instance_type
|
57
|
-
@module_type = module_type
|
58
|
-
@defined_instance_methods = Set.new
|
59
|
-
@defined_module_methods = Set.new
|
60
|
-
@implement_name = implement_name
|
61
|
-
@current_namespace = current_namespace
|
62
|
-
@const_env = const_env
|
63
|
-
@class_name = class_name
|
64
|
-
@instance_definition = instance_definition
|
65
|
-
@module_definition = module_definition
|
66
|
-
end
|
23
|
+
def break_context
|
24
|
+
context.break_context
|
25
|
+
end
|
67
26
|
|
68
|
-
|
69
|
-
|
70
|
-
end
|
27
|
+
def self_type
|
28
|
+
context.self_type
|
71
29
|
end
|
72
30
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
attr_reader :typing
|
77
|
-
attr_reader :method_context
|
78
|
-
attr_reader :block_context
|
79
|
-
attr_reader :module_context
|
80
|
-
attr_reader :self_type
|
81
|
-
attr_reader :break_context
|
82
|
-
attr_reader :type_env
|
31
|
+
def type_env
|
32
|
+
context.type_env
|
33
|
+
end
|
83
34
|
|
84
|
-
def initialize(checker:, source:, annotations:,
|
35
|
+
def initialize(checker:, source:, annotations:, typing:, context:)
|
85
36
|
@checker = checker
|
86
37
|
@source = source
|
87
38
|
@annotations = annotations
|
88
39
|
@typing = typing
|
89
|
-
@
|
90
|
-
@block_context = block_context
|
91
|
-
@method_context = method_context
|
92
|
-
@module_context = module_context
|
93
|
-
@break_context = break_context
|
94
|
-
@type_env = type_env
|
40
|
+
@context = context
|
95
41
|
end
|
96
42
|
|
97
43
|
def with_new_typing(typing)
|
@@ -99,13 +45,8 @@ module Steep
|
|
99
45
|
checker: checker,
|
100
46
|
source: source,
|
101
47
|
annotations: annotations,
|
102
|
-
type_env: type_env,
|
103
48
|
typing: typing,
|
104
|
-
|
105
|
-
method_context: method_context,
|
106
|
-
block_context: block_context,
|
107
|
-
module_context: module_context,
|
108
|
-
break_context: break_context
|
49
|
+
context: context
|
109
50
|
)
|
110
51
|
end
|
111
52
|
|
@@ -173,7 +114,7 @@ module Steep
|
|
173
114
|
end
|
174
115
|
end
|
175
116
|
|
176
|
-
method_context = MethodContext.new(
|
117
|
+
method_context = TypeInference::Context::MethodContext.new(
|
177
118
|
name: method_name,
|
178
119
|
method: definition && definition.methods[method_name],
|
179
120
|
method_type: method_type,
|
@@ -205,13 +146,15 @@ module Steep
|
|
205
146
|
checker: checker,
|
206
147
|
source: source,
|
207
148
|
annotations: annots,
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
149
|
+
context: TypeInference::Context.new(
|
150
|
+
method_context: method_context,
|
151
|
+
module_context: module_context,
|
152
|
+
block_context: nil,
|
153
|
+
break_context: nil,
|
154
|
+
self_type: annots.self_type || self_type,
|
155
|
+
type_env: type_env
|
156
|
+
),
|
212
157
|
typing: typing,
|
213
|
-
module_context: module_context,
|
214
|
-
break_context: nil
|
215
158
|
)
|
216
159
|
end
|
217
160
|
|
@@ -283,7 +226,7 @@ module Steep
|
|
283
226
|
end
|
284
227
|
module_const_env = TypeInference::ConstantEnv.new(factory: checker.factory, context: const_context)
|
285
228
|
|
286
|
-
module_context_ = ModuleContext.new(
|
229
|
+
module_context_ = TypeInference::Context::ModuleContext.new(
|
287
230
|
instance_type: instance_type,
|
288
231
|
module_type: annots.self_type || module_type,
|
289
232
|
implement_name: implement_module_name,
|
@@ -303,13 +246,15 @@ module Steep
|
|
303
246
|
checker: checker,
|
304
247
|
source: source,
|
305
248
|
annotations: annots,
|
306
|
-
type_env: module_type_env,
|
307
249
|
typing: typing,
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
250
|
+
context: TypeInference::Context.new(
|
251
|
+
method_context: nil,
|
252
|
+
block_context: nil,
|
253
|
+
break_context: nil,
|
254
|
+
module_context: module_context_,
|
255
|
+
self_type: module_context_.module_type,
|
256
|
+
type_env: module_type_env
|
257
|
+
)
|
313
258
|
)
|
314
259
|
end
|
315
260
|
|
@@ -380,7 +325,7 @@ module Steep
|
|
380
325
|
end
|
381
326
|
class_const_env = TypeInference::ConstantEnv.new(factory: checker.factory, context: const_context)
|
382
327
|
|
383
|
-
module_context = ModuleContext.new(
|
328
|
+
module_context = TypeInference::Context::ModuleContext.new(
|
384
329
|
instance_type: annots.instance_type || instance_type,
|
385
330
|
module_type: annots.self_type || annots.module_type || module_type,
|
386
331
|
implement_name: implement_module_name,
|
@@ -401,21 +346,21 @@ module Steep
|
|
401
346
|
checker: checker,
|
402
347
|
source: source,
|
403
348
|
annotations: annots,
|
404
|
-
type_env: class_type_env,
|
405
349
|
typing: typing,
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
350
|
+
context: TypeInference::Context.new(
|
351
|
+
method_context: nil,
|
352
|
+
block_context: nil,
|
353
|
+
module_context: module_context,
|
354
|
+
break_context: nil,
|
355
|
+
self_type: module_context.module_type,
|
356
|
+
type_env: class_type_env
|
357
|
+
)
|
411
358
|
)
|
412
359
|
end
|
413
360
|
|
414
|
-
def for_branch(node, truthy_vars: Set.new, type_case_override: nil)
|
361
|
+
def for_branch(node, truthy_vars: Set.new, type_case_override: nil, break_context: context.break_context)
|
415
362
|
annots = source.annotations(block: node, factory: checker.factory, current_module: current_namespace)
|
416
363
|
|
417
|
-
type_env = self.type_env
|
418
|
-
|
419
364
|
lvar_types = self.type_env.lvar_types.each.with_object({}) do |(var, type), env|
|
420
365
|
if truthy_vars.member?(var)
|
421
366
|
env[var] = unwrap(type)
|
@@ -423,7 +368,8 @@ module Steep
|
|
423
368
|
env[var] = type
|
424
369
|
end
|
425
370
|
end
|
426
|
-
|
371
|
+
|
372
|
+
type_env = self.type_env.with_annotations(lvar_types: lvar_types, self_type: self_type) do |var, relation, result|
|
427
373
|
raise "Unexpected annotate failure: #{relation}"
|
428
374
|
end
|
429
375
|
|
@@ -453,23 +399,18 @@ module Steep
|
|
453
399
|
)
|
454
400
|
end
|
455
401
|
|
456
|
-
with(type_env: type_env)
|
402
|
+
with(context: context.with(type_env: type_env, break_context: break_context))
|
457
403
|
end
|
458
404
|
|
459
405
|
NOTHING = ::Object.new
|
460
406
|
|
461
|
-
def with(annotations: NOTHING,
|
407
|
+
def with(annotations: NOTHING, context: NOTHING)
|
462
408
|
self.class.new(
|
463
409
|
checker: checker,
|
464
410
|
source: source,
|
465
411
|
annotations: annotations.equal?(NOTHING) ? self.annotations : annotations,
|
466
|
-
type_env: type_env.equal?(NOTHING) ? self.type_env : type_env,
|
467
412
|
typing: typing,
|
468
|
-
|
469
|
-
block_context: block_context.equal?(NOTHING) ? self.block_context : block_context,
|
470
|
-
module_context: module_context.equal?(NOTHING) ? self.module_context : module_context,
|
471
|
-
self_type: self_type.equal?(NOTHING) ? self.self_type : self_type,
|
472
|
-
break_context: break_context.equal?(NOTHING) ? self.break_context : break_context
|
413
|
+
context: context.equal?(NOTHING) ? self.context : context
|
473
414
|
)
|
474
415
|
end
|
475
416
|
|
@@ -489,7 +430,7 @@ module Steep
|
|
489
430
|
type = AST::Builtin.nil_type
|
490
431
|
end
|
491
432
|
|
492
|
-
typing.add_typing(node, type)
|
433
|
+
typing.add_typing(node, type, context)
|
493
434
|
end
|
494
435
|
|
495
436
|
when :lvasgn
|
@@ -500,9 +441,9 @@ module Steep
|
|
500
441
|
case var.name
|
501
442
|
when :_, :__any__
|
502
443
|
synthesize(rhs, hint: AST::Builtin.any_type)
|
503
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
444
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
504
445
|
when :__skip__
|
505
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
446
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
506
447
|
else
|
507
448
|
type_assignment(var, rhs, node, hint: hint)
|
508
449
|
end
|
@@ -515,7 +456,7 @@ module Steep
|
|
515
456
|
fallback_to_any node
|
516
457
|
end
|
517
458
|
|
518
|
-
typing.add_typing node, type
|
459
|
+
typing.add_typing node, type, context
|
519
460
|
end
|
520
461
|
|
521
462
|
when :ivasgn
|
@@ -530,7 +471,7 @@ module Steep
|
|
530
471
|
type = type_env.get(ivar: name) do
|
531
472
|
fallback_to_any node
|
532
473
|
end
|
533
|
-
typing.add_typing(node, type)
|
474
|
+
typing.add_typing(node, type, context)
|
534
475
|
end
|
535
476
|
|
536
477
|
when :send
|
@@ -543,7 +484,7 @@ module Steep
|
|
543
484
|
module_type
|
544
485
|
end
|
545
486
|
|
546
|
-
typing.add_typing(node, type)
|
487
|
+
typing.add_typing(node, type, context)
|
547
488
|
else
|
548
489
|
type_send(node, send_node: node, block_params: nil, block_body: nil)
|
549
490
|
end
|
@@ -558,7 +499,7 @@ module Steep
|
|
558
499
|
else
|
559
500
|
module_type
|
560
501
|
end
|
561
|
-
typing.add_typing(node, type)
|
502
|
+
typing.add_typing(node, type, context)
|
562
503
|
else
|
563
504
|
type_send(node, send_node: node, block_params: nil, block_body: nil, unwrap: true)
|
564
505
|
end
|
@@ -570,7 +511,7 @@ module Steep
|
|
570
511
|
each_child_node(node) do |child|
|
571
512
|
synthesize(child)
|
572
513
|
end
|
573
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
514
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
574
515
|
|
575
516
|
when :op_asgn
|
576
517
|
yield_self do
|
@@ -594,7 +535,7 @@ module Steep
|
|
594
535
|
|
595
536
|
case
|
596
537
|
when lhs_type == AST::Builtin.any_type
|
597
|
-
typing.add_typing(node, lhs_type)
|
538
|
+
typing.add_typing(node, lhs_type, context)
|
598
539
|
when !lhs_type
|
599
540
|
fallback_to_any(node)
|
600
541
|
else
|
@@ -627,7 +568,7 @@ module Steep
|
|
627
568
|
typing.add_error Errors::NoMethod.new(node: node, method: op, type: expand_self(lhs_type))
|
628
569
|
end
|
629
570
|
|
630
|
-
typing.add_typing(node, lhs_type)
|
571
|
+
typing.add_typing(node, lhs_type, context)
|
631
572
|
end
|
632
573
|
end
|
633
574
|
|
@@ -656,7 +597,7 @@ module Steep
|
|
656
597
|
block_body: nil,
|
657
598
|
topdown_hint: true)
|
658
599
|
|
659
|
-
typing.add_typing node, return_type
|
600
|
+
typing.add_typing node, return_type, context
|
660
601
|
else
|
661
602
|
fallback_to_any node do
|
662
603
|
Errors::UnexpectedSuper.new(node: node, method: method_context.name)
|
@@ -717,7 +658,7 @@ module Steep
|
|
717
658
|
module_context.defined_instance_methods << node.children[0]
|
718
659
|
end
|
719
660
|
|
720
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
661
|
+
typing.add_typing(node, AST::Builtin.any_type, new.context)
|
721
662
|
|
722
663
|
when :defs
|
723
664
|
synthesize(node.children[0]).tap do |self_type|
|
@@ -762,7 +703,7 @@ module Steep
|
|
762
703
|
end
|
763
704
|
end
|
764
705
|
|
765
|
-
typing.add_typing(node, AST::Builtin::Symbol.instance_type)
|
706
|
+
typing.add_typing(node, AST::Builtin::Symbol.instance_type, context)
|
766
707
|
|
767
708
|
when :return
|
768
709
|
yield_self do
|
@@ -791,7 +732,7 @@ module Steep
|
|
791
732
|
end
|
792
733
|
end
|
793
734
|
|
794
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
735
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
795
736
|
end
|
796
737
|
|
797
738
|
when :break
|
@@ -817,7 +758,7 @@ module Steep
|
|
817
758
|
typing.add_error Errors::UnexpectedJump.new(node: node)
|
818
759
|
end
|
819
760
|
|
820
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
761
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
821
762
|
|
822
763
|
when :next
|
823
764
|
value = node.children[0]
|
@@ -842,13 +783,13 @@ module Steep
|
|
842
783
|
typing.add_error Errors::UnexpectedJump.new(node: node)
|
843
784
|
end
|
844
785
|
|
845
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
786
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
846
787
|
|
847
788
|
when :retry
|
848
789
|
unless break_context
|
849
790
|
typing.add_error Errors::UnexpectedJump.new(node: node)
|
850
791
|
end
|
851
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
792
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
852
793
|
|
853
794
|
when :arg, :kwarg, :procarg0
|
854
795
|
yield_self do
|
@@ -856,7 +797,7 @@ module Steep
|
|
856
797
|
type = type_env.get(lvar: var.name) do
|
857
798
|
fallback_to_any node
|
858
799
|
end
|
859
|
-
typing.add_typing(node, type)
|
800
|
+
typing.add_typing(node, type, context)
|
860
801
|
end
|
861
802
|
|
862
803
|
when :optarg, :kwoptarg
|
@@ -874,7 +815,7 @@ module Steep
|
|
874
815
|
AST::Builtin::Array.instance_type(AST::Builtin.any_type)
|
875
816
|
end
|
876
817
|
|
877
|
-
typing.add_typing(node, type)
|
818
|
+
typing.add_typing(node, type, context)
|
878
819
|
end
|
879
820
|
|
880
821
|
when :kwrestarg
|
@@ -885,23 +826,23 @@ module Steep
|
|
885
826
|
AST::Builtin::Hash.instance_type(AST::Builtin::Symbol.instance_type, AST::Builtin.any_type)
|
886
827
|
end
|
887
828
|
|
888
|
-
typing.add_typing(node, type)
|
829
|
+
typing.add_typing(node, type, context)
|
889
830
|
end
|
890
831
|
|
891
832
|
when :float
|
892
|
-
typing.add_typing(node, AST::Builtin::Float.instance_type)
|
833
|
+
typing.add_typing(node, AST::Builtin::Float.instance_type, context)
|
893
834
|
|
894
835
|
when :nil
|
895
|
-
typing.add_typing(node, AST::Builtin.nil_type)
|
836
|
+
typing.add_typing(node, AST::Builtin.nil_type, context)
|
896
837
|
|
897
838
|
when :int
|
898
839
|
yield_self do
|
899
840
|
literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_)}
|
900
841
|
|
901
842
|
if literal_type
|
902
|
-
typing.add_typing(node, literal_type)
|
843
|
+
typing.add_typing(node, literal_type, context)
|
903
844
|
else
|
904
|
-
typing.add_typing(node, AST::Builtin::Integer.instance_type)
|
845
|
+
typing.add_typing(node, AST::Builtin::Integer.instance_type, context)
|
905
846
|
end
|
906
847
|
end
|
907
848
|
|
@@ -910,9 +851,9 @@ module Steep
|
|
910
851
|
literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_)}
|
911
852
|
|
912
853
|
if literal_type
|
913
|
-
typing.add_typing(node, literal_type)
|
854
|
+
typing.add_typing(node, literal_type, context)
|
914
855
|
else
|
915
|
-
typing.add_typing(node, AST::Builtin::Symbol.instance_type)
|
856
|
+
typing.add_typing(node, AST::Builtin::Symbol.instance_type, context)
|
916
857
|
end
|
917
858
|
end
|
918
859
|
|
@@ -921,14 +862,14 @@ module Steep
|
|
921
862
|
literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_)}
|
922
863
|
|
923
864
|
if literal_type
|
924
|
-
typing.add_typing(node, literal_type)
|
865
|
+
typing.add_typing(node, literal_type, context)
|
925
866
|
else
|
926
|
-
typing.add_typing(node, AST::Builtin::String.instance_type)
|
867
|
+
typing.add_typing(node, AST::Builtin::String.instance_type, context)
|
927
868
|
end
|
928
869
|
end
|
929
870
|
|
930
871
|
when :true, :false
|
931
|
-
typing.add_typing(node, AST::Types::Boolean.new)
|
872
|
+
typing.add_typing(node, AST::Types::Boolean.new, context)
|
932
873
|
|
933
874
|
when :hash
|
934
875
|
yield_self do
|
@@ -975,7 +916,7 @@ module Steep
|
|
975
916
|
typing.add_error Errors::FallbackAny.new(node: node)
|
976
917
|
end
|
977
918
|
|
978
|
-
typing.add_typing(node, AST::Builtin::Hash.instance_type(key_type, value_type))
|
919
|
+
typing.add_typing(node, AST::Builtin::Hash.instance_type(key_type, value_type), context)
|
979
920
|
end
|
980
921
|
|
981
922
|
when :dstr, :xstr
|
@@ -983,14 +924,14 @@ module Steep
|
|
983
924
|
synthesize(child)
|
984
925
|
end
|
985
926
|
|
986
|
-
typing.add_typing(node, AST::Builtin::String.instance_type)
|
927
|
+
typing.add_typing(node, AST::Builtin::String.instance_type, context)
|
987
928
|
|
988
929
|
when :dsym
|
989
930
|
each_child_node(node) do |child|
|
990
931
|
synthesize(child)
|
991
932
|
end
|
992
933
|
|
993
|
-
typing.add_typing(node, AST::Builtin::Symbol.instance_type)
|
934
|
+
typing.add_typing(node, AST::Builtin::Symbol.instance_type, context)
|
994
935
|
|
995
936
|
when :class
|
996
937
|
yield_self do
|
@@ -1002,7 +943,7 @@ module Steep
|
|
1002
943
|
end
|
1003
944
|
end
|
1004
945
|
|
1005
|
-
typing.add_typing(node, AST::Builtin.nil_type)
|
946
|
+
typing.add_typing(node, AST::Builtin.nil_type, context)
|
1006
947
|
end
|
1007
948
|
|
1008
949
|
when :module
|
@@ -1015,11 +956,11 @@ module Steep
|
|
1015
956
|
end
|
1016
957
|
end
|
1017
958
|
|
1018
|
-
typing.add_typing(node, AST::Builtin.nil_type)
|
959
|
+
typing.add_typing(node, AST::Builtin.nil_type, context)
|
1019
960
|
end
|
1020
961
|
|
1021
962
|
when :self
|
1022
|
-
typing.add_typing node, AST::Types::Self.new
|
963
|
+
typing.add_typing node, AST::Types::Self.new, context
|
1023
964
|
|
1024
965
|
when :const
|
1025
966
|
const_name = Names::Module.from_node(node)
|
@@ -1027,7 +968,7 @@ module Steep
|
|
1027
968
|
type = type_env.get(const: const_name) do
|
1028
969
|
fallback_to_any node
|
1029
970
|
end
|
1030
|
-
typing.add_typing node, type
|
971
|
+
typing.add_typing node, type, context
|
1031
972
|
else
|
1032
973
|
fallback_to_any node
|
1033
974
|
end
|
@@ -1050,7 +991,7 @@ module Steep
|
|
1050
991
|
end
|
1051
992
|
end
|
1052
993
|
|
1053
|
-
typing.add_typing(node, type)
|
994
|
+
typing.add_typing(node, type, context)
|
1054
995
|
else
|
1055
996
|
synthesize(node.children.last)
|
1056
997
|
fallback_to_any(node)
|
@@ -1072,7 +1013,7 @@ module Steep
|
|
1072
1013
|
end
|
1073
1014
|
end
|
1074
1015
|
|
1075
|
-
typing.add_typing(node, block_type.type.return_type)
|
1016
|
+
typing.add_typing(node, block_type.type.return_type, context)
|
1076
1017
|
else
|
1077
1018
|
typing.add_error(Errors::UnexpectedYield.new(node: node))
|
1078
1019
|
fallback_to_any node
|
@@ -1095,7 +1036,7 @@ module Steep
|
|
1095
1036
|
raise "Unexpected method_type: #{method_type.inspect}"
|
1096
1037
|
end
|
1097
1038
|
}
|
1098
|
-
typing.add_typing(node, union_type(*types))
|
1039
|
+
typing.add_typing(node, union_type(*types), context)
|
1099
1040
|
else
|
1100
1041
|
typing.add_error(Errors::UnexpectedSuper.new(node: node, method: method_context.name))
|
1101
1042
|
fallback_to_any node
|
@@ -1117,7 +1058,7 @@ module Steep
|
|
1117
1058
|
end
|
1118
1059
|
end
|
1119
1060
|
|
1120
|
-
typing.add_typing(node, array_type || AST::Builtin::Array.instance_type(AST::Builtin.any_type))
|
1061
|
+
typing.add_typing(node, array_type || AST::Builtin::Array.instance_type(AST::Builtin.any_type), context)
|
1121
1062
|
else
|
1122
1063
|
is_tuple = nil
|
1123
1064
|
|
@@ -1170,7 +1111,7 @@ module Steep
|
|
1170
1111
|
array_type = AST::Builtin::Array.instance_type(AST::Types::Union.build(types: element_types))
|
1171
1112
|
end
|
1172
1113
|
|
1173
|
-
typing.add_typing(node, array_type)
|
1114
|
+
typing.add_typing(node, array_type, context)
|
1174
1115
|
end
|
1175
1116
|
end
|
1176
1117
|
|
@@ -1189,9 +1130,9 @@ module Steep
|
|
1189
1130
|
const_env: nil)])
|
1190
1131
|
|
1191
1132
|
if left_type.is_a?(AST::Types::Boolean)
|
1192
|
-
typing.add_typing(node, union_type(left_type, right_type))
|
1133
|
+
typing.add_typing(node, union_type(left_type, right_type), context)
|
1193
1134
|
else
|
1194
|
-
typing.add_typing(node, union_type(right_type, AST::Builtin.nil_type))
|
1135
|
+
typing.add_typing(node, union_type(right_type, AST::Builtin.nil_type), context)
|
1195
1136
|
end
|
1196
1137
|
end
|
1197
1138
|
|
@@ -1201,7 +1142,7 @@ module Steep
|
|
1201
1142
|
t1 = synthesize(c1, hint: hint)
|
1202
1143
|
t2 = synthesize(c2, hint: unwrap(t1))
|
1203
1144
|
type = union_type(unwrap(t1), t2)
|
1204
|
-
typing.add_typing(node, type)
|
1145
|
+
typing.add_typing(node, type, context)
|
1205
1146
|
end
|
1206
1147
|
|
1207
1148
|
when :if
|
@@ -1224,7 +1165,7 @@ module Steep
|
|
1224
1165
|
end
|
1225
1166
|
|
1226
1167
|
type_env.join!([true_env, false_env].compact)
|
1227
|
-
typing.add_typing(node, union_type(true_type, false_type))
|
1168
|
+
typing.add_typing(node, union_type(true_type, false_type), context)
|
1228
1169
|
|
1229
1170
|
when :case
|
1230
1171
|
yield_self do
|
@@ -1306,7 +1247,7 @@ module Steep
|
|
1306
1247
|
end
|
1307
1248
|
|
1308
1249
|
type_env.join!(envs.compact)
|
1309
|
-
typing.add_typing(node, union_type(*types))
|
1250
|
+
typing.add_typing(node, union_type(*types), context)
|
1310
1251
|
end
|
1311
1252
|
|
1312
1253
|
when :rescue
|
@@ -1373,7 +1314,7 @@ module Steep
|
|
1373
1314
|
type_env.join!([*resbody_envs, else_env].compact)
|
1374
1315
|
|
1375
1316
|
types = [body_type, *resbody_types, else_type].compact
|
1376
|
-
typing.add_typing(node, union_type(*types))
|
1317
|
+
typing.add_typing(node, union_type(*types), context)
|
1377
1318
|
end
|
1378
1319
|
|
1379
1320
|
when :resbody
|
@@ -1382,7 +1323,7 @@ module Steep
|
|
1382
1323
|
synthesize(klasses) if klasses
|
1383
1324
|
synthesize(asgn) if asgn
|
1384
1325
|
body_type = synthesize(body) if body
|
1385
|
-
typing.add_typing(node, body_type)
|
1326
|
+
typing.add_typing(node, body_type, context)
|
1386
1327
|
end
|
1387
1328
|
|
1388
1329
|
when :ensure
|
@@ -1390,7 +1331,7 @@ module Steep
|
|
1390
1331
|
body, ensure_body = node.children
|
1391
1332
|
body_type = synthesize(body) if body
|
1392
1333
|
synthesize(ensure_body) if ensure_body
|
1393
|
-
typing.add_typing(node, union_type(body_type))
|
1334
|
+
typing.add_typing(node, union_type(body_type), context)
|
1394
1335
|
end
|
1395
1336
|
|
1396
1337
|
when :masgn
|
@@ -1404,38 +1345,43 @@ module Steep
|
|
1404
1345
|
truthy_vars = node.type == :while ? TypeConstruction.truthy_variables(cond) : Set.new
|
1405
1346
|
|
1406
1347
|
if body
|
1407
|
-
for_loop = for_branch(body,
|
1348
|
+
for_loop = for_branch(body,
|
1349
|
+
truthy_vars: truthy_vars,
|
1350
|
+
break_context: TypeInference::Context::BreakContext.new(
|
1351
|
+
break_type: nil,
|
1352
|
+
next_type: nil
|
1353
|
+
))
|
1408
1354
|
for_loop.synthesize(body)
|
1409
1355
|
type_env.join!([for_loop.type_env])
|
1410
1356
|
end
|
1411
1357
|
|
1412
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
1358
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
1413
1359
|
end
|
1414
1360
|
|
1415
1361
|
when :irange, :erange
|
1416
1362
|
types = node.children.map {|n| synthesize(n)}
|
1417
1363
|
type = AST::Builtin::Range.instance_type(union_type(*types))
|
1418
|
-
typing.add_typing(node, type)
|
1364
|
+
typing.add_typing(node, type, context)
|
1419
1365
|
|
1420
1366
|
when :regexp
|
1421
1367
|
each_child_node(node) do |child|
|
1422
1368
|
synthesize(child)
|
1423
1369
|
end
|
1424
1370
|
|
1425
|
-
typing.add_typing(node, AST::Builtin::Regexp.instance_type)
|
1371
|
+
typing.add_typing(node, AST::Builtin::Regexp.instance_type, context)
|
1426
1372
|
|
1427
1373
|
when :regopt
|
1428
1374
|
# ignore
|
1429
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
1375
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
1430
1376
|
|
1431
1377
|
when :nth_ref, :back_ref
|
1432
|
-
typing.add_typing(node, AST::Builtin::String.instance_type)
|
1378
|
+
typing.add_typing(node, AST::Builtin::String.instance_type, context)
|
1433
1379
|
|
1434
1380
|
when :or_asgn, :and_asgn
|
1435
1381
|
yield_self do
|
1436
1382
|
_, rhs = node.children
|
1437
1383
|
rhs_type = synthesize(rhs)
|
1438
|
-
typing.add_typing(node, rhs_type)
|
1384
|
+
typing.add_typing(node, rhs_type, context)
|
1439
1385
|
end
|
1440
1386
|
|
1441
1387
|
when :defined?
|
@@ -1443,7 +1389,7 @@ module Steep
|
|
1443
1389
|
synthesize(child)
|
1444
1390
|
end
|
1445
1391
|
|
1446
|
-
typing.add_typing(node, AST::Builtin.any_type)
|
1392
|
+
typing.add_typing(node, AST::Builtin.any_type, context)
|
1447
1393
|
|
1448
1394
|
when :gvasgn
|
1449
1395
|
yield_self do
|
@@ -1469,7 +1415,7 @@ module Steep
|
|
1469
1415
|
typing.add_error Errors::FallbackAny.new(node: node)
|
1470
1416
|
end
|
1471
1417
|
|
1472
|
-
typing.add_typing(node, type)
|
1418
|
+
typing.add_typing(node, type, context)
|
1473
1419
|
end
|
1474
1420
|
|
1475
1421
|
when :block_pass
|
@@ -1499,7 +1445,7 @@ module Steep
|
|
1499
1445
|
|
1500
1446
|
type ||= synthesize(node.children[0], hint: hint)
|
1501
1447
|
|
1502
|
-
typing.add_typing node, type
|
1448
|
+
typing.add_typing node, type, context
|
1503
1449
|
end
|
1504
1450
|
|
1505
1451
|
when :blockarg
|
@@ -1508,7 +1454,7 @@ module Steep
|
|
1508
1454
|
synthesize(child)
|
1509
1455
|
end
|
1510
1456
|
|
1511
|
-
typing.add_typing node, AST::Builtin.any_type
|
1457
|
+
typing.add_typing node, AST::Builtin.any_type, context
|
1512
1458
|
end
|
1513
1459
|
|
1514
1460
|
when :splat, :sclass, :alias
|
@@ -1519,7 +1465,7 @@ module Steep
|
|
1519
1465
|
synthesize(child)
|
1520
1466
|
end
|
1521
1467
|
|
1522
|
-
typing.add_typing node, AST::Builtin.any_type
|
1468
|
+
typing.add_typing node, AST::Builtin.any_type, context
|
1523
1469
|
end
|
1524
1470
|
|
1525
1471
|
else
|
@@ -1541,14 +1487,14 @@ module Steep
|
|
1541
1487
|
if rhs
|
1542
1488
|
expand_alias(synthesize(rhs, hint: type_env.lvar_types[var.name] || hint)) do |rhs_type|
|
1543
1489
|
node_type = assign_type_to_variable(var, rhs_type, node)
|
1544
|
-
typing.add_typing(node, node_type)
|
1490
|
+
typing.add_typing(node, node_type, context)
|
1545
1491
|
end
|
1546
1492
|
else
|
1547
1493
|
raise
|
1548
1494
|
lhs_type = variable_type(var)
|
1549
1495
|
|
1550
1496
|
if lhs_type
|
1551
|
-
typing.add_typing(node, lhs_type)
|
1497
|
+
typing.add_typing(node, lhs_type, context)
|
1552
1498
|
else
|
1553
1499
|
fallback_to_any node
|
1554
1500
|
end
|
@@ -1580,7 +1526,7 @@ module Steep
|
|
1580
1526
|
fallback_to_any node
|
1581
1527
|
end
|
1582
1528
|
end
|
1583
|
-
typing.add_typing(node, ivar_type)
|
1529
|
+
typing.add_typing(node, ivar_type, context)
|
1584
1530
|
end
|
1585
1531
|
|
1586
1532
|
def type_masgn(node)
|
@@ -1600,7 +1546,7 @@ module Steep
|
|
1600
1546
|
end
|
1601
1547
|
end
|
1602
1548
|
|
1603
|
-
typing.add_typing(node, rhs_type)
|
1549
|
+
typing.add_typing(node, rhs_type, context)
|
1604
1550
|
|
1605
1551
|
when rhs_type.is_a?(AST::Types::Tuple) && lhs.children.all? {|a| a.type == :lvasgn || a.type == :ivasgn}
|
1606
1552
|
lhs.children.each.with_index do |asgn, index|
|
@@ -1628,7 +1574,7 @@ module Steep
|
|
1628
1574
|
end
|
1629
1575
|
end
|
1630
1576
|
|
1631
|
-
typing.add_typing(node, rhs_type)
|
1577
|
+
typing.add_typing(node, rhs_type, context)
|
1632
1578
|
|
1633
1579
|
when rhs_type.is_a?(AST::Types::Any)
|
1634
1580
|
fallback_to_any(node)
|
@@ -1658,7 +1604,7 @@ module Steep
|
|
1658
1604
|
end
|
1659
1605
|
end
|
1660
1606
|
|
1661
|
-
typing.add_typing node, rhs_type
|
1607
|
+
typing.add_typing node, rhs_type, context
|
1662
1608
|
|
1663
1609
|
when rhs_type.is_a?(AST::Types::Union) &&
|
1664
1610
|
rhs_type.types.all? {|type| AST::Builtin::Array.instance_type?(type)}
|
@@ -1691,7 +1637,7 @@ module Steep
|
|
1691
1637
|
end
|
1692
1638
|
end
|
1693
1639
|
|
1694
|
-
typing.add_typing node, rhs_type
|
1640
|
+
typing.add_typing node, rhs_type, context
|
1695
1641
|
|
1696
1642
|
else
|
1697
1643
|
Steep.logger.error("Unsupported masgn: #{rhs.type} (#{rhs_type})")
|
@@ -1717,7 +1663,7 @@ module Steep
|
|
1717
1663
|
block_annotations: block_annotations,
|
1718
1664
|
topdown_hint: true)
|
1719
1665
|
|
1720
|
-
typing.add_typing node, block_type
|
1666
|
+
typing.add_typing node, block_type, context
|
1721
1667
|
end
|
1722
1668
|
|
1723
1669
|
def type_send(node, send_node:, block_params:, block_body:, unwrap: false)
|
@@ -1732,7 +1678,7 @@ module Steep
|
|
1732
1678
|
|
1733
1679
|
return_type = case receiver_type
|
1734
1680
|
when AST::Types::Any
|
1735
|
-
typing.add_typing node, AST::Builtin.any_type
|
1681
|
+
typing.add_typing node, AST::Builtin.any_type, context
|
1736
1682
|
|
1737
1683
|
when nil
|
1738
1684
|
fallback_to_any node
|
@@ -1768,7 +1714,7 @@ module Steep
|
|
1768
1714
|
receiver_type: receiver_type,
|
1769
1715
|
topdown_hint: true)
|
1770
1716
|
|
1771
|
-
typing.add_typing node, return_type
|
1717
|
+
typing.add_typing node, return_type, context
|
1772
1718
|
else
|
1773
1719
|
fallback_to_any node do
|
1774
1720
|
Errors::NoMethod.new(node: node, method: method_name, type: expanded_receiver_type)
|
@@ -1793,7 +1739,7 @@ module Steep
|
|
1793
1739
|
unless typing.has_type?(arg)
|
1794
1740
|
if arg.type == :splat
|
1795
1741
|
type = synthesize(arg.children[0])
|
1796
|
-
typing.add_typing(arg, AST::Builtin::Array.instance_type(type))
|
1742
|
+
typing.add_typing(arg, AST::Builtin::Array.instance_type(type), context)
|
1797
1743
|
else
|
1798
1744
|
synthesize(arg)
|
1799
1745
|
end
|
@@ -1843,10 +1789,10 @@ module Steep
|
|
1843
1789
|
end
|
1844
1790
|
Steep.logger.debug("return_type = #{return_type}")
|
1845
1791
|
|
1846
|
-
block_context = BlockContext.new(body_type: block_annotations.block_type)
|
1792
|
+
block_context = TypeInference::Context::BlockContext.new(body_type: block_annotations.block_type)
|
1847
1793
|
Steep.logger.debug("block_context { body_type: #{block_context.body_type} }")
|
1848
1794
|
|
1849
|
-
break_context = BreakContext.new(
|
1795
|
+
break_context = TypeInference::Context::BreakContext.new(
|
1850
1796
|
break_type: block_annotations.break_type || method_return_type,
|
1851
1797
|
next_type: block_annotations.block_type
|
1852
1798
|
)
|
@@ -1856,13 +1802,15 @@ module Steep
|
|
1856
1802
|
checker: checker,
|
1857
1803
|
source: source,
|
1858
1804
|
annotations: annotations.merge_block_annotations(block_annotations),
|
1859
|
-
type_env: block_type_env,
|
1860
|
-
block_context: block_context,
|
1861
1805
|
typing: typing,
|
1862
|
-
|
1863
|
-
|
1864
|
-
|
1865
|
-
|
1806
|
+
context: TypeInference::Context.new(
|
1807
|
+
block_context: block_context,
|
1808
|
+
method_context: method_context,
|
1809
|
+
module_context: module_context,
|
1810
|
+
break_context: break_context,
|
1811
|
+
self_type: block_annotations.self_type || self_type,
|
1812
|
+
type_env: block_type_env
|
1813
|
+
)
|
1866
1814
|
), return_type]
|
1867
1815
|
end
|
1868
1816
|
|
@@ -1979,7 +1927,7 @@ module Steep
|
|
1979
1927
|
this.synthesize(block_body)
|
1980
1928
|
end
|
1981
1929
|
|
1982
|
-
child_typing.add_typing node, AST::Builtin.any_type
|
1930
|
+
child_typing.add_typing node, AST::Builtin.any_type, context
|
1983
1931
|
|
1984
1932
|
[[AST::Builtin.any_type, child_typing, :any]]
|
1985
1933
|
end
|
@@ -2029,7 +1977,7 @@ module Steep
|
|
2029
1977
|
when :hash
|
2030
1978
|
keyword_hash_type = AST::Builtin::Hash.instance_type(AST::Builtin::Symbol.instance_type,
|
2031
1979
|
AST::Builtin.any_type)
|
2032
|
-
typing.add_typing node, keyword_hash_type
|
1980
|
+
typing.add_typing node, keyword_hash_type, context
|
2033
1981
|
|
2034
1982
|
given_keys = Set.new()
|
2035
1983
|
|
@@ -2053,7 +2001,7 @@ module Steep
|
|
2053
2001
|
params.rest_keywords
|
2054
2002
|
end
|
2055
2003
|
|
2056
|
-
typing.add_typing key_node, AST::Builtin::Symbol.instance_type
|
2004
|
+
typing.add_typing key_node, AST::Builtin::Symbol.instance_type, context
|
2057
2005
|
|
2058
2006
|
given_keys << key_symbol
|
2059
2007
|
|
@@ -2159,13 +2107,8 @@ module Steep
|
|
2159
2107
|
checker: checker,
|
2160
2108
|
source: source,
|
2161
2109
|
annotations: annotations,
|
2162
|
-
type_env: type_env,
|
2163
2110
|
typing: child_typing,
|
2164
|
-
|
2165
|
-
method_context: method_context,
|
2166
|
-
block_context: block_context,
|
2167
|
-
module_context: module_context,
|
2168
|
-
break_context: break_context
|
2111
|
+
context: context
|
2169
2112
|
)
|
2170
2113
|
|
2171
2114
|
method_type.instantiate(instantiation).yield_self do |method_type|
|
@@ -2182,7 +2125,7 @@ module Steep
|
|
2182
2125
|
|
2183
2126
|
arg_type = if arg_node.type == :splat
|
2184
2127
|
type = construction.synthesize(arg_node.children[0])
|
2185
|
-
child_typing.add_typing(arg_node, type)
|
2128
|
+
child_typing.add_typing(arg_node, type, context)
|
2186
2129
|
else
|
2187
2130
|
construction.synthesize(arg_node, hint: topdown_hint ? param_type : nil)
|
2188
2131
|
end
|
@@ -2364,10 +2307,10 @@ module Steep
|
|
2364
2307
|
end
|
2365
2308
|
Steep.logger.debug("return_type = #{break_type}")
|
2366
2309
|
|
2367
|
-
block_context = BlockContext.new(body_type: block_annotations.block_type)
|
2310
|
+
block_context = TypeInference::Context::BlockContext.new(body_type: block_annotations.block_type)
|
2368
2311
|
Steep.logger.debug("block_context { body_type: #{block_context.body_type} }")
|
2369
2312
|
|
2370
|
-
break_context = BreakContext.new(
|
2313
|
+
break_context = TypeInference::Context::BreakContext.new(
|
2371
2314
|
break_type: break_type,
|
2372
2315
|
next_type: block_context.body_type
|
2373
2316
|
)
|
@@ -2377,13 +2320,15 @@ module Steep
|
|
2377
2320
|
checker: checker,
|
2378
2321
|
source: source,
|
2379
2322
|
annotations: annotations.merge_block_annotations(block_annotations),
|
2380
|
-
type_env: block_type_env,
|
2381
|
-
block_context: block_context,
|
2382
2323
|
typing: typing,
|
2383
|
-
|
2384
|
-
|
2385
|
-
|
2386
|
-
|
2324
|
+
context: TypeInference::Context.new(
|
2325
|
+
block_context: block_context,
|
2326
|
+
method_context: method_context,
|
2327
|
+
module_context: module_context,
|
2328
|
+
break_context: break_context,
|
2329
|
+
self_type: block_annotations.self_type || self_type,
|
2330
|
+
type_env: block_type_env
|
2331
|
+
)
|
2387
2332
|
)
|
2388
2333
|
|
2389
2334
|
if block_body
|
@@ -2600,7 +2545,7 @@ module Steep
|
|
2600
2545
|
typing.add_error Errors::FallbackAny.new(node: node)
|
2601
2546
|
end
|
2602
2547
|
|
2603
|
-
typing.add_typing node, AST::Builtin.any_type
|
2548
|
+
typing.add_typing node, AST::Builtin.any_type, context
|
2604
2549
|
end
|
2605
2550
|
|
2606
2551
|
def self_class?(node)
|
@@ -2774,7 +2719,7 @@ module Steep
|
|
2774
2719
|
child_typing.save!
|
2775
2720
|
|
2776
2721
|
hash = AST::Types::Record.new(elements: elements)
|
2777
|
-
typing.add_typing(node, hash)
|
2722
|
+
typing.add_typing(node, hash, context)
|
2778
2723
|
end
|
2779
2724
|
when AST::Types::Union
|
2780
2725
|
hint.types.find do |type|
|