steep 1.0.0 → 1.1.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby-windows.yml +34 -0
  3. data/.github/workflows/ruby.yml +7 -2
  4. data/.gitignore +1 -0
  5. data/CHANGELOG.md +59 -0
  6. data/Gemfile +7 -4
  7. data/Gemfile.lock +17 -21
  8. data/Gemfile.steep +3 -0
  9. data/Gemfile.steep.lock +49 -0
  10. data/Rakefile +5 -0
  11. data/Steepfile +6 -1
  12. data/bin/setup +2 -0
  13. data/bin/steep +19 -0
  14. data/lib/steep/ast/builtin.rb +2 -2
  15. data/lib/steep/ast/types/factory.rb +7 -3
  16. data/lib/steep/ast/types/proc.rb +2 -0
  17. data/lib/steep/cli.rb +3 -1
  18. data/lib/steep/diagnostic/ruby.rb +50 -4
  19. data/lib/steep/diagnostic/signature.rb +18 -0
  20. data/lib/steep/drivers/check.rb +3 -3
  21. data/lib/steep/drivers/watch.rb +3 -1
  22. data/lib/steep/method_name.rb +9 -3
  23. data/lib/steep/node_helper.rb +49 -0
  24. data/lib/steep/path_helper.rb +22 -0
  25. data/lib/steep/project.rb +3 -15
  26. data/lib/steep/server/base_worker.rb +1 -0
  27. data/lib/steep/server/change_buffer.rb +1 -1
  28. data/lib/steep/server/interaction_worker.rb +3 -5
  29. data/lib/steep/server/master.rb +61 -45
  30. data/lib/steep/server/type_check_worker.rb +10 -25
  31. data/lib/steep/services/completion_provider.rb +25 -18
  32. data/lib/steep/services/goto_service.rb +2 -4
  33. data/lib/steep/services/hover_provider/rbs.rb +1 -1
  34. data/lib/steep/services/hover_provider/ruby.rb +30 -12
  35. data/lib/steep/services/stats_calculator.rb +0 -1
  36. data/lib/steep/services/type_check_service.rb +15 -12
  37. data/lib/steep/shims/symbol_start_with.rb +18 -0
  38. data/lib/steep/signature/validator.rb +25 -1
  39. data/lib/steep/source.rb +1 -1
  40. data/lib/steep/subtyping/check.rb +0 -3
  41. data/lib/steep/subtyping/constraints.rb +43 -14
  42. data/lib/steep/type_construction.rb +721 -764
  43. data/lib/steep/type_inference/constant_env.rb +0 -2
  44. data/lib/steep/type_inference/context.rb +23 -17
  45. data/lib/steep/type_inference/logic_type_interpreter.rb +210 -117
  46. data/lib/steep/type_inference/method_call.rb +80 -6
  47. data/lib/steep/type_inference/multiple_assignment.rb +189 -0
  48. data/lib/steep/type_inference/send_args.rb +1 -2
  49. data/lib/steep/type_inference/type_env.rb +273 -116
  50. data/lib/steep/type_inference/type_env_builder.rb +138 -0
  51. data/lib/steep/typing.rb +2 -0
  52. data/lib/steep/version.rb +1 -1
  53. data/lib/steep.rb +7 -5
  54. data/rbs_collection.steep.lock.yaml +112 -0
  55. data/rbs_collection.steep.yaml +19 -0
  56. data/sample/sig/conference.rbs +8 -0
  57. data/sig/shims/parser/source/map.rbs +146 -0
  58. data/sig/shims/parser/source/range.rbs +237 -0
  59. data/sig/shims/parser.rbs +17 -0
  60. data/sig/steep/ast/annotation/collection.rbs +75 -0
  61. data/sig/steep/ast/annotation.rbs +126 -0
  62. data/sig/steep/ast/builtin.rbs +69 -0
  63. data/sig/steep/ast/type_params.rbs +11 -0
  64. data/sig/steep/ast/types/any.rbs +29 -0
  65. data/sig/steep/ast/types/boolean.rbs +31 -0
  66. data/sig/steep/ast/types/bot.rbs +29 -0
  67. data/sig/steep/ast/types/class.rbs +29 -0
  68. data/sig/steep/ast/types/factory.rbs +76 -0
  69. data/sig/steep/ast/types/helper.rbs +19 -0
  70. data/sig/steep/ast/types/instance.rbs +29 -0
  71. data/sig/steep/ast/types/intersection.rbs +35 -0
  72. data/sig/steep/ast/types/literal.rbs +33 -0
  73. data/sig/steep/ast/types/logic.rbs +78 -0
  74. data/sig/steep/ast/types/name.rbs +71 -0
  75. data/sig/steep/ast/types/nil.rbs +31 -0
  76. data/sig/steep/ast/types/proc.rbs +46 -0
  77. data/sig/steep/ast/types/record.rbs +38 -0
  78. data/sig/steep/ast/types/self.rbs +29 -0
  79. data/sig/steep/ast/types/top.rbs +29 -0
  80. data/sig/steep/ast/types/tuple.rbs +34 -0
  81. data/sig/steep/ast/types/union.rbs +38 -0
  82. data/sig/steep/ast/types/var.rbs +37 -0
  83. data/sig/steep/ast/types/void.rbs +29 -0
  84. data/sig/steep/ast/types.rbs +37 -0
  85. data/sig/steep/diagnostic/deprecated/unknown_constant_assigned.rbs +15 -0
  86. data/sig/steep/diagnostic/helper.rbs +9 -0
  87. data/sig/steep/diagnostic/lsp_formatter.rbs +29 -0
  88. data/sig/steep/diagnostic/ruby.rbs +494 -0
  89. data/sig/steep/diagnostic/signature.rbs +215 -0
  90. data/sig/steep/interface/block.rbs +35 -0
  91. data/sig/steep/interface/function.rbs +253 -0
  92. data/sig/steep/interface/interface.rbs +23 -0
  93. data/sig/steep/interface/method_type.rbs +55 -0
  94. data/sig/steep/interface/substitution.rbs +53 -0
  95. data/sig/steep/interface/type_param.rbs +35 -0
  96. data/sig/steep/method_name.rbs +26 -0
  97. data/sig/steep/module_helper.rbs +7 -0
  98. data/sig/steep/node_helper.rbs +11 -0
  99. data/sig/steep/project/dsl.rbs +94 -0
  100. data/sig/steep/project/options.rbs +15 -0
  101. data/sig/steep/project/pattern.rbs +25 -0
  102. data/sig/steep/project/target.rbs +25 -0
  103. data/sig/steep/project.rbs +19 -0
  104. data/sig/steep/services/completion_provider.rbs +123 -0
  105. data/sig/steep/services/content_change.rbs +35 -0
  106. data/sig/steep/services/file_loader.rbs +13 -0
  107. data/sig/steep/services/goto_service.rbs +45 -0
  108. data/sig/steep/services/hover_provider/rbs.rbs +21 -0
  109. data/sig/steep/services/hover_provider/ruby.rbs +109 -0
  110. data/sig/steep/services/hover_provider/singleton_methods.rbs +11 -0
  111. data/sig/steep/services/path_assignment.rbs +21 -0
  112. data/sig/steep/services/signature_service.rbs +91 -0
  113. data/sig/steep/services/stats_calculator.rbs +17 -0
  114. data/sig/steep/services/type_check_service.rbs +93 -0
  115. data/sig/steep/source.rbs +55 -0
  116. data/sig/steep/subtyping/cache.rbs +17 -0
  117. data/sig/steep/subtyping/check.rbs +93 -0
  118. data/sig/steep/subtyping/constraints.rbs +111 -0
  119. data/sig/steep/subtyping/relation.rbs +51 -0
  120. data/sig/steep/subtyping/result.rbs +157 -0
  121. data/sig/steep/subtyping/variable_variance.rbs +23 -0
  122. data/sig/steep/type_construction.rbs +285 -0
  123. data/sig/steep/type_inference/block_params.rbs +52 -0
  124. data/sig/steep/type_inference/constant_env.rbs +27 -0
  125. data/sig/steep/type_inference/context.rbs +137 -0
  126. data/sig/steep/type_inference/logic_type_interpreter.rbs +72 -0
  127. data/sig/steep/type_inference/method_call.rbs +124 -0
  128. data/sig/steep/type_inference/method_params.rbs +104 -0
  129. data/sig/steep/type_inference/multiple_assignment.rbs +76 -0
  130. data/sig/steep/type_inference/type_env.rbs +158 -0
  131. data/sig/steep/type_inference/type_env_builder.rbs +77 -0
  132. data/sig/steep/typing.rbs +68 -0
  133. data/sig/steep.rbs +31 -0
  134. data/smoke/class/f.rb +1 -0
  135. data/smoke/class/test_expectations.yml +2 -2
  136. data/smoke/diagnostics/test_expectations.yml +4 -2
  137. data/smoke/regression/lambda.rb +3 -0
  138. data/smoke/regression/test_expectations.yml +12 -0
  139. data/steep.gemspec +1 -1
  140. metadata +95 -9
  141. data/lib/steep/subtyping/variable_occurrence.rb +0 -51
  142. data/lib/steep/type_inference/local_variable_type_env.rb +0 -249
  143. data/lib/steep/type_inference/logic.rb +0 -161
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steep
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-20 00:00:00.000000000 Z
11
+ date: 2022-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.0'
19
+ version: '3.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3.0'
26
+ version: '3.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -149,12 +149,15 @@ extensions: []
149
149
  extra_rdoc_files: []
