steep-activesupport-4 1.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.gitmodules +0 -0
  4. data/CHANGELOG.md +1032 -0
  5. data/LICENSE +21 -0
  6. data/README.md +260 -0
  7. data/Rakefile +227 -0
  8. data/Steepfile +68 -0
  9. data/bin/console +14 -0
  10. data/bin/generate-diagnostics-docs.rb +112 -0
  11. data/bin/mem_graph.rb +67 -0
  12. data/bin/mem_prof.rb +102 -0
  13. data/bin/output_rebaseline.rb +34 -0
  14. data/bin/output_test.rb +60 -0
  15. data/bin/rbs +20 -0
  16. data/bin/rbs-inline +19 -0
  17. data/bin/setup +9 -0
  18. data/bin/stackprof_test.rb +19 -0
  19. data/bin/steep +19 -0
  20. data/bin/steep-check.rb +251 -0
  21. data/bin/steep-prof +16 -0
  22. data/doc/narrowing.md +195 -0
  23. data/doc/shape.md +194 -0
  24. data/exe/steep +18 -0
  25. data/guides/README.md +5 -0
  26. data/guides/src/gem-rbs-collection/gem-rbs-collection.md +126 -0
  27. data/guides/src/getting-started/getting-started.md +163 -0
  28. data/guides/src/nil-optional/nil-optional.md +195 -0
  29. data/lib/steep/annotation_parser.rb +199 -0
  30. data/lib/steep/ast/annotation/collection.rb +172 -0
  31. data/lib/steep/ast/annotation.rb +137 -0
  32. data/lib/steep/ast/builtin.rb +104 -0
  33. data/lib/steep/ast/ignore.rb +148 -0
  34. data/lib/steep/ast/node/type_application.rb +88 -0
  35. data/lib/steep/ast/node/type_assertion.rb +81 -0
  36. data/lib/steep/ast/types/any.rb +35 -0
  37. data/lib/steep/ast/types/boolean.rb +45 -0
  38. data/lib/steep/ast/types/bot.rb +35 -0
  39. data/lib/steep/ast/types/class.rb +43 -0
  40. data/lib/steep/ast/types/factory.rb +557 -0
  41. data/lib/steep/ast/types/helper.rb +40 -0
  42. data/lib/steep/ast/types/instance.rb +42 -0
  43. data/lib/steep/ast/types/intersection.rb +93 -0
  44. data/lib/steep/ast/types/literal.rb +59 -0
  45. data/lib/steep/ast/types/logic.rb +84 -0
  46. data/lib/steep/ast/types/name.rb +128 -0
  47. data/lib/steep/ast/types/nil.rb +41 -0
  48. data/lib/steep/ast/types/proc.rb +117 -0
  49. data/lib/steep/ast/types/record.rb +79 -0
  50. data/lib/steep/ast/types/self.rb +43 -0
  51. data/lib/steep/ast/types/shared_instance.rb +11 -0
  52. data/lib/steep/ast/types/top.rb +35 -0
  53. data/lib/steep/ast/types/tuple.rb +60 -0
  54. data/lib/steep/ast/types/union.rb +97 -0
  55. data/lib/steep/ast/types/var.rb +65 -0
  56. data/lib/steep/ast/types/void.rb +35 -0
  57. data/lib/steep/cli.rb +401 -0
  58. data/lib/steep/diagnostic/deprecated/else_on_exhaustive_case.rb +20 -0
  59. data/lib/steep/diagnostic/deprecated/unknown_constant_assigned.rb +28 -0
  60. data/lib/steep/diagnostic/helper.rb +18 -0
  61. data/lib/steep/diagnostic/lsp_formatter.rb +78 -0
  62. data/lib/steep/diagnostic/result_printer2.rb +48 -0
  63. data/lib/steep/diagnostic/ruby.rb +1221 -0
  64. data/lib/steep/diagnostic/signature.rb +570 -0
  65. data/lib/steep/drivers/annotations.rb +52 -0
  66. data/lib/steep/drivers/check.rb +339 -0
  67. data/lib/steep/drivers/checkfile.rb +210 -0
  68. data/lib/steep/drivers/diagnostic_printer.rb +105 -0
  69. data/lib/steep/drivers/init.rb +66 -0
  70. data/lib/steep/drivers/langserver.rb +56 -0
  71. data/lib/steep/drivers/print_project.rb +113 -0
  72. data/lib/steep/drivers/stats.rb +203 -0
  73. data/lib/steep/drivers/utils/driver_helper.rb +143 -0
  74. data/lib/steep/drivers/utils/jobs_option.rb +26 -0
  75. data/lib/steep/drivers/vendor.rb +27 -0
  76. data/lib/steep/drivers/watch.rb +194 -0
  77. data/lib/steep/drivers/worker.rb +58 -0
  78. data/lib/steep/equatable.rb +23 -0
  79. data/lib/steep/expectations.rb +228 -0
  80. data/lib/steep/index/rbs_index.rb +350 -0
  81. data/lib/steep/index/signature_symbol_provider.rb +185 -0
  82. data/lib/steep/index/source_index.rb +167 -0
  83. data/lib/steep/interface/block.rb +103 -0
  84. data/lib/steep/interface/builder.rb +843 -0
  85. data/lib/steep/interface/function.rb +1090 -0
  86. data/lib/steep/interface/method_type.rb +330 -0
  87. data/lib/steep/interface/shape.rb +239 -0
  88. data/lib/steep/interface/substitution.rb +159 -0
  89. data/lib/steep/interface/type_param.rb +115 -0
  90. data/lib/steep/located_value.rb +20 -0
  91. data/lib/steep/method_name.rb +42 -0
  92. data/lib/steep/module_helper.rb +24 -0
  93. data/lib/steep/node_helper.rb +273 -0
  94. data/lib/steep/path_helper.rb +30 -0
  95. data/lib/steep/project/dsl.rb +268 -0
  96. data/lib/steep/project/group.rb +31 -0
  97. data/lib/steep/project/options.rb +63 -0
  98. data/lib/steep/project/pattern.rb +59 -0
  99. data/lib/steep/project/target.rb +92 -0
  100. data/lib/steep/project.rb +78 -0
  101. data/lib/steep/rake_task.rb +132 -0
  102. data/lib/steep/range_extension.rb +29 -0
  103. data/lib/steep/server/base_worker.rb +97 -0
  104. data/lib/steep/server/change_buffer.rb +73 -0
  105. data/lib/steep/server/custom_methods.rb +77 -0
  106. data/lib/steep/server/delay_queue.rb +45 -0
  107. data/lib/steep/server/interaction_worker.rb +492 -0
  108. data/lib/steep/server/lsp_formatter.rb +455 -0
  109. data/lib/steep/server/master.rb +912 -0
  110. data/lib/steep/server/target_group_files.rb +205 -0
  111. data/lib/steep/server/type_check_controller.rb +366 -0
  112. data/lib/steep/server/type_check_worker.rb +303 -0
  113. data/lib/steep/server/work_done_progress.rb +64 -0
  114. data/lib/steep/server/worker_process.rb +176 -0
  115. data/lib/steep/services/completion_provider.rb +802 -0
  116. data/lib/steep/services/content_change.rb +61 -0
  117. data/lib/steep/services/file_loader.rb +74 -0
  118. data/lib/steep/services/goto_service.rb +441 -0
  119. data/lib/steep/services/hover_provider/rbs.rb +88 -0
  120. data/lib/steep/services/hover_provider/ruby.rb +221 -0
  121. data/lib/steep/services/hover_provider/singleton_methods.rb +20 -0
  122. data/lib/steep/services/path_assignment.rb +46 -0
  123. data/lib/steep/services/signature_help_provider.rb +202 -0
  124. data/lib/steep/services/signature_service.rb +428 -0
  125. data/lib/steep/services/stats_calculator.rb +68 -0
  126. data/lib/steep/services/type_check_service.rb +394 -0
  127. data/lib/steep/services/type_name_completion.rb +236 -0
  128. data/lib/steep/signature/validator.rb +651 -0
  129. data/lib/steep/source/ignore_ranges.rb +69 -0
  130. data/lib/steep/source.rb +691 -0
  131. data/lib/steep/subtyping/cache.rb +30 -0
  132. data/lib/steep/subtyping/check.rb +1113 -0
  133. data/lib/steep/subtyping/constraints.rb +341 -0
  134. data/lib/steep/subtyping/relation.rb +101 -0
  135. data/lib/steep/subtyping/result.rb +324 -0
  136. data/lib/steep/subtyping/variable_variance.rb +89 -0
  137. data/lib/steep/test.rb +9 -0
  138. data/lib/steep/thread_waiter.rb +43 -0
  139. data/lib/steep/type_construction.rb +5183 -0
  140. data/lib/steep/type_inference/block_params.rb +416 -0
  141. data/lib/steep/type_inference/case_when.rb +303 -0
  142. data/lib/steep/type_inference/constant_env.rb +56 -0
  143. data/lib/steep/type_inference/context.rb +195 -0
  144. data/lib/steep/type_inference/logic_type_interpreter.rb +613 -0
  145. data/lib/steep/type_inference/method_call.rb +193 -0
  146. data/lib/steep/type_inference/method_params.rb +531 -0
  147. data/lib/steep/type_inference/multiple_assignment.rb +194 -0
  148. data/lib/steep/type_inference/send_args.rb +712 -0
  149. data/lib/steep/type_inference/type_env.rb +341 -0
  150. data/lib/steep/type_inference/type_env_builder.rb +138 -0
  151. data/lib/steep/typing.rb +321 -0
  152. data/lib/steep/version.rb +3 -0
  153. data/lib/steep.rb +369 -0
  154. data/manual/annotations.md +181 -0
  155. data/manual/ignore.md +20 -0
  156. data/manual/ruby-diagnostics.md +1879 -0
  157. data/sample/Steepfile +22 -0
  158. data/sample/lib/conference.rb +49 -0
  159. data/sample/lib/length.rb +35 -0
  160. data/sample/sig/conference.rbs +42 -0
  161. data/sample/sig/generics.rbs +15 -0
  162. data/sample/sig/length.rbs +34 -0
  163. data/steep-activesupport-4.gemspec +55 -0
  164. metadata +437 -0
