steep 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/exe/rbs +1 -1
  4. data/lib/steep/annotation_parser.rb +4 -4
  5. data/lib/steep/ast/buffer.rb +11 -7
  6. data/lib/steep/ast/builtin.rb +8 -0
  7. data/lib/steep/ast/types/factory.rb +55 -55
  8. data/lib/steep/drivers/check.rb +20 -4
  9. data/lib/steep/drivers/langserver.rb +6 -1
  10. data/lib/steep/drivers/vendor.rb +2 -2
  11. data/lib/steep/project/completion_provider.rb +5 -11
  12. data/lib/steep/project/dsl.rb +14 -0
  13. data/lib/steep/project/file.rb +42 -46
  14. data/lib/steep/project/hover_content.rb +11 -5
  15. data/lib/steep/project/options.rb +25 -3
  16. data/lib/steep/project/target.rb +10 -4
  17. data/lib/steep/signature/errors.rb +1 -1
  18. data/lib/steep/signature/validator.rb +8 -8
  19. data/lib/steep/source.rb +1 -1
  20. data/lib/steep/type_construction.rb +987 -711
  21. data/lib/steep/type_inference/constant_env.rb +1 -1
  22. data/lib/steep/type_inference/context.rb +7 -3
  23. data/lib/steep/type_inference/context_array.rb +111 -0
  24. data/lib/steep/type_inference/local_variable_type_env.rb +226 -0
  25. data/lib/steep/type_inference/logic.rb +130 -0
  26. data/lib/steep/type_inference/type_env.rb +5 -69
  27. data/lib/steep/typing.rb +79 -22
  28. data/lib/steep/version.rb +1 -1
  29. data/lib/steep.rb +6 -1
  30. data/smoke/alias/Steepfile +1 -0
  31. data/smoke/and/Steepfile +1 -0
  32. data/smoke/array/Steepfile +1 -0
  33. data/smoke/array/b.rb +0 -2
  34. data/smoke/block/Steepfile +1 -0
  35. data/smoke/case/Steepfile +1 -0
  36. data/smoke/class/Steepfile +1 -0
  37. data/smoke/const/Steepfile +1 -0
  38. data/smoke/dstr/Steepfile +1 -0
  39. data/smoke/ensure/Steepfile +1 -0
  40. data/smoke/enumerator/Steepfile +1 -0
  41. data/smoke/extension/Steepfile +1 -0
  42. data/smoke/extension/c.rb +1 -0
  43. data/smoke/hash/Steepfile +1 -0
  44. data/smoke/hello/Steepfile +1 -0
  45. data/smoke/if/Steepfile +1 -0
  46. data/smoke/if/a.rb +1 -1
  47. data/smoke/implements/Steepfile +1 -0
  48. data/smoke/initialize/Steepfile +1 -0
  49. data/smoke/integer/Steepfile +1 -0
  50. data/smoke/interface/Steepfile +1 -0
  51. data/smoke/kwbegin/Steepfile +1 -0
  52. data/smoke/lambda/Steepfile +1 -0
  53. data/smoke/literal/Steepfile +1 -0
  54. data/smoke/map/Steepfile +1 -0
  55. data/smoke/method/Steepfile +1 -0
  56. data/smoke/module/Steepfile +1 -0
  57. data/smoke/regexp/Steepfile +1 -0
  58. data/smoke/regression/Steepfile +1 -0
  59. data/smoke/rescue/Steepfile +1 -0
  60. data/smoke/rescue/a.rb +1 -1
  61. data/smoke/self/Steepfile +1 -0
  62. data/smoke/skip/Steepfile +1 -0
  63. data/smoke/stdout/Steepfile +1 -0
  64. data/smoke/super/Steepfile +1 -0
  65. data/smoke/type_case/Steepfile +1 -0
  66. data/smoke/yield/Steepfile +1 -0
  67. data/steep.gemspec +1 -1
  68. data/vendor/ruby-signature/.gitignore +2 -2
  69. data/vendor/ruby-signature/README.md +2 -2
  70. data/vendor/ruby-signature/Rakefile +2 -2
  71. data/vendor/ruby-signature/bin/annotate-with-rdoc +14 -13
  72. data/vendor/ruby-signature/bin/console +1 -1
  73. data/vendor/ruby-signature/bin/sort +7 -6
  74. data/vendor/ruby-signature/bin/test_runner.rb +0 -1
  75. data/vendor/ruby-signature/docs/CONTRIBUTING.md +1 -1
  76. data/vendor/ruby-signature/docs/sigs.md +3 -3
  77. data/vendor/ruby-signature/docs/stdlib.md +1 -1
  78. data/vendor/ruby-signature/docs/syntax.md +9 -9
  79. data/vendor/ruby-signature/exe/rbs +5 -1
  80. data/vendor/ruby-signature/lib/rbs/ast/annotation.rb +27 -0
  81. data/vendor/ruby-signature/lib/rbs/ast/comment.rb +27 -0
  82. data/vendor/ruby-signature/lib/rbs/ast/declarations.rb +395 -0
  83. data/vendor/ruby-signature/lib/rbs/ast/members.rb +362 -0
  84. data/vendor/ruby-signature/lib/rbs/buffer.rb +50 -0
  85. data/vendor/ruby-signature/lib/rbs/builtin_names.rb +55 -0
  86. data/vendor/ruby-signature/lib/rbs/cli.rb +558 -0
  87. data/vendor/ruby-signature/lib/rbs/constant.rb +26 -0
  88. data/vendor/ruby-signature/lib/rbs/constant_table.rb +150 -0
  89. data/vendor/ruby-signature/lib/rbs/definition.rb +170 -0
  90. data/vendor/ruby-signature/lib/rbs/definition_builder.rb +919 -0
  91. data/vendor/ruby-signature/lib/rbs/environment.rb +281 -0
  92. data/vendor/ruby-signature/lib/rbs/environment_loader.rb +136 -0
  93. data/vendor/ruby-signature/lib/rbs/environment_walker.rb +124 -0
  94. data/vendor/ruby-signature/lib/rbs/errors.rb +187 -0
  95. data/vendor/ruby-signature/lib/rbs/location.rb +102 -0
  96. data/vendor/ruby-signature/lib/rbs/method_type.rb +123 -0
  97. data/vendor/ruby-signature/lib/rbs/namespace.rb +91 -0
  98. data/vendor/ruby-signature/lib/{ruby/signature → rbs}/parser.rb +21 -23
  99. data/vendor/ruby-signature/lib/{ruby/signature → rbs}/parser.y +18 -18
  100. data/vendor/ruby-signature/lib/rbs/prototype/rb.rb +553 -0
  101. data/vendor/ruby-signature/lib/rbs/prototype/rbi.rb +587 -0
  102. data/vendor/ruby-signature/lib/rbs/prototype/runtime.rb +381 -0
  103. data/vendor/ruby-signature/lib/rbs/substitution.rb +46 -0
  104. data/vendor/ruby-signature/lib/rbs/test/errors.rb +61 -0
  105. data/vendor/ruby-signature/lib/rbs/test/hook.rb +294 -0
  106. data/vendor/ruby-signature/lib/{ruby/signature → rbs}/test/setup.rb +7 -7
  107. data/vendor/ruby-signature/lib/rbs/test/spy.rb +325 -0
  108. data/vendor/ruby-signature/lib/rbs/test/test_helper.rb +183 -0
  109. data/vendor/ruby-signature/lib/rbs/test/type_check.rb +254 -0
  110. data/vendor/ruby-signature/lib/rbs/test.rb +26 -0
  111. data/vendor/ruby-signature/lib/rbs/type_name.rb +70 -0
  112. data/vendor/ruby-signature/lib/rbs/types.rb +936 -0
  113. data/vendor/ruby-signature/lib/rbs/variance_calculator.rb +138 -0
  114. data/vendor/ruby-signature/lib/rbs/vendorer.rb +47 -0
  115. data/vendor/ruby-signature/lib/rbs/version.rb +3 -0
  116. data/vendor/ruby-signature/lib/rbs/writer.rb +269 -0
  117. data/vendor/ruby-signature/lib/rbs.rb +64 -0
  118. data/vendor/ruby-signature/lib/ruby/signature.rb +4 -61
  119. data/vendor/ruby-signature/{ruby-signature.gemspec → rbs.gemspec} +4 -4
  120. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +58 -1
  121. data/vendor/ruby-signature/stdlib/base64/base64.rbs +69 -13
  122. data/vendor/ruby-signature/stdlib/benchmark/benchmark.rbs +372 -0
  123. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +9 -0
  124. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +1 -7
  125. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +2 -1
  126. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +3 -2
  127. data/vendor/ruby-signature/stdlib/builtin/file.rbs +902 -302
  128. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +190 -68
  129. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +3 -6
  130. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +6 -4
  131. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +146 -120
  132. data/vendor/ruby-signature/stdlib/builtin/math.rbs +310 -7
  133. data/vendor/ruby-signature/stdlib/builtin/method.rbs +11 -8
  134. data/vendor/ruby-signature/stdlib/builtin/module.rbs +959 -103
  135. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +3 -0
  136. data/vendor/ruby-signature/stdlib/builtin/process.rbs +981 -108
  137. data/vendor/ruby-signature/stdlib/builtin/random.rbs +215 -41
  138. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +47 -0
  139. data/vendor/ruby-signature/stdlib/builtin/string.rbs +9 -2
  140. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +282 -11
  141. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +11 -13
  142. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +25 -29
  143. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +1 -1
  144. data/vendor/ruby-signature/stdlib/builtin/time.rbs +875 -567
  145. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +243 -44
  146. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +103 -109
  147. data/vendor/ruby-signature/stdlib/coverage/coverage.rbs +62 -0
  148. data/vendor/ruby-signature/stdlib/csv/csv.rbs +773 -0
  149. data/vendor/ruby-signature/stdlib/erb/erb.rbs +375 -1
  150. data/vendor/ruby-signature/stdlib/find/find.rbs +0 -4
  151. data/vendor/ruby-signature/stdlib/ipaddr/ipaddr.rbs +247 -0
  152. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +1088 -16
  153. data/vendor/ruby-signature/stdlib/set/set.rbs +251 -27
  154. metadata +49 -44
  155. data/exe/ruby-signature +0 -3
  156. data/vendor/ruby-signature/exe/ruby-signature +0 -7
  157. data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +0 -29
  158. data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +0 -29
  159. data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +0 -391
  160. data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +0 -364
  161. data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +0 -52
  162. data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +0 -54
  163. data/vendor/ruby-signature/lib/ruby/signature/cli.rb +0 -555
  164. data/vendor/ruby-signature/lib/ruby/signature/constant.rb +0 -28
  165. data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +0 -152
  166. data/vendor/ruby-signature/lib/ruby/signature/definition.rb +0 -172
  167. data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +0 -921
  168. data/vendor/ruby-signature/lib/ruby/signature/environment.rb +0 -283
  169. data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +0 -138
  170. data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +0 -126
  171. data/vendor/ruby-signature/lib/ruby/signature/errors.rb +0 -189
  172. data/vendor/ruby-signature/lib/ruby/signature/location.rb +0 -104
  173. data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +0 -125
  174. data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +0 -93
  175. data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +0 -444
  176. data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +0 -579
  177. data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +0 -383
  178. data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +0 -48
  179. data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +0 -63
  180. data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +0 -290
  181. data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +0 -327
  182. data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +0 -185
  183. data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +0 -256
  184. data/vendor/ruby-signature/lib/ruby/signature/test.rb +0 -28
  185. data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +0 -72
  186. data/vendor/ruby-signature/lib/ruby/signature/types.rb +0 -932
  187. data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +0 -140
  188. data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +0 -49
  189. data/vendor/ruby-signature/lib/ruby/signature/version.rb +0 -5
  190. data/vendor/ruby-signature/lib/ruby/signature/writer.rb +0 -271