150
150
  files:
151
151
  - ".github/dependabot.yml"
152
+ - ".github/workflows/ruby-windows.yml"
152
153
  - ".github/workflows/ruby.yml"
153
154
  - ".gitignore"
154
155
  - ".gitmodules"
155
156
  - CHANGELOG.md
156
157
  - Gemfile
157
158
  - Gemfile.lock
159
+ - Gemfile.steep
160
+ - Gemfile.steep.lock
158
161
  - LICENSE
159
162
  - README.md
160
163
  - Rakefile
@@ -163,6 +166,7 @@ files:
163
166
  - bin/output_rebaseline.rb
164
167
  - bin/output_test.rb
165
168
  - bin/setup
169
+ - bin/steep
166
170
  - bin/steep-prof
167
171
  - exe/steep
168
172
  - lib/steep.rb
@@ -224,6 +228,8 @@ files:
224
228
  - lib/steep/interface/type_param.rb
225
229
  - lib/steep/method_name.rb
226
230
  - lib/steep/module_helper.rb
231
+ - lib/steep/node_helper.rb
232
+ - lib/steep/path_helper.rb
227
233
  - lib/steep/project.rb
228
234
  - lib/steep/project/dsl.rb
229
235
  - lib/steep/project/options.rb
@@ -249,6 +255,7 @@ files:
249
255
  - lib/steep/services/stats_calculator.rb