@@ -0,0 +1,557 @@
1
+ module Steep
2
+ module AST
3
+ module Types
4
+ class Factory
5
+ attr_reader :definition_builder
6
+
7
+ attr_reader :type_cache
8
+
9
+ def inspect
10
+ s = "#<%s:%#018x " % [self.class, object_id]
11
+ s << "@definition_builder=#<%s:%#018x>" % [definition_builder.class, definition_builder.object_id]
12
+ s + ">"
13
+ end
14
+
15
+ def initialize(builder:)
16
+ @definition_builder = builder
17
+
18
+ @type_cache = {}
19
+ @method_type_cache = {}
20
+ @method_type_cache.compare_by_identity
21
+ end
22
+
23
+ def type_name_resolver
24
+ @type_name_resolver ||= RBS::Resolver::TypeNameResolver.new(definition_builder.env)
25
+ end
26
+
27
+ def type_opt(type)
28
+ if type
29
+ type(type)
30
+ end
31
+ end
32
+
33
+ def type_1_opt(type)
34
+ if type
35
+ type_1(type)
36
+ end
37
+ end
38
+
39
+ def normalize_args(type_name, args)
40
+ case
41
+ when type_name.class?
42
+ if entry = env.normalized_module_class_entry(type_name)
43
+ type_params = entry.type_params
44
+ end
45
+ when type_name.interface?
46
+ if entry = env.interface_decls.fetch(type_name, nil)
47
+ type_params = entry.decl.type_params
48
+ end
49
+ when type_name.alias?
50
+ if entry = env.type_alias_decls.fetch(type_name, nil)
51
+ type_params = entry.decl.type_params
52
+ end
53
+ end
54
+
55
+ if type_params && !type_params.empty?
56
+ RBS::AST::TypeParam.normalize_args(type_params, args)
57
+ else
58
+ args
59
+ end
60
+ end
61
+
62
+ def type(type)
63
+ if ty = type_cache[type]
64
+ return ty
65
+ end
66
+
67
+ type_cache[type] =
68
+ case type
69
+ when RBS::Types::Bases::Any
70
+ Any.instance
71
+ when RBS::Types::Bases::Class
72
+ Class.instance
73
+ when RBS::Types::Bases::Instance
74
+ Instance.instance
75
+ when RBS::Types::Bases::Self
76
+ Self.instance
77
+ when RBS::Types::Bases::Top
78
+ Top.instance
79
+ when RBS::Types::Bases::Bottom
80
+ Bot.instance
81
+ when RBS::Types::Bases::Bool
82
+ Boolean.instance
83
+ when RBS::Types::Bases::Void
84
+ Void.instance
85
+ when RBS::Types::Bases::Nil
86
+ Nil.instance
87
+ when RBS::Types::Variable
88
+ Var.new(name: type.name)
89
+ when RBS::Types::ClassSingleton
90
+ type_name = type.name
91
+ Name::Singleton.new(name: type_name)
92
+ when RBS::Types::ClassInstance
93
+ type_name = type.name
94
+ args = normalize_args(type_name, type.args).map {|arg| type(arg) }
95
+ Name::Instance.new(name: type_name, args: args)
96
+ when RBS::Types::Interface
97
+ type_name = type.name
98
+ args = normalize_args(type_name, type.args).map {|arg| type(arg) }
99
+ Name::Interface.new(name: type_name, args: args)
100
+ when RBS::Types::Alias
101
+ type_name = type.name
102
+ args = normalize_args(type_name, type.args).map {|arg| type(arg) }
103
+ Name::Alias.new(name: type_name, args: args)
104
+ when RBS::Types::Union
105
+ Union.build(types: type.types.map {|ty| type(ty) })
106
+ when RBS::Types::Intersection
107
+ Intersection.build(types: type.types.map {|ty| type(ty) })
108
+ when RBS::Types::Optional
109
+ Union.build(types: [type(type.type), Nil.instance()])
110
+ when RBS::Types::Literal
111
+ Literal.new(value: type.literal)
112
+ when RBS::Types::Tuple
113
+ Tuple.new(types: type.types.map {|ty| type(ty) })
114
+ when RBS::Types::Record
115
+ elements = {} #: Hash[Record::key, AST::Types::t]
116
+ required_keys = Set[] #: Set[Record::key]
117
+
118
+ type.all_fields.each do |key, (value, required)|
119
+ required_keys << key if required
120
+ elements[key] = type(value)
121
+ end
122
+
123
+ Record.new(elements: elements, required_keys: required_keys)
124
+ when RBS::Types::Proc
125
+ func = Interface::Function.new(
126
+ params: params(type.type),
127
+ return_type: type(type.type.return_type),
128
+ location: nil
129
+ )
130
+ block = if type.block
131
+ Interface::Block.new(
132
+ type: Interface::Function.new(
133
+ params: params(type.block.type),
134
+ return_type: type(type.block.type.return_type),
135
+ location: nil
136
+ ),
137
+ optional: !type.block.required,
138
+ self_type: type_opt(type.block.self_type)
139
+ )
140
+ end
141
+
142
+ Proc.new(
143
+ type: func,
144
+ block: block,
145
+ self_type: type_opt(type.self_type)
146
+ )
147
+ else
148
+ raise "Unexpected type given: #{type}"
149
+ end
150
+ end
151
+
152
+ def type_1(type)
153
+ case type
154
+ when Any
155
+ RBS::Types::Bases::Any.new(location: nil)
156
+ when Class
157
+ RBS::Types::Bases::Class.new(location: nil)
158
+ when Instance
159
+ RBS::Types::Bases::Instance.new(location: nil)
160
+ when Self
161
+ RBS::Types::Bases::Self.new(location: nil)
162
+ when Top
163
+ RBS::Types::Bases::Top.new(location: nil)
164
+ when Bot
165
+ RBS::Types::Bases::Bottom.new(location: nil)
166
+ when Boolean
167
+ RBS::Types::Bases::Bool.new(location: nil)
168
+ when Void
169
+ RBS::Types::Bases::Void.new(location: nil)
170
+ when Nil
171
+ RBS::Types::Bases::Nil.new(location: nil)
172
+ when Var
173
+ RBS::Types::Variable.new(name: type.name, location: nil)
174
+ when Name::Singleton
175
+ RBS::Types::ClassSingleton.new(name: type.name, location: nil)
176
+ when Name::Instance
177
+ RBS::Types::ClassInstance.new(
178
+ name: type.name,
179
+ args: type.args.map {|arg| type_1(arg) },
180
+ location: nil
181
+ )
182
+ when Name::Interface
183
+ RBS::Types::Interface.new(
184
+ name: type.name,
185
+ args: type.args.map {|arg| type_1(arg) },
186
+ location: nil
187
+ )
188
+ when Name::Alias
189
+ RBS::Types::Alias.new(
190
+ name: type.name,
191
+ args: type.args.map {|arg| type_1(arg) },
192
+ location: nil
193
+ )
194
+ when Union
195
+ RBS::Types::Union.new(
196
+ types: type.types.map {|ty| type_1(ty) },
197
+ location: nil
198
+ )
199
+ when Intersection
200
+ RBS::Types::Intersection.new(
201
+ types: type.types.map {|ty| type_1(ty) },
202
+ location: nil
203
+ )
204
+ when Literal
205
+ RBS::Types::Literal.new(literal: type.value, location: nil)
206
+ when Tuple
207
+ RBS::Types::Tuple.new(
208
+ types: type.types.map {|ty| type_1(ty) },
209
+ location: nil
210
+ )
211
+ when Record
212
+ all_fields = {} #: Hash[Symbol, [RBS::Types::t, bool]]
213
+ type.elements.each do |key, value|
214
+ raise unless key.is_a?(Symbol)
215
+ all_fields[key] = [type_1(value), type.required?(key)]
216
+ end
217
+ RBS::Types::Record.new(all_fields: all_fields, location: nil)
218
+ when Proc
219
+ block = if type.block
220
+ RBS::Types::Block.new(
221
+ type: function_1(type.block.type),
222
+ required: !type.block.optional?,
223
+ self_type: type_1_opt(type.block.self_type)
224
+ )
225
+ end
226
+ RBS::Types::Proc.new(
227
+ type: function_1(type.type),
228
+ self_type: type_1_opt(type.self_type),
229
+ block: block,
230
+ location: nil
231
+ )
232
+ when Logic::Base
233
+ RBS::Types::Bases::Bool.new(location: nil)
234
+ else
235
+ raise "Unexpected type given: #{type} (#{type.class})"
236
+ end
237
+ end
238
+
239
+ def function_1(func)
240
+ params = func.params
241
+ return_type = func.return_type
242
+
243
+ if params
244
+ RBS::Types::Function.new(
245
+ required_positionals: params.required.map {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
246
+ optional_positionals: params.optional.map {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
247
+ rest_positionals: params.rest&.yield_self {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
248
+ trailing_positionals: [],
249
+ required_keywords: params.required_keywords.transform_values {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
250
+ optional_keywords: params.optional_keywords.transform_values {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
251
+ rest_keywords: params.rest_keywords&.yield_self {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
252
+ return_type: type_1(return_type)
253
+ )
254
+ else
255
+ RBS::Types::UntypedFunction.new(return_type: type_1(return_type))
256
+ end
257
+ end
258
+
259
+ def params(type)
260
+ case type
261
+ when RBS::Types::Function
262
+ Interface::Function::Params.build(
263
+ required: type.required_positionals.map {|param| type(param.type) },
264
+ optional: type.optional_positionals.map {|param| type(param.type) },
265
+ rest: type.rest_positionals&.yield_self {|param| type(param.type) },
266
+ required_keywords: type.required_keywords.transform_values {|param| type(param.type) },
267
+ optional_keywords: type.optional_keywords.transform_values {|param| type(param.type) },
268
+ rest_keywords: type.rest_keywords&.yield_self {|param| type(param.type) }
269
+ )
270
+ when RBS::Types::UntypedFunction
271
+ nil
272
+ end
273
+ end
274
+
275
+ def type_param(type_param)
276
+ Interface::TypeParam.new(
277
+ name: type_param.name,
278
+ upper_bound: type_opt(type_param.upper_bound_type),
279
+ variance: type_param.variance,
280
+ unchecked: type_param.unchecked?,
281
+ default_type: type_opt(type_param.default_type)
282
+ )
283
+ end
284
+
285
+ def type_param_1(type_param)
286
+ RBS::AST::TypeParam.new(
287
+ name: type_param.name,
288
+ variance: type_param.variance,
289
+ upper_bound: type_param.upper_bound&.yield_self {|u|
290
+ case u_ = type_1(u)
291
+ when RBS::Types::ClassInstance, RBS::Types::ClassSingleton, RBS::Types::Interface
292
+ u_
293
+ else
294
+ raise "`#{u_}` cannot be type parameter upper bound"
295
+ end
296
+ },
297
+ location: type_param.location
298
+ ).unchecked!(type_param.unchecked)
299
+ end
300
+
301
+ def method_type(method_type)
302
+ @method_type_cache[method_type] ||=
303
+ Interface::MethodType.new(
304
+ type_params: method_type.type_params.map {|param| type_param(param) },
305
+ type: Interface::Function.new(
306
+ params: params(method_type.type),
307
+ return_type: type(method_type.type.return_type),
308
+ location: method_type.location
309
+ ),
310
+ block: method_type.block&.yield_self do |block|
311
+ Interface::Block.new(
312
+ optional: !block.required,
313
+ type: Interface::Function.new(
314
+ params: params(block.type),
315
+ return_type: type(block.type.return_type),
316
+ location: nil
317
+ ),
318
+ self_type: type_opt(block.self_type)
319
+ )
320
+ end
321
+ )
322
+ end
323
+
324
+ def method_type_1(method_type)
325
+ RBS::MethodType.new(
326
+ type_params: method_type.type_params.map {|param| type_param_1(param) },
327
+ type: function_1(method_type.type),
328
+ block: method_type.block&.yield_self do |block|
329
+ RBS::Types::Block.new(
330
+ type: function_1(block.type),
331
+ required: !block.optional,
332
+ self_type: type_1_opt(block.self_type)
333
+ )
334
+ end,
335
+ location: nil
336
+ )
337
+ end
338
+
339
+ def unfold(type_name, args)
340
+ type(
341
+ definition_builder.expand_alias2(
342
+ type_name,
343
+ args.empty? ? [] : args.map {|t| type_1(t) }
344
+ )
345
+ )
346
+ end
347
+
348
+ def expand_alias(type)
349
+ case type
350
+ when AST::Types::Name::Alias
351
+ unfold(type.name, type.args)
352
+ else
353
+ type
354
+ end
355
+ end
356
+
357
+ def deep_expand_alias(type, recursive: Set.new)
358
+ case type
359
+ when AST::Types::Name::Alias
360
+ unless recursive.member?(type.name)
361
+ unfolded = expand_alias(type)
362
+ deep_expand_alias(unfolded, recursive: recursive.union([type.name]))
363
+ end
364
+ when AST::Types::Union
365
+ types = type.types.map {|ty| deep_expand_alias(ty, recursive: recursive) or return }
366
+ AST::Types::Union.build(types: types)
367
+ when AST::Types::Intersection
368
+ types = type.types.map {|ty| deep_expand_alias(ty, recursive: recursive) or return }
369
+ AST::Types::Intersection.build(types: types)
370
+ else
371
+ type
372
+ end
373
+ end
374
+
375
+ def flatten_union(type, acc = [])
376
+ case type
377
+ when AST::Types::Union
378
+ type.types.each {|ty| flatten_union(ty, acc) }
379
+ else
380
+ acc << type
381
+ end
382
+
383
+ acc
384
+ end
385
+
386
+ def partition_union(type)
387
+ case type
388
+ when AST::Types::Name::Alias
389
+ unfold = expand_alias(type)
390
+ if unfold == type
391
+ [type, type]
392
+ else
393
+ partition_union(unfold)
394
+ end
395
+ when AST::Types::Union
396
+ truthy_types = [] #: Array[AST::Types::t]
397
+ falsy_types = [] #: Array[AST::Types::t]
398
+
399
+ type.types.each do |type|
400
+ truthy, falsy = partition_union(type)
401
+
402
+ truthy_types << truthy if truthy
403
+ falsy_types << falsy if falsy
404
+ end
405
+
406
+ [
407
+ truthy_types.empty? ? nil : AST::Types::Union.build(types: truthy_types),
408
+ falsy_types.empty? ? nil : AST::Types::Union.build(types: falsy_types)
409
+ ]
410
+ when AST::Types::Any, AST::Types::Boolean, AST::Types::Top, AST::Types::Logic::Base
411
+ [type, type]
412
+ when AST::Types::Bot, AST::Types::Void
413
+ [nil, nil]
414
+ when AST::Types::Nil
415
+ [nil, type]
416
+ when AST::Types::Literal
417
+ if type.value == false
418
+ [nil, type]
419
+ else
420
+ [type, nil]
421
+ end
422
+ else
423
+ [type, nil]
424
+ end
425
+ end
426
+
427
+ def unwrap_optional(type)
428
+ case type
429
+ when AST::Types::Union
430
+ unwrap = type.types.filter_map do |type|
431
+ unless type.is_a?(AST::Types::Nil)
432
+ type
433
+ end
434
+ end
435
+
436
+ unless unwrap.empty?
437
+ AST::Types::Union.build(types: unwrap)
438
+ end
439
+ when AST::Types::Nil
440
+ nil
441
+ when AST::Types::Name::Alias
442
+ type_ = expand_alias(type)
443
+ if type_ == type
444
+ type_
445
+ else
446
+ unwrap_optional(type_)
447
+ end
448
+ else
449
+ type
450
+ end
451
+ end
452
+
453
+ def module_name?(type_name)
454
+ env.module_entry(type_name) ? true : false
455
+ end
456
+
457
+ def class_name?(type_name)
458
+ env.class_entry(type_name) ? true : false
459
+ end
460
+
461
+ def env
462
+ definition_builder.env
463
+ end
464
+
465
+ def absolute_type(type, context:)
466
+ absolute_type = type_1(type).map_type_name do |name|
467
+ absolute_type_name(name, context: context) || name.absolute!
468
+ end
469
+ type(absolute_type)
470
+ end
471
+
472
+ def absolute_type_name(type_name, context:)
473
+ type_name_resolver.resolve(type_name, context: context)
474
+ end
475
+
476
+ def instance_type(type_name, args: nil)
477
+ raise unless type_name.class?
478
+
479
+ definition = definition_builder.build_singleton(type_name)
480
+ def_args = definition.type_params.map { Any.instance }
481
+
482
+ if args
483
+ raise if def_args.size != args.size
484
+ else
485
+ args = def_args
486
+ end
487
+
488
+ AST::Types::Name::Instance.new(name: type_name, args: args)
489
+ end
490
+
491
+ def try_instance_type(type)
492
+ case type
493
+ when AST::Types::Name::Instance
494
+ instance_type(type.name)
495
+ when AST::Types::Name::Singleton
496
+ instance_type(type.name)
497
+ else
498
+ nil
499
+ end
500
+ end
501
+
502
+ def try_singleton_type(type)
503
+ case type
504
+ when AST::Types::Name::Instance, AST::Types::Name::Singleton
505
+ AST::Types::Name::Singleton.new(name:type.name)
506
+ else
507
+ nil
508
+ end
509
+ end
510
+
511
+ def normalize_type(type)
512
+ case type
513
+ when AST::Types::Name::Instance
514
+ AST::Types::Name::Instance.new(
515
+ name: env.normalize_module_name(type.name),
516
+ args: type.args.map {|ty| normalize_type(ty) }
517
+ )
518
+ when AST::Types::Name::Singleton
519
+ AST::Types::Name::Singleton.new(
520
+ name: env.normalize_module_name(type.name)
521
+ )
522
+ when AST::Types::Any, AST::Types::Boolean, AST::Types::Bot, AST::Types::Nil,
523
+ AST::Types::Top, AST::Types::Void, AST::Types::Literal, AST::Types::Class, AST::Types::Instance,
524
+ AST::Types::Self, AST::Types::Var, AST::Types::Logic::Base
525
+ type
526
+ when AST::Types::Intersection
527
+ AST::Types::Intersection.build(
528
+ types: type.types.map {|type| normalize_type(type) }
529
+ )
530
+ when AST::Types::Union
531
+ AST::Types::Union.build(
532
+ types: type.types.map {|type| normalize_type(type) }
533
+ )
534
+ when AST::Types::Record
535
+ type.map_type {|type| normalize_type(type) }
536
+ when AST::Types::Tuple
537
+ AST::Types::Tuple.new(
538
+ types: type.types.map {|type| normalize_type(type) }
539
+ )
540
+ when AST::Types::Proc
541
+ type.map_type {|type| normalize_type(type) }
542
+ when AST::Types::Name::Alias
543
+ AST::Types::Name::Alias.new(
544
+ name: type.name,
545
+ args: type.args.map {|ty| normalize_type(ty) }
546
+ )
547
+ when AST::Types::Name::Interface
548
+ AST::Types::Name::Interface.new(
549
+ name: type.name,
550
+ args: type.args.map {|ty| normalize_type(ty) }
551
+ )
552
+ end
553
+ end
554
+ end
555
+ end
556
+ end
557
+ end
@@ -0,0 +1,40 @@
1
+ module Steep
2
+ module AST
3
+ module Types
4
+ module Helper
5
+ module ChildrenLevel
6
+ def level_of_children(children)
7
+ levels = children.map(&:level)
8
+ children.map(&:level).sort {|a, b| (b.size <=> a.size) || 0 }.inject() do |a, b|
9
+ a.zip(b).map do |x, y|
10
+ if x && y
11
+ x + y
12
+ else
13
+ x || y || raise
14
+ end
15
+ end
16
+ end || []
17
+ end
18
+ end
19
+
20
+ module NoFreeVariables
21
+ def free_variables()
22
+ @fvs ||= Set.new
23
+ end
24
+ end
25
+
26
+ module NoChild
27
+ def each_child(&block)
28
+ unless block
29
+ enum_for :each_child
30
+ end
31
+ end
32
+
33
+ def map_type
34
+ self
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,42 @@
1
+ module Steep
2
+ module AST
3
+ module Types
4
+ class Instance
5
+ extend SharedInstance
6
+
7
+ def ==(other)
8
+ other.is_a?(Instance)
9
+ end
10
+
11
+ def hash
12
+ self.class.hash
13
+ end
14
+
15
+ alias eql? ==
16
+
17
+ def subst(s)
18
+ if s.instance_type
19
+ s.instance_type
20
+ else
21
+ self
22
+ end
23
+ end
24
+
25
+ @@fvs = Set[instance]
26
+ def free_variables
27
+ @@fvs
28
+ end
29
+
30
+ include Helper::NoChild
31
+
32
+ def to_s
33
+ "instance"
34
+ end
35
+
36
+ def level
37
+ [0]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end