@@ -1,932 +0,0 @@
1
- module Ruby
2
- module Signature
3
- module Types
4
- module NoFreeVariables
5
- def free_variables(set = Set.new)
6
- set
7
- end
8
- end
9
-
10
- module NoSubst
11
- def sub(s)
12
- self
13
- end
14
- end
15
-
16
- module EmptyEachType
17
- def each_type
18
- if block_given?
19
- # nop
20
- else
21
- enum_for :each_type
22
- end
23
- end
24
- end
25
-
26
- module Bases
27
- class Base
28
- attr_reader :location
29
-
30
- def initialize(location:)
31
- @location = location
32
- end
33
-
34
- def ==(other)
35
- other.is_a?(self.class)
36
- end
37
-
38
- def hash
39
- self.class.hash
40
- end
41
-
42
- alias eql? ==
43
-
44
- include NoFreeVariables
45
- include NoSubst
46
- include EmptyEachType
47
-
48
- def to_json(*a)
49
- klass = to_s.to_sym
50
- { class: klass, location: location }.to_json(*a)
51
- end
52
-
53
- def to_s(level = 0)
54
- case self
55
- when Types::Bases::Bool
56
- 'bool'
57
- when Types::Bases::Void
58
- 'void'
59
- when Types::Bases::Any
60
- 'untyped'
61
- when Types::Bases::Nil
62
- 'nil'
63
- when Types::Bases::Top
64
- 'top'
65
- when Types::Bases::Bottom
66
- 'bot'
67
- when Types::Bases::Self
68
- 'self'
69
- when Types::Bases::Instance
70
- 'instance'
71
- when Types::Bases::Class
72
- 'class'
73
- else
74
- raise "Unexpected base type: #{type.inspect}"
75
- end
76
- end
77
- end
78
-
79
- class Bool < Base; end
80
- class Void < Base; end
81
- class Any < Base; end
82
- class Nil < Base; end
83
- class Top < Base; end
84
- class Bottom < Base; end
85
- class Self < Base; end
86
- class Instance < Base; end
87
- class Class < Base; end
88
- end
89
-
90
- class Variable
91
- attr_reader :name
92
- attr_reader :location
93
-
94
- def initialize(name:, location:)
95
- @name = name
96
- @location = location
97
- end
98
-
99
- def ==(other)
100
- other.is_a?(Variable) && other.name == name
101
- end
102
-
103
- alias eql? ==
104
-
105
- def hash
106
- self.class.hash ^ name.hash
107
- end
108
-
109
- def free_variables(set = Set.new)
110
- set.tap do
111
- set << name
112
- end
113
- end
114
-
115
- def to_json(*a)
116
- { class: :variable, name: name, location: location }.to_json(*a)
117
- end
118
-
119
- def sub(s)
120
- s.apply(self)
121
- end
122
-
123
- def self.build(v)
124
- case v
125
- when Symbol
126
- new(name: v, location: nil)
127
- when Array
128
- v.map {|x| new(name: x, location: nil) }
129
- else
130
- raise
131
- end
132
- end
133
-
134
- @@count = 0
135
- def self.fresh(v = :T)
136
- @@count = @@count + 1
137
- new(name: :"#{v}@#{@@count}", location: nil)
138
- end
139
-
140
- def to_s(level = 0)
141
- name.to_s
142
- end
143
-
144
- include EmptyEachType
145
- end
146
-
147
- class ClassSingleton
148
- attr_reader :name
149
- attr_reader :location
150
-
151
- def initialize(name:, location:)
152
- @name = name
153
- @location = location
154
- end
155
-
156
- def ==(other)
157
- other.is_a?(ClassSingleton) && other.name == name
158
- end
159
-
160
- alias eql? ==
161
-
162
- def hash
163
- self.class.hash ^ name.hash
164
- end
165
-
166
- include NoFreeVariables
167
- include NoSubst
168
-
169
- def to_json(*a)
170
- { class: :class_singleton, name: name, location: location }.to_json(*a)
171
- end
172
-
173
- def to_s(level = 0)
174
- "singleton(#{name})"
175
- end
176
-
177
- include EmptyEachType
178
- end
179
-
180
- module Application
181
- attr_reader :name
182
- attr_reader :args
183
-
184
- def ==(other)
185
- other.is_a?(self.class) && other.name == name && other.args == args
186
- end
187
-
188
- alias eql? ==
189
-
190
- def hash
191
- self.class.hash ^ name.hash ^ args.hash
192
- end
193
-
194
- def free_variables(set = Set.new)
195
- set.tap do
196
- args.each do |arg|
197
- arg.free_variables(set)
198
- end
199
- end
200
- end
201
-
202
- def to_s(level = 0)
203
- if args.empty?
204
- name.to_s
205
- else
206
- "#{name}[#{args.join(", ")}]"
207
- end
208
- end
209
-
210
- def each_type(&block)
211
- if block_given?
212
- args.each(&block)
213
- else
214
- enum_for :each_type
215
- end
216
- end
217
- end
218
-
219
- class Interface
220
- attr_reader :location
221
-
222
- include Application
223
-
224
- def initialize(name:, args:, location:)
225
- @name = name
226
- @args = args
227
- @location = location
228
- end
229
-
230
- def to_json(*a)
231
- { class: :interface, name: name, args: args, location: location }.to_json(*a)
232
- end
233
-
234
- def sub(s)
235
- self.class.new(name: name,
236
- args: args.map {|ty| ty.sub(s) },
237
- location: location)
238
- end
239
- end
240
-
241
- class ClassInstance
242
- attr_reader :location
243
-
244
- include Application
245
-
246
- def initialize(name:, args:, location:)
247
- @name = name
248
- @args = args
249
- @location = location
250
- end
251
-
252
- def to_json(*a)
253
- { class: :class_instance, name: name, args: args, location: location }.to_json(*a)
254
- end
255
-
256
- def sub(s)
257
- self.class.new(name: name,
258
- args: args.map {|ty| ty.sub(s) },
259
- location: location)
260
- end
261
- end
262
-
263
- class Alias
264
- attr_reader :location
265
- attr_reader :name
266
-
267
- def initialize(name:, location:)
268
- @name = name
269
- @location = location
270
- end
271
-
272
- def ==(other)
273
- other.is_a?(Alias) && other.name == name
274
- end
275
-
276
- alias eql? ==
277
-
278
- def hash
279
- self.class.hash ^ name.hash
280
- end
281
-
282
- include NoFreeVariables
283
- include NoSubst
284
-
285
- def to_json(*a)
286
- { class: :alias, name: name, location: location }.to_json(*a)
287
- end
288
-
289
- def to_s(level = 0)
290
- name.to_s
291
- end
292
-
293
- include EmptyEachType
294
- end
295
-
296
- class Tuple
297
- attr_reader :types
298
- attr_reader :location
299
-
300
- def initialize(types:, location:)
301
- @types = types
302
- @location = location
303
- end
304
-
305
- def ==(other)
306
- other.is_a?(Tuple) && other.types == types
307
- end
308
-
309
- alias eql? ==
310
-
311
- def hash
312
- self.class.hash ^ types.hash
313
- end
314
-
315
- def free_variables(set = Set.new)
316
- set.tap do
317
- types.each do |type|
318
- type.free_variables set
319
- end
320
- end
321
- end
322
-
323
- def to_json(*a)
324
- { class: :tuple, types: types, location: location }.to_json(*a)
325
- end
326
-
327
- def sub(s)
328
- self.class.new(types: types.map {|ty| ty.sub(s) },
329
- location: location)
330
- end
331
-
332
- def to_s(level = 0)
333
- if types.empty?
334
- "[ ]"
335
- else
336
- "[ #{types.join(", ")} ]"
337
- end
338
- end
339
-
340
- def each_type(&block)
341
- if block_given?
342
- types.each(&block)
343
- else
344
- enum_for :each_type
345
- end
346
- end
347
- end
348
-
349
- class Record
350
- attr_reader :fields
351
- attr_reader :location
352
-
353
- def initialize(fields:, location:)
354
- @fields = fields
355
- @location = location
356
- end
357
-
358
- def ==(other)
359
- other.is_a?(Record) && other.fields == fields
360
- end
361
-
362
- alias eql? ==
363
-
364
- def hash
365
- self.class.hash ^ fields.hash
366
- end
367
-
368
- def free_variables(set = Set.new)
369
- set.tap do
370
- fields.each_value do |type|
371
- type.free_variables set
372
- end
373
- end
374
- end
375
-
376
- def to_json(*a)
377
- { class: :record, fields: fields, location: location }.to_json(*a)
378
- end
379
-
380
- def sub(s)
381
- self.class.new(fields: fields.transform_values {|ty| ty.sub(s) },
382
- location: location)
383
- end
384
-
385
- def to_s(level = 0)
386
- fields = self.fields.map do |key, type|
387
- "#{key}: #{type}"
388
- end
389
- "{ #{fields.join(", ")} }"
390
- end
391
-
392
- def each_type(&block)
393
- if block_given?
394
- fields.each_value(&block)
395
- else
396
- enum_for :each_type
397
- end
398
- end
399
- end
400
-
401
- class Optional
402
- attr_reader :type
403
- attr_reader :location
404
-
405
- def initialize(type:, location:)
406
- @type = type
407
- @location = location
408
- end
409
-
410
- def ==(other)
411
- other.is_a?(Optional) && other.type == type
412
- end
413
-
414
- alias eql? ==
415
-
416
- def hash
417
- self.class.hash ^ type.hash
418
- end
419
-
420
- def free_variables(set = Set.new)
421
- type.free_variables(set)
422
- end
423
-
424
- def to_json(*a)
425
- { class: :optional, type: type, location: location }.to_json(*a)
426
- end
427
-
428
- def sub(s)
429
- self.class.new(type: type.sub(s), location: location)
430
- end
431
-
432
- def to_s(level = 0)
433
- if type.is_a?(Ruby::Signature::Types::Literal) && type.literal.is_a?(Symbol)
434
- "#{type.to_s(1)} ?"
435
- else
436
- "#{type.to_s(1)}?"
437
- end
438
- end
439
-
440
- def each_type
441
- if block_given?
442
- yield type
443
- else
444
- enum_for :each_type
445
- end
446
- end
447
- end
448
-
449
- class Union
450
- attr_reader :types
451
- attr_reader :location
452
-
453
- def initialize(types:, location:)
454
- @types = types
455
- @location = location
456
- end
457
-
458
- def ==(other)
459
- other.is_a?(Union) && other.types == types
460
- end
461
-
462
- alias eql? ==
463
-
464
- def hash
465
- self.class.hash ^ types.hash
466
- end
467
-
468
- def free_variables(set = Set.new)
469
- set.tap do
470
- types.each do |type|
471
- type.free_variables set
472
- end
473
- end
474
- end
475
-
476
- def to_json(*a)
477
- { class: :union, types: types, location: location }.to_json(*a)
478
- end
479
-
480
- def sub(s)
481
- self.class.new(types: types.map {|ty| ty.sub(s) },
482
- location: location)
483
- end
484
-
485
- def to_s(level = 0)
486
- if level > 0
487
- "(#{types.join(" | ")})"
488
- else
489
- types.join(" | ")
490
- end
491
- end
492
-
493
- def each_type(&block)
494
- if block_given?
495
- types.each(&block)
496
- else
497
- enum_for :each_type
498
- end
499
- end
500
-
501
- def map_type(&block)
502
- if block_given?
503
- Union.new(types: types.map(&block), location: location)
504
- else
505
- enum_for :map_type
506
- end
507
- end
508
- end
509
-
510
- class Intersection
511
- attr_reader :types
512
- attr_reader :location
513
-
514
- def initialize(types:, location:)
515
- @types = types
516
- @location = location
517
- end
518
-
519
- def ==(other)
520
- other.is_a?(Intersection) && other.types == types
521
- end
522
-
523
- alias eql? ==
524
-
525
- def hash
526
- self.class.hash ^ types.hash
527
- end
528
-
529
- def free_variables(set = Set.new)
530
- set.tap do
531
- types.each do |type|
532
- type.free_variables set
533
- end
534
- end
535
- end
536
-
537
- def to_json(*a)
538
- { class: :intersection, types: types, location: location }.to_json(*a)
539
- end
540
-
541
- def sub(s)
542
- self.class.new(types: types.map {|ty| ty.sub(s) },
543
- location: location)
544
- end
545
-
546
- def to_s(level = 0)
547
- strs = types.map {|ty| ty.to_s(2) }
548
- if level > 0
549
- "(#{strs.join(" & ")})"
550
- else
551
- strs.join(" & ")
552
- end
553
- end
554
-
555
- def each_type(&block)
556
- if block_given?
557
- types.each(&block)
558
- else
559
- enum_for :each_type
560
- end
561
- end
562
-
563
- def map_type(&block)
564
- if block_given?
565
- Intersection.new(types: types.map(&block), location: location)
566
- else
567
- enum_for :map_type
568
- end
569
- end
570
- end
571
-
572
- class Function
573
- class Param
574
- attr_reader :type
575
- attr_reader :name
576
-
577
- def initialize(type:, name:)
578
- @type = type
579
- @name = name
580
- end
581
-
582
- def ==(other)
583
- other.is_a?(Param) && other.type == type && other.name == name
584
- end
585
-
586
- alias eql? ==
587
-
588
- def hash
589
- self.class.hash ^ type.hash ^ name.hash
590
- end
591
-
592
- def map_type
593
- if block_given?
594
- Param.new(name: name, type: yield(type))
595
- else
596
- enum_for :map_type
597
- end
598
- end
599
-
600
- def to_json(*a)
601
- { type: type, name: name }.to_json(*a)
602
- end
603
-
604
- def to_s
605
- if name
606
- if /\A#{Parser::KEYWORDS_RE}\z/.match?(name)
607
- "#{type} `#{name}`"
608
- else
609
- "#{type} #{name}"
610
- end
611
- else
612
- "#{type}"
613
- end
614
- end
615
- end
616
-
617
- attr_reader :required_positionals
618
- attr_reader :optional_positionals
619
- attr_reader :rest_positionals
620
- attr_reader :trailing_positionals
621
- attr_reader :required_keywords
622
- attr_reader :optional_keywords
623
- attr_reader :rest_keywords
624
- attr_reader :return_type
625
-
626
- def initialize(required_positionals:, optional_positionals:, rest_positionals:, trailing_positionals:, required_keywords:, optional_keywords:, rest_keywords:, return_type:)
627
- @return_type = return_type
628
- @required_positionals = required_positionals
629
- @optional_positionals = optional_positionals
630
- @rest_positionals = rest_positionals
631
- @trailing_positionals = trailing_positionals
632
- @required_keywords = required_keywords
633
- @optional_keywords = optional_keywords
634
- @rest_keywords = rest_keywords
635
- end
636
-
637
- def ==(other)
638
- other.is_a?(Function) &&
639
- other.required_positionals == required_positionals &&
640
- other.optional_positionals == optional_positionals &&
641
- other.rest_positionals == rest_positionals &&
642
- other.trailing_positionals == trailing_positionals &&
643
- other.required_keywords == required_keywords &&
644
- other.optional_keywords == optional_keywords &&
645
- other.rest_keywords == rest_keywords &&
646
- return_type == return_type
647
- end
648
-
649
- alias eql? ==
650
-
651
- def hash
652
- self.class.hash ^
653
- required_positionals.hash ^
654
- optional_positionals.hash ^
655
- rest_positionals.hash ^
656
- trailing_positionals.hash ^
657
- required_keywords.hash ^
658
- optional_keywords.hash ^
659
- rest_keywords.hash ^
660
- return_type.hash
661
- end
662
-
663
- def free_variables(set = Set.new)
664
- set.tap do
665
- required_positionals.each do |param|
666
- param.type.free_variables(set)
667
- end
668
- optional_positionals.each do |param|
669
- param.type.free_variables(set)
670
- end
671
- rest_positionals&.yield_self do |param|
672
- param.type.free_variables(set)
673
- end
674
- trailing_positionals.each do |param|
675
- param.type.free_variables(set)
676
- end
677
- required_keywords.each_value do |param|
678
- param.type.free_variables(set)
679
- end
680
- optional_keywords.each_value do |param|
681
- param.type.free_variables(set)
682
- end
683
- rest_keywords&.yield_self do |param|
684
- param.type.free_variables(set)
685
- end
686
-
687
- return_type.free_variables(set)
688
- end
689
- end
690
-
691
- def map_type(&block)
692
- if block_given?
693
- Function.new(
694
- required_positionals: required_positionals.map {|param| param.map_type(&block) },
695
- optional_positionals: optional_positionals.map {|param| param.map_type(&block) },
696
- rest_positionals: rest_positionals&.yield_self {|param| param.map_type(&block) },
697
- trailing_positionals: trailing_positionals.map {|param| param.map_type(&block) },
698
- required_keywords: required_keywords.transform_values {|param| param.map_type(&block) },
699
- optional_keywords: optional_keywords.transform_values {|param| param.map_type(&block) },
700
- rest_keywords: rest_keywords&.yield_self {|param| param.map_type(&block) },
701
- return_type: yield(return_type)
702
- )
703
- else
704
- enum_for :map_type
705
- end
706
- end
707
-
708
- def each_type
709
- if block_given?
710
- required_positionals.each {|param| yield param.type }
711
- optional_positionals.each {|param| yield param.type }
712
- rest_positionals&.yield_self {|param| yield param.type }
713
- trailing_positionals.each {|param| yield param.type }
714
- required_keywords.each_value {|param| yield param.type }
715
- optional_keywords.each_value {|param| yield param.type }
716
- rest_keywords&.yield_self {|param| yield param.type }
717
- yield(return_type)
718
- else
719
- enum_for :each_type
720
- end
721
- end
722
-
723
- def each_param(&block)
724
- if block_given?
725
- required_positionals.each(&block)
726
- optional_positionals.each(&block)
727
- rest_positionals&.yield_self(&block)
728
- trailing_positionals.each(&block)
729
- required_keywords.each_value(&block)
730
- optional_keywords.each_value(&block)
731
- rest_keywords&.yield_self(&block)
732
- else
733
- enum_for :each_param
734
- end
735
- end
736
-
737
- def to_json(*a)
738
- {
739
- required_positionals: required_positionals,
740
- optional_positionals: optional_positionals,
741
- rest_positionals: rest_positionals,
742
- trailing_positionals: trailing_positionals,
743
- required_keywords: required_keywords,
744
- optional_keywords: optional_keywords,
745
- rest_keywords: rest_keywords,
746
- return_type: return_type
747
- }.to_json(*a)
748
- end
749
-
750
- def sub(s)
751
- map_type {|ty| ty.sub(s) }
752
- end
753
-
754
- def self.empty(return_type)
755
- Function.new(
756
- required_positionals: [],
757
- optional_positionals: [],
758
- rest_positionals: nil,
759
- trailing_positionals: [],
760
- required_keywords: {},
761
- optional_keywords: {},
762
- rest_keywords: nil,
763
- return_type: return_type
764
- )
765
- end
766
-
767
- def with_return_type(type)
768
- Function.new(
769
- required_positionals: required_positionals,
770
- optional_positionals: optional_positionals,
771
- rest_positionals: rest_positionals,
772
- trailing_positionals: trailing_positionals,
773
- required_keywords: required_keywords,
774
- optional_keywords: optional_keywords,
775
- rest_keywords: rest_keywords,
776
- return_type: type
777
- )
778
- end
779
-
780
- def update(required_positionals: self.required_positionals, optional_positionals: self.optional_positionals, rest_positionals: self.rest_positionals, trailing_positionals: self.trailing_positionals,
781
- required_keywords: self.required_keywords, optional_keywords: self.optional_keywords, rest_keywords: self.rest_keywords, return_type: self.return_type)
782
- Function.new(
783
- required_positionals: required_positionals,
784
- optional_positionals: optional_positionals,
785
- rest_positionals: rest_positionals,
786
- trailing_positionals: trailing_positionals,
787
- required_keywords: required_keywords,
788
- optional_keywords: optional_keywords,
789
- rest_keywords: rest_keywords,
790
- return_type: return_type
791
- )
792
- end
793
-
794
- def empty?
795
- required_positionals.empty? &&
796
- optional_positionals.empty? &&
797
- !rest_positionals &&
798
- trailing_positionals.empty? &&
799
- required_keywords.empty? &&
800
- optional_keywords.empty? &&
801
- !rest_keywords
802
- end
803
-
804
- def param_to_s
805
- params = []
806
- params.push(*required_positionals.map(&:to_s))
807
- params.push(*optional_positionals.map {|p| "?#{p}"})
808
- params.push("*#{rest_positionals}") if rest_positionals
809
- params.push(*trailing_positionals.map(&:to_s))
810
- params.push(*required_keywords.map {|name, param| "#{name}: #{param}" })
811
- params.push(*optional_keywords.map {|name, param| "?#{name}: #{param}" })
812
- params.push("**#{rest_keywords}") if rest_keywords
813
-
814
- params.join(", ")
815
- end
816
-
817
- def return_to_s
818
- return_type.to_s(1)
819
- end
820
-
821
- def drop_head
822
- case
823
- when !required_positionals.empty?
824
- [
825
- required_positionals[0],
826
- update(required_positionals: required_positionals.drop(1))
827
- ]
828
- when !optional_positionals.empty?
829
- [
830
- optional_positionals[0],
831
- update(optional_positionals: optional_positionals.drop(1))
832
- ]
833
- else
834
- raise "Cannot #drop_head"
835
- end
836
- end
837
-
838
- def drop_tail
839
- case
840
- when !trailing_positionals.empty?
841
- [
842
- trailing_positionals.last,
843
- update(trailing_positionals: trailing_positionals.take(trailing_positionals.size - 1))
844
- ]
845
- else
846
- raise "Cannot #drop_tail"
847
- end
848
- end
849
-
850
- def has_keyword?
851
- !required_keywords.empty? || !optional_keywords.empty? || rest_keywords
852
- end
853
- end
854
-
855
- class Proc
856
- attr_reader :type
857
- attr_reader :location
858
-
859
- def initialize(location:, type:)
860
- @type = type
861
- @location = location
862
- end
863
-
864
- def ==(other)
865
- other.is_a?(Proc) && other.type == type
866
- end
867
-
868
- alias eql? ==
869
-
870
- def hash
871
- self.class.hash ^ type.hash
872
- end
873
-
874
- def free_variables(set)
875
- type.free_variables(set)
876
- end
877
-
878
- def to_json(*a)
879
- { class: :proc, type: type, location: location }.to_json(*a)
880
- end
881
-
882
- def sub(s)
883
- self.class.new(type: type.sub(s), location: location)
884
- end
885
-
886
- def to_s(level = 0)
887
- "^(#{type.param_to_s}) -> #{type.return_to_s}".lstrip
888
- end
889
-
890
- def each_type(&block)
891
- if block_given?
892
- type.each_type(&block)
893
- else
894
- enum_for :each_type
895
- end
896
- end
897
- end
898
-
899
- class Literal
900
- attr_reader :literal
901
- attr_reader :location
902
-
903
- def initialize(literal:, location:)
904
- @literal = literal
905
- @location = location
906
- end
907
-
908
- def ==(other)
909
- other.is_a?(Literal) && other.literal == literal
910
- end
911
-
912
- alias eql? ==
913
-
914
- def hash
915
- self.class.hash ^ literal.hash
916
- end
917
-
918
- include NoFreeVariables
919
- include NoSubst
920
- include EmptyEachType
921
-
922
- def to_json(*a)
923
- { class: :literal, literal: literal.inspect, location: location }.to_json(*a)
924
- end
925
-
926
- def to_s(level = 0)
927
- literal.inspect
928
- end
929
- end
930
- end
931
- end
932
- end