250
256
  - lib/steep/services/type_check_service.rb
251
257
  - lib/steep/shims/filter_map.rb
258
+ - lib/steep/shims/symbol_start_with.rb
252
259
  - lib/steep/signature/validator.rb
253
260
  - lib/steep/source.rb
254
261
  - lib/steep/subtyping/cache.rb
@@ -256,28 +263,106 @@ files:
256
263
  - lib/steep/subtyping/constraints.rb
257
264
  - lib/steep/subtyping/relation.rb
258
265
  - lib/steep/subtyping/result.rb
259
- - lib/steep/subtyping/variable_occurrence.rb
260
266
  - lib/steep/subtyping/variable_variance.rb
261
267
  - lib/steep/type_construction.rb
262
268
  - lib/steep/type_inference/block_params.rb
263
269
  - lib/steep/type_inference/constant_env.rb
264
270
  - lib/steep/type_inference/context.rb
265
271
  - lib/steep/type_inference/context_array.rb
266
- - lib/steep/type_inference/local_variable_type_env.rb
267
- - lib/steep/type_inference/logic.rb
268
272
  - lib/steep/type_inference/logic_type_interpreter.rb
269
273
  - lib/steep/type_inference/method_call.rb
270
274
  - lib/steep/type_inference/method_params.rb
