steep 1.0.2 → 1.1.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +5 -0
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +22 -3
  5. data/Gemfile +6 -3
  6. data/Gemfile.lock +12 -16
  7. data/Gemfile.steep +3 -0
  8. data/Gemfile.steep.lock +49 -0
  9. data/Rakefile +5 -0
  10. data/Steepfile +6 -1
  11. data/bin/setup +2 -0
  12. data/bin/steep +19 -0
  13. data/lib/steep/ast/types/factory.rb +1 -1
  14. data/lib/steep/diagnostic/ruby.rb +49 -3
  15. data/lib/steep/diagnostic/signature.rb +18 -0
  16. data/lib/steep/drivers/watch.rb +3 -1
  17. data/lib/steep/method_name.rb +9 -3
  18. data/lib/steep/node_helper.rb +49 -0
  19. data/lib/steep/services/completion_provider.rb +22 -15
  20. data/lib/steep/services/hover_provider/ruby.rb +30 -12
  21. data/lib/steep/services/type_check_service.rb +12 -12
  22. data/lib/steep/shims/symbol_start_with.rb +18 -0
  23. data/lib/steep/signature/validator.rb +19 -0
  24. data/lib/steep/subtyping/constraints.rb +43 -14
  25. data/lib/steep/type_construction.rb +666 -746
  26. data/lib/steep/type_inference/constant_env.rb +0 -2
  27. data/lib/steep/type_inference/context.rb +23 -17
  28. data/lib/steep/type_inference/logic_type_interpreter.rb +210 -119
  29. data/lib/steep/type_inference/method_call.rb +80 -6
  30. data/lib/steep/type_inference/multiple_assignment.rb +189 -0
  31. data/lib/steep/type_inference/type_env.rb +271 -120
  32. data/lib/steep/type_inference/type_env_builder.rb +138 -0
  33. data/lib/steep/typing.rb +2 -0
  34. data/lib/steep/version.rb +1 -1
  35. data/lib/steep.rb +4 -3
  36. data/rbs_collection.steep.lock.yaml +112 -0
  37. data/rbs_collection.steep.yaml +19 -0
  38. data/sample/sig/conference.rbs +8 -0
  39. data/sig/shims/parser/source/map.rbs +146 -0
  40. data/sig/shims/parser/source/range.rbs +237 -0
  41. data/sig/shims/parser.rbs +17 -0
  42. data/sig/steep/ast/annotation/collection.rbs +75 -0
  43. data/sig/steep/ast/annotation.rbs +126 -0
  44. data/sig/steep/ast/builtin.rbs +69 -0
  45. data/sig/steep/ast/type_params.rbs +11 -0
  46. data/sig/steep/ast/types/any.rbs +29 -0
  47. data/sig/steep/ast/types/boolean.rbs +31 -0
  48. data/sig/steep/ast/types/bot.rbs +29 -0
  49. data/sig/steep/ast/types/class.rbs +29 -0
  50. data/sig/steep/ast/types/factory.rbs +76 -0
  51. data/sig/steep/ast/types/helper.rbs +19 -0
  52. data/sig/steep/ast/types/instance.rbs +29 -0
  53. data/sig/steep/ast/types/intersection.rbs +35 -0
  54. data/sig/steep/ast/types/literal.rbs +33 -0
  55. data/sig/steep/ast/types/logic.rbs +78 -0
  56. data/sig/steep/ast/types/name.rbs +71 -0
  57. data/sig/steep/ast/types/nil.rbs +31 -0
  58. data/sig/steep/ast/types/proc.rbs +46 -0
  59. data/sig/steep/ast/types/record.rbs +38 -0
  60. data/sig/steep/ast/types/self.rbs +29 -0
  61. data/sig/steep/ast/types/top.rbs +29 -0
  62. data/sig/steep/ast/types/tuple.rbs +34 -0
  63. data/sig/steep/ast/types/union.rbs +38 -0
  64. data/sig/steep/ast/types/var.rbs +37 -0
  65. data/sig/steep/ast/types/void.rbs +29 -0
  66. data/sig/steep/ast/types.rbs +37 -0
  67. data/sig/steep/diagnostic/deprecated/unknown_constant_assigned.rbs +15 -0
  68. data/sig/steep/diagnostic/helper.rbs +9 -0
  69. data/sig/steep/diagnostic/lsp_formatter.rbs +29 -0
  70. data/sig/steep/diagnostic/ruby.rbs +494 -0
  71. data/sig/steep/diagnostic/signature.rbs +215 -0
  72. data/sig/steep/interface/block.rbs +35 -0
  73. data/sig/steep/interface/function.rbs +253 -0
  74. data/sig/steep/interface/interface.rbs +23 -0
  75. data/sig/steep/interface/method_type.rbs +55 -0
  76. data/sig/steep/interface/substitution.rbs +53 -0
  77. data/sig/steep/interface/type_param.rbs +35 -0
  78. data/sig/steep/method_name.rbs +26 -0
  79. data/sig/steep/module_helper.rbs +7 -0
  80. data/sig/steep/node_helper.rbs +11 -0
  81. data/sig/steep/project/dsl.rbs +94 -0
  82. data/sig/steep/project/options.rbs +15 -0
  83. data/sig/steep/project/pattern.rbs +25 -0
  84. data/sig/steep/project/target.rbs +25 -0
  85. data/sig/steep/project.rbs +19 -0
  86. data/sig/steep/services/completion_provider.rbs +123 -0
  87. data/sig/steep/services/content_change.rbs +35 -0
  88. data/sig/steep/services/file_loader.rbs +13 -0
  89. data/sig/steep/services/goto_service.rbs +45 -0
  90. data/sig/steep/services/hover_provider/rbs.rbs +21 -0
  91. data/sig/steep/services/hover_provider/ruby.rbs +109 -0
  92. data/sig/steep/services/hover_provider/singleton_methods.rbs +11 -0
  93. data/sig/steep/services/path_assignment.rbs +21 -0
  94. data/sig/steep/services/signature_service.rbs +91 -0
  95. data/sig/steep/services/stats_calculator.rbs +17 -0
  96. data/sig/steep/services/type_check_service.rbs +93 -0
  97. data/sig/steep/source.rbs +55 -0
  98. data/sig/steep/subtyping/cache.rbs +17 -0
  99. data/sig/steep/subtyping/check.rbs +93 -0
  100. data/sig/steep/subtyping/constraints.rbs +111 -0
  101. data/sig/steep/subtyping/relation.rbs +51 -0
  102. data/sig/steep/subtyping/result.rbs +157 -0
  103. data/sig/steep/subtyping/variable_variance.rbs +23 -0
  104. data/sig/steep/type_construction.rbs +285 -0
  105. data/sig/steep/type_inference/block_params.rbs +52 -0
  106. data/sig/steep/type_inference/constant_env.rbs +27 -0
  107. data/sig/steep/type_inference/context.rbs +137 -0
  108. data/sig/steep/type_inference/logic_type_interpreter.rbs +72 -0
  109. data/sig/steep/type_inference/method_call.rbs +124 -0
  110. data/sig/steep/type_inference/method_params.rbs +104 -0
  111. data/sig/steep/type_inference/multiple_assignment.rbs +76 -0
  112. data/sig/steep/type_inference/type_env.rbs +158 -0
  113. data/sig/steep/type_inference/type_env_builder.rbs +77 -0
  114. data/sig/steep/typing.rbs +68 -0
  115. data/sig/steep.rbs +31 -0
  116. data/smoke/class/f.rb +1 -0
  117. data/smoke/class/test_expectations.yml +2 -2
  118. data/smoke/diagnostics/test_expectations.yml +4 -2
  119. metadata +90 -6
  120. data/lib/steep/type_inference/local_variable_type_env.rb +0 -249
  121. 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.2
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-07-19 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
@@ -156,6 +156,8 @@ files:
156
156
  - CHANGELOG.md