275
+ - lib/steep/type_inference/multiple_assignment.rb
271
276
  - lib/steep/type_inference/send_args.rb
272
277
  - lib/steep/type_inference/type_env.rb
278
+ - lib/steep/type_inference/type_env_builder.rb
273
279
  - lib/steep/typing.rb
274
280
  - lib/steep/version.rb
275
281
  - manual/annotations.md
282
+ - rbs_collection.steep.lock.yaml
283
+ - rbs_collection.steep.yaml
276
284
  - sample/Steepfile
277
285
  - sample/lib/conference.rb
278
286
  - sample/lib/length.rb
279
287
  - sample/sig/conference.rbs
280
288
  - sample/sig/length.rbs
289
+ - sig/shims/parser.rbs
290
+ - sig/shims/parser/source/map.rbs
291
+ - sig/shims/parser/source/range.rbs
292
+ - sig/steep.rbs
293
+ - sig/steep/ast/annotation.rbs
294
+ - sig/steep/ast/annotation/collection.rbs
295
+ - sig/steep/ast/builtin.rbs
296
+ - sig/steep/ast/type_params.rbs
297
+ - sig/steep/ast/types.rbs
298
+ - sig/steep/ast/types/any.rbs
299
+ - sig/steep/ast/types/boolean.rbs
300
+ - sig/steep/ast/types/bot.rbs
301
+ - sig/steep/ast/types/class.rbs
302
+ - sig/steep/ast/types/factory.rbs
303
+ - sig/steep/ast/types/helper.rbs
304
+ - sig/steep/ast/types/instance.rbs
305
+ - sig/steep/ast/types/intersection.rbs
306
+ - sig/steep/ast/types/literal.rbs
307
+ - sig/steep/ast/types/logic.rbs
308
+ - sig/steep/ast/types/name.rbs
309
+ - sig/steep/ast/types/nil.rbs
310
+ - sig/steep/ast/types/proc.rbs
311
+ - sig/steep/ast/types/record.rbs
312
+ - sig/steep/ast/types/self.rbs
313
+ - sig/steep/ast/types/top.rbs
314
+ - sig/steep/ast/types/tuple.rbs
315
+ - sig/steep/ast/types/union.rbs
316
+ - sig/steep/ast/types/var.rbs
317
+ - sig/steep/ast/types/void.rbs
318
+ - sig/steep/diagnostic/deprecated/unknown_constant_assigned.rbs
319
+ - sig/steep/diagnostic/helper.rbs
320
+ - sig/steep/diagnostic/lsp_formatter.rbs
321
+ - sig/steep/diagnostic/ruby.rbs
322
+ - sig/steep/diagnostic/signature.rbs
323
+ - sig/steep/interface/block.rbs
324
+ - sig/steep/interface/function.rbs
325
+ - sig/steep/interface/interface.rbs
326
+ - sig/steep/interface/method_type.rbs
327
+ - sig/steep/interface/substitution.rbs
328
+ - sig/steep/interface/type_param.rbs
329
+ - sig/steep/method_name.rbs
330
+ - sig/steep/module_helper.rbs
331
+ - sig/steep/node_helper.rbs
332
+ - sig/steep/project.rbs
333
+ - sig/steep/project/dsl.rbs
334
+ - sig/steep/project/options.rbs
335
+ - sig/steep/project/pattern.rbs
336
+ - sig/steep/project/target.rbs
337
+ - sig/steep/services/completion_provider.rbs
338
+ - sig/steep/services/content_change.rbs
339
+ - sig/steep/services/file_loader.rbs
340
+ - sig/steep/services/goto_service.rbs
341
+ - sig/steep/services/hover_provider/rbs.rbs
342
+ - sig/steep/services/hover_provider/ruby.rbs
343
+ - sig/steep/services/hover_provider/singleton_methods.rbs
344
+ - sig/steep/services/path_assignment.rbs
345
+ - sig/steep/services/signature_service.rbs
346
+ - sig/steep/services/stats_calculator.rbs
347
+ - sig/steep/services/type_check_service.rbs
348
+ - sig/steep/source.rbs
349
+ - sig/steep/subtyping/cache.rbs
350
+ - sig/steep/subtyping/check.rbs
351
+ - sig/steep/subtyping/constraints.rbs
352
+ - sig/steep/subtyping/relation.rbs
353
+ - sig/steep/subtyping/result.rbs
354
+ - sig/steep/subtyping/variable_variance.rbs
355
+ - sig/steep/type_construction.rbs
356
+ - sig/steep/type_inference/block_params.rbs
357
+ - sig/steep/type_inference/constant_env.rbs
358
+ - sig/steep/type_inference/context.rbs
359
+ - sig/steep/type_inference/logic_type_interpreter.rbs
360
+ - sig/steep/type_inference/method_call.rbs
361
+ - sig/steep/type_inference/method_params.rbs
362
+ - sig/steep/type_inference/multiple_assignment.rbs
363
+ - sig/steep/type_inference/type_env.rbs
364
+ - sig/steep/type_inference/type_env_builder.rbs
365
+ - sig/steep/typing.rbs
281
366
  - smoke/alias/Steepfile