157
157
  - Gemfile
158
158
  - Gemfile.lock
159
+ - Gemfile.steep
160
+ - Gemfile.steep.lock
159
161
  - LICENSE
160
162
  - README.md
161
163
  - Rakefile
@@ -164,6 +166,7 @@ files:
164
166
  - bin/output_rebaseline.rb
165
167
  - bin/output_test.rb
166
168
  - bin/setup
169
+ - bin/steep
167
170
  - bin/steep-prof
168
171
  - exe/steep
169
172
  - lib/steep.rb
@@ -225,6 +228,7 @@ files:
225
228
  - lib/steep/interface/type_param.rb
226
229
  - lib/steep/method_name.rb
227
230
  - lib/steep/module_helper.rb
231
+ - lib/steep/node_helper.rb
228
232
  - lib/steep/path_helper.rb
229
233
  - lib/steep/project.rb
230
234
  - lib/steep/project/dsl.rb
@@ -251,6 +255,7 @@ files:
251
255
  - lib/steep/services/stats_calculator.rb
252
256
  - lib/steep/services/type_check_service.rb
253
257
  - lib/steep/shims/filter_map.rb
258
+ - lib/steep/shims/symbol_start_with.rb
254
259
  - lib/steep/signature/validator.rb
255
260
  - lib/steep/source.rb
256
261
  - lib/steep/subtyping/cache.rb
@@ -264,21 +269,100 @@ files:
264
269
  - lib/steep/type_inference/constant_env.rb
265
270
  - lib/steep/type_inference/context.rb
266
271
  - lib/steep/type_inference/context_array.rb
267
- - lib/steep/type_inference/local_variable_type_env.rb
268
- - lib/steep/type_inference/logic.rb
269
272
  - lib/steep/type_inference/logic_type_interpreter.rb
270
273
  - lib/steep/type_inference/method_call.rb
271
274
  - lib/steep/type_inference/method_params.rb
275
+ - lib/steep/type_inference/multiple_assignment.rb
272
276
  - lib/steep/type_inference/send_args.rb
273
277
  - lib/steep/type_inference/type_env.rb
278
+ - lib/steep/type_inference/type_env_builder.rb
274
279
  - lib/steep/typing.rb
275
280
  - lib/steep/version.rb
276
281
  - manual/annotations.md
282
+ - rbs_collection.steep.lock.yaml
283
+ - rbs_collection.steep.yaml
277
284
  - sample/Steepfile
278
285
  - sample/lib/conference.rb
279
286
  - sample/lib/length.rb
280
287
  - sample/sig/conference.rbs
281
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
282
366
  - smoke/alias/Steepfile
283
367
  - smoke/alias/a.rb
284
368
  - smoke/alias/a.rbs
@@ -545,9 +629,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
545
629
  version: 2.6.0
546
630
  required_rubygems_version: !ruby/object:Gem::Requirement
547
631
  requirements:
548
- - - ">="
632
+ - - ">"
549
633
  - !ruby/object:Gem::Version
550
- version: '0'
634
+ version: 1.3.1
551
635
  requirements: []
552
636
  rubygems_version: 3.3.7
553
637
  signing_key:
@@ -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