282
367
  - smoke/alias/a.rb
283
368
  - smoke/alias/a.rbs
@@ -481,6 +566,7 @@ files:
481
566
  - smoke/regression/issue_332.rbs
482
567
  - smoke/regression/issue_372.rb
483
568
  - smoke/regression/issue_372.rbs
569
+ - smoke/regression/lambda.rb
484
570
  - smoke/regression/masgn.rb
485
571
  - smoke/regression/poly_new.rb
486
572
  - smoke/regression/poly_new.rbs
@@ -543,9 +629,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
543
629
  version: 2.6.0
544
630
  required_rubygems_version: !ruby/object:Gem::Requirement
545
631
  requirements:
546
- - - ">="
632
+ - - ">"
547
633
  - !ruby/object:Gem::Version
548
- version: '0'
634
+ version: 1.3.1
549
635
  requirements: []
550
636
  rubygems_version: 3.3.7
551
637
  signing_key:
@@ -1,51 +0,0 @@
1
- module Steep
2
- module Subtyping
3
- class VariableOccurence
4
- attr_reader :params
5
- attr_reader :returns
6
-
7
- def initialize
8
- @params = Set.new
9
- @returns = Set.new
10
- end
11
-
12
- def add_method_type(method_type)
13
- method_type.type.params.each_type do |type|
14
- each_var(type) do |var|
15
- params << var
16
- end
17
- end
18
- each_var(method_type.type.return_type) do |var|
19
- returns << var
20
- end
21
-
22
- method_type.block&.yield_self do |block|
23
- block.type.params.each_type do |type|
24
- each_var(type) do |var|
25
- params << var
26
- end
27
- end
28
- each_var(block.type.return_type) do |var|
29
- returns << var
30
- end
31
- end
32
-
33
- params.subtract(returns)
34
- end
35
-
36
- def each_var(type, &block)
37
- type.free_variables.each(&block)
38
- end
39
-
40
- def strictly_return?(var)
41
- !params.member?(var) && returns.member?(var)
42
- end
43
-
44
- def self.from_method_type(method_type)
45
- self.new.tap do |occurence|
46
- occurence.add_method_type(method_type)
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -1,249 +0,0 @@
1
- module Steep
2
- module TypeInference
3
- class LocalVariableTypeEnv
4
- class Entry
5
- attr_reader :type
6
- attr_reader :annotations
7
- attr_reader :nodes
8
-
9
- def initialize(type:, annotations: [], nodes: [])
10
- @type = type
11
- @annotations = Set.new(annotations)
12
- @nodes = Set[].compare_by_identity.merge(nodes)
13
- end
14
-
15
- def update(type: self.type, annotations: self.annotations, nodes: self.nodes)
16
- Entry.new(type: type, annotations: annotations, nodes: nodes)
17
- end
18
-
19
- def ==(other)
20
- other.is_a?(Entry) &&
21
- other.type == type &&
22
- other.annotations == annotations &&
23
- other.nodes == nodes
24
- end
25
-
26
- def +(other)
27
- self.class.new(type: AST::Types::Union.build(types: [type, other.type]),
28
- annotations: annotations + other.annotations,
29
- nodes: nodes + other.nodes)
30
- end
31
-
32
- def optional
33
- self.class.new(type: AST::Types::Union.build(types: [type, AST::Builtin.nil_type]),
34
- annotations: annotations,
35
- nodes: nodes)
36
- end
37
- end
38
-
39
- attr_reader :subtyping
40
- attr_reader :self_type
41
- attr_reader :instance_type
42
- attr_reader :class_type
43
- attr_reader :declared_types
44
- attr_reader :assigned_types
45
-
46
- def self.empty(subtyping:, self_type:, instance_type:, class_type:)
47
- new(
48
- subtyping: subtyping,
49
- declared_types: {},
50
- assigned_types: {},
51
- self_type: self_type,
52
- instance_type: instance_type,
53
- class_type: class_type
54
- )
55
- end
56
-
57
- def initialize(subtyping:, declared_types:, assigned_types:, self_type:, instance_type:, class_type:)
58
- @subtyping = subtyping
59
- @self_type = self_type
60
-
61
- @declared_types = declared_types
62
- @assigned_types = assigned_types
63
- @class_type = class_type
64
- @instance_type = instance_type
65
-
66
- unless (intersection = Set.new(declared_types.keys) & Set.new(assigned_types.keys)).empty?
67
- raise "Declared types and assigned types should be disjoint: #{intersection}"
68
- end
69
- end
70
-
71
- def update(declared_types: self.declared_types, assigned_types: self.assigned_types, self_type: self.self_type, instance_type: self.instance_type, class_type: self.class_type)
72
- self.class.new(
73
- subtyping: subtyping,
74
- declared_types: declared_types,
75
- assigned_types: assigned_types,
76
- self_type: self_type,
77
- instance_type: instance_type,
78
- class_type: class_type
79
- )
80
- end
81
-
82
- def assign!(var, node:, type:)
83
- declared_type = declared_types[var]&.type
84
-
85
- if declared_type
86
- relation = Subtyping::Relation.new(sub_type: type, super_type: declared_type)
87
- constraints = Subtyping::Constraints.new(unknowns: Set.new)
88
- subtyping.check(relation, constraints: constraints, self_type: self_type, instance_type: instance_type, class_type: class_type).else do |result|
89
- yield declared_type, type, result
90
- end
91
- end
92
-
93
- assignments = { var => Entry.new(type: type, nodes: [node]) }
94
- update(assigned_types: assigned_types.merge(assignments),
95
- declared_types: declared_types.reject {|k, _| k == var })
96
- end
97
-
98
- def assign(var, node:, type:)
99
- declared_type = declared_types[var]&.type
100
-
101
- if declared_type
102
- relation = Subtyping::Relation.new(sub_type: type, super_type: declared_type)
103
- constraints = Subtyping::Constraints.new(unknowns: Set.new)
104
- subtyping.check(relation, constraints: constraints, self_type: self_type, instance_type: instance_type, class_type: class_type).else do |result|
105
- yield declared_type, type, result
106
- end
107
-
108
- self
109
- else
110
- assignments = { var => Entry.new(type: type, nodes: [node]) }
111
- update(assigned_types: assigned_types.merge(assignments))
112
- end
113
- end
114
-
115
- def annotate(collection)
116
- decls = collection.var_type_annotations.each.with_object({}) do |(var, annotation), hash|
117
- type = collection.var_type(lvar: var)
118
- hash[var] = Entry.new(type: type, annotations: [annotation])
119
- end
120
-
121
- decls.each do |var, annot|
122
- inner_type = annot.type
123
- outer_type = self[var]
124
-
125
- if outer_type
126
- relation = Subtyping::Relation.new(sub_type: inner_type, super_type: outer_type)
127
- constraints = Subtyping::Constraints.new(unknowns: Set.new)
128
- subtyping.check(relation, constraints: constraints, self_type: self_type, instance_type: instance_type, class_type: class_type).else do |result|
129
- if block_given?
130
- yield var, outer_type, inner_type, result
131
- end
132
- end
133
- end
134
- end
135
-
136
- new_decls = declared_types.merge(decls)
137
- new_assigns = assigned_types.reject {|var, _| new_decls.key?(var) }
138
-
139
- update(declared_types: new_decls, assigned_types: new_assigns)
140
- end
141
-
142
- def [](var)
143
- entry(var)&.type
144
- end
145
-
146
- def entry(var)
147
- declared_types[var] || assigned_types[var]
148
- end
149
-
150
- def pin_assignments
151
- update(
152
- declared_types: assigned_types.merge(declared_types),
153
- assigned_types: {}
154
- )
155
- end
156
-
157
- def except(variables)
158
- update(
159
- declared_types: declared_types.reject {|var, _| variables.include?(var) },
160
- assigned_types: assigned_types.reject {|var, _| variables.include?(var) }
161
- )
162
- end
163
-
164
- def subst(s)
165
- update(
166
- declared_types: declared_types.transform_values {|e| e.update(type: e.type.subst(s)) },
167
- assigned_types: assigned_types.transform_values {|e| e.update(type: e.type.subst(s)) }
168
- )
169
- end
170
-
171
- def each
172
- if block_given?
173
- vars.each do |var|
174
- yield var, self[var]
175
- end
176
- else
177
- enum_for :each
178
- end
179
- end
180
-
181
- def vars
182
- @vars ||= Set.new(declared_types.keys + assigned_types.keys)
183
- end
184
-
185
- def join(*envs)
186
- if envs.empty?
187
- self
188
- else
189
- env = envs.inject do |env1, env2|
190
- assigned_types = {}
191
- declared_types = {}
192
-
193
- (env1.vars + env2.vars).each do |var|
194
- e1 = env1.entry(var)
195
- e2 = env2.entry(var)
196
- je = join_entry(e1, e2)
197
-
198
- if env1.declared_types.key?(var) || env2.declared_types.key?(var)
199
- declared_types[var] = je
200
- else
201
- assigned_types[var] = je
202
- end
203
- end
204
-
205
- LocalVariableTypeEnv.new(
206
- subtyping: subtyping,
207
- self_type: self_type,
208
- declared_types: declared_types,
209
- assigned_types: assigned_types,
210
- instance_type: instance_type,
211
- class_type: class_type
212
- )
213
- end
214
-
215
- decls = env.declared_types.merge(declared_types)
216
- assignments = env.assigned_types.reject {|var, _| decls.key?(var) }
217
-
218
- update(
219
- declared_types: decls,
220
- assigned_types: assignments,
221
- )
222
- end
223
- end
224
-
225
- def join_entry(e1, e2)
226
- case
227
- when e1 && e2
228
- e1 + e2
229
- when e1
230
- e1.optional
231
- when e2
232
- e2.optional
233
- else
234
- raise
235
- end
236
- end
237
-
238
- def to_s
239
- ss = []
240
-
241
- vars.each do |var|
242
- ss << "#{var}: #{self[var].to_s}"
243
- end
244
-
245
- "{#{ss.join(", ")}}"
246
- end
247
- end
248
- end
249
- end
@@ -1,161 +0,0 @@
1
- module Steep
2
- module TypeInference
3
- class Logic
4
- class Result
5
- attr_reader :nodes
6
-
7
- def initialize(nodes = [])
8
- @nodes = Set[].compare_by_identity.merge(nodes)
9
- end
10
-
11
- def +(other)
12
- Result.new(nodes + other.nodes)
13
- end
14
-
15
- def merge(nodes)
16
- Result.new(self.nodes + nodes)
17
- end
18
-
19
- def vars
20
- vars = Set[]
21
-
22
- nodes.each do |node|
23
- case node.type
24
- when :lvar, :lvasgn
25
- vars << node.children[0]
26
- end
27
- end
28
-
29
- vars
30
- end
31
- end
32
-
33
- attr_reader :subtyping
34
-
35
- def initialize(subtyping:)
36
- @subtyping = subtyping
37
- end
38
-
39
- def nodes(node:)
40
- case node.type
41
- when :lvasgn
42
- rhs = node.children[1]
43
- t, f = nodes(node: rhs)
44
-
45
- [
46
- t.merge([node]),
47
- f.merge([node])
48
- ]
49
-
50
- when :masgn
51
- lhs, rhs = node.children
52
-
53
- lt, lf = nodes(node: lhs)
54
- rt, rf = nodes(node: rhs)
55
-
56
- [
57
- (lt + rt).merge([node]),
58
- (lf + rf).merge([node])
59
- ]
60
-
61
- when :mlhs
62
- nodes = [node]
63
-
64
- node.children.each do |child|
65
- case child.type
66
- when :lvasgn
67
- nodes << child
68
- when :splat
69
- if node.children[0].type == :lvasgn
70
- nodes << child
71
- nodes << child.children[0]
72
- end
73
- end
74
- end
75
-
76
- [
77
- Result.new(nodes),
78
- Result.new(nodes)
79
- ]
80
-
81
- when :and
82
- lhs, rhs = node.children
83
-
84
- lt, _ = nodes(node: lhs)
85
- rt, _ = nodes(node: rhs)
86
-
87
- [
88
- Result.new([node]) + lt + rt,
89
- Result.new([node])
90
- ]
91
-
92
- when :or
93
- lhs, rhs = node.children
94
-
95
- _, lf = nodes(node: lhs)
96
- _, rf = nodes(node: rhs)
97
-
98
- [
99
- Result.new([node]),
100
- Result.new([node]) + lf + rf
101
- ]
102
-
103
- when :begin
104
- nodes(node: node.children.last)
105
-
106
- else
107
- [
108
- Result.new([node]),
109
- Result.new([node])
110
- ]
111
- end
112
- end
113
-
114
- def environments(truthy_vars:, falsey_vars:, lvar_env:)
115
- truthy_hash = lvar_env.assigned_types.dup
116
- falsey_hash = lvar_env.assigned_types.dup
117
-
118
- (truthy_vars + falsey_vars).each do |var|
119
- type = lvar_env[var]
120
- truthy_type, falsey_type = partition_union(type)
121
-
122
- if truthy_vars.include?(var)
123
- truthy_hash[var] = LocalVariableTypeEnv::Entry.new(type: truthy_type)
124
- end
125
-
126
- if falsey_vars.include?(var)
127
- falsey_hash[var] = LocalVariableTypeEnv::Entry.new(type: falsey_type)
128
- end
129
- end
130
-
131
- [
132
- lvar_env.except(truthy_vars).update(assigned_types: truthy_hash),
133
- lvar_env.except(falsey_vars).update(assigned_types: falsey_hash)
134
- ]
135
- end
136
-
137
- def partition_union(type)
138
- case type
139
- when AST::Types::Union
140
- falsey_types, truthy_types = type.types.partition do |type|
141
- case type
142
- when AST::Types::Nil
143
- true
144
- when AST::Types::Literal
145
- type.value == false
146
- end
147
- end
148
-
149
- [
150
- truthy_types.empty? ? AST::Types::Bot.new : AST::Types::Union.build(types: truthy_types),
151
- falsey_types.empty? ? AST::Types::Bot.new : AST::Types::Union.build(types: falsey_types)
152
- ]
153
- when AST::Types::Any, AST::Types::Top, AST::Types::Boolean, AST::Types::Void
154
- [type, type]
155
- else
156
- [type, AST::Types::Bot.new]
157
- end
158
- end
159
- end
160
- end
161
- end