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,1090 @@
1
+ module Steep
2
+ module Interface
3
+ class Function
4
+ class Params
5
+ module Utils
6
+ def union(*types, null: false)
7
+ types << AST::Builtin.nil_type if null
8
+ AST::Types::Union.build(types: types)
9
+ end
10
+
11
+ def intersection(*types)
12
+ AST::Types::Intersection.build(types: types)
13
+ end
14
+ end
15
+
16
+ class PositionalParams
17
+ class Base
18
+ attr_reader :type
19
+
20
+ def initialize(type)
21
+ @type = type
22
+ end
23
+
24
+ def ==(other)
25
+ other.is_a?(self.class) && other.type == type
26
+ end
27
+
28
+ alias eql? ==
29
+
30
+ def hash
31
+ self.class.hash ^ type.hash
32
+ end
33
+
34
+ def subst(s)
35
+ ty = type.subst(s)
36
+
37
+ if ty == type
38
+ self
39
+ else
40
+ _ = self.class.new(ty)
41
+ end
42
+ end
43
+
44
+ def var_type
45
+ type
46
+ end
47
+
48
+ def map_type(&block)
49
+ if block_given?
50
+ _ = self.class.new(yield type)
51
+ else
52
+ enum_for(:map_type)
53
+ end
54
+ end
55
+ end
56
+
57
+ class Required < Base; end
58
+ class Optional < Base; end
59
+ class Rest < Base; end
60
+
61
+ attr_reader :head
62
+ attr_reader :tail
63
+
64
+ def initialize(head:, tail:)
65
+ @head = head
66
+ @tail = tail
67
+ end
68
+
69
+ def self.required(type, tail = nil)
70
+ PositionalParams.new(head: Required.new(type), tail: tail)
71
+ end
72
+
73
+ def self.optional(type, tail = nil)
74
+ PositionalParams.new(head: Optional.new(type), tail: tail)
75
+ end
76
+
77
+ def self.rest(type, tail = nil)
78
+ PositionalParams.new(head: Rest.new(type), tail: tail)
79
+ end
80
+
81
+ def to_ary
82
+ [head, tail]
83
+ end
84
+
85
+ def map(&block)
86
+ hd = yield(head)
87
+ tl = tail&.map(&block)
88
+
89
+ if head == hd && tail == tl
90
+ self
91
+ else
92
+ PositionalParams.new(head: hd, tail: tl)
93
+ end
94
+ end
95
+
96
+ def map_type(&block)
97
+ if block
98
+ map {|param| param.map_type(&block) }
99
+ else
100
+ enum_for :map_type
101
+ end
102
+ end
103
+
104
+ def subst(s)
105
+ map_type do |type|
106
+ ty = type.subst(s)
107
+ if ty == type
108
+ type
109
+ else
110
+ ty
111
+ end
112
+ end
113
+ end
114
+
115
+ def ==(other)
116
+ other.is_a?(PositionalParams) && other.head == head && other.tail == tail
117
+ end
118
+
119
+ alias eql? ==
120
+
121
+ def hash
122
+ self.class.hash ^ head.hash ^ tail.hash
123
+ end
124
+
125
+ def each(&block)
126
+ if block
127
+ yield head
128
+ tail&.each(&block)
129
+ else
130
+ enum_for(:each)
131
+ end
132
+ end
133
+
134
+ def each_type
135
+ if block_given?
136
+ each do |param|
137
+ yield param.type
138
+ end
139
+ else
140
+ enum_for :each_type
141
+ end
142
+ end
143
+
144
+ def size
145
+ 1 + (tail&.size || 0)
146
+ end
147
+
148
+ def self.build(required:, optional:, rest:)
149
+ params = rest ? self.rest(rest) : nil
150
+ params = optional.reverse_each.inject(params) {|params, type| self.optional(type, params) }
151
+ params = required.reverse_each.inject(params) {|params, type| self.required(type, params) }
152
+
153
+ params
154
+ end
155
+
156
+ extend Utils
157
+
158
+ # Calculates xs + ys.
159
+ # Never fails.
160
+ def self.merge_for_overload(xs, ys)
161
+ x = xs&.head
162
+ y = ys&.head
163
+
164
+ case
165
+ when x.is_a?(Required) && y.is_a?(Required)
166
+ xs or raise
167
+ ys or raise
168
+ required(
169
+ union(x.type, y.type),
170
+ merge_for_overload(xs.tail, ys.tail)
171
+ )
172
+ when x.is_a?(Required) && y.is_a?(Optional)
173
+ xs or raise
174
+ ys or raise
175
+ optional(
176
+ union(x.type, y.type, null: true),
177
+ merge_for_overload(xs.tail, ys.tail)
178
+ )
179
+ when x.is_a?(Required) && y.is_a?(Rest)
180
+ xs or raise
181
+ ys or raise
182
+ optional(
183
+ union(x.type, y.type, null: true),
184
+ merge_for_overload(xs.tail, ys)
185
+ )
186
+ when x.is_a?(Required) && !y
187
+ xs or raise
188
+ optional(
189
+ union(x.type, null: true),
190
+ merge_for_overload(xs.tail, nil)
191
+ )
192
+ when x.is_a?(Optional) && y.is_a?(Required)
193
+ xs or raise
194
+ ys or raise
195
+ optional(
196
+ union(x.type, y.type, null: true),
197
+ merge_for_overload(xs.tail, ys.tail)
198
+ )
199
+ when x.is_a?(Optional) && y.is_a?(Optional)
200
+ xs or raise
201
+ ys or raise
202
+ optional(
203
+ union(x.type, y.type),
204
+ merge_for_overload(xs.tail, ys.tail)
205
+ )
206
+ when x.is_a?(Optional) && y.is_a?(Rest)
207
+ xs or raise
208
+ ys or raise
209
+ optional(
210
+ union(x.type, y.type),
211
+ merge_for_overload(xs.tail, ys)
212
+ )
213
+ when x.is_a?(Optional) && !y
214
+ xs or raise
215
+ optional(
216
+ x.type,
217
+ merge_for_overload(xs.tail, nil)
218
+ ) # == xs
219
+ when x.is_a?(Rest) && y.is_a?(Required)
220
+ xs or raise
221
+ ys or raise
222
+ optional(
223
+ union(x.type, y.type, null: true),
224
+ merge_for_overload(xs, ys.tail)
225
+ )
226
+ when x.is_a?(Rest) && y.is_a?(Optional)
227
+ xs or raise
228
+ ys or raise
229
+ optional(
230
+ union(x.type, y.type),
231
+ merge_for_overload(xs, ys.tail)
232
+ )
233
+ when x.is_a?(Rest) && y.is_a?(Rest)
234
+ xs or raise
235
+ ys or raise
236
+ rest(union(x.type, y.type))
237
+ when x.is_a?(Rest) && !y
238
+ xs or raise
239
+ when !x && y.is_a?(Required)
240
+ ys or raise
241
+ optional(
242
+ union(y.type, null: true),
243
+ merge_for_overload(nil, ys.tail)
244
+ )
245
+ when !x && y.is_a?(Optional)
246
+ ys or raise
247
+ optional(
248
+ y.type,
249
+ merge_for_overload(nil, ys.tail)
250
+ ) # == ys
251
+ when !x && y.is_a?(Rest)
252
+ ys or raise
253
+ when !x && !y
254
+ nil
255
+ end
256
+ end
257
+
258
+ # xs | ys
259
+ def self.merge_for_union(xs, ys)
260
+ x = xs&.head
261
+ y = ys&.head
262
+
263
+ case
264
+ when x.is_a?(Required) && y.is_a?(Required)
265
+ xs or raise
266
+ ys or raise
267
+ required(
268
+ union(x.type, y.type),
269
+ merge_for_union(xs.tail, ys.tail)
270
+ )
271
+ when x.is_a?(Required) && !y
272
+ xs or raise
273
+ optional(
274
+ x.type,
275
+ merge_for_union(xs.tail, nil)
276
+ )
277
+ when x.is_a?(Required) && y.is_a?(Optional)
278
+ xs or raise
279
+ ys or raise
280
+ optional(
281
+ union(x.type, y.type),
282
+ merge_for_union(xs.tail, ys.tail)
283
+ )
284
+ when x.is_a?(Required) && y.is_a?(Rest)
285
+ xs or raise
286
+ ys or raise
287
+ optional(
288
+ union(x.type, y.type),
289
+ merge_for_union(xs.tail, ys)
290
+ )
291
+ when !x && y.is_a?(Required)
292
+ ys or raise
293
+ optional(
294
+ y.type,
295
+ merge_for_union(nil, ys.tail)
296
+ )
297
+ when !x && !y
298
+ nil
299
+ when !x && y.is_a?(Optional)
300
+ ys or raise
301
+ PositionalParams.new(head: y, tail: merge_for_union(nil, ys.tail))
302
+ when !x && y.is_a?(Rest)
303
+ ys or raise
304
+ when x.is_a?(Optional) && y.is_a?(Required)
305
+ xs or raise
306
+ ys or raise
307
+ optional(
308
+ union(x.type, y.type),
309
+ merge_for_union(xs.tail, ys.tail)
310
+ )
311
+ when x.is_a?(Optional) && !y
312
+ xs or raise
313
+ PositionalParams.new(head: x, tail: merge_for_union(xs.tail, nil)) # == xs
314
+ when x.is_a?(Optional) && y.is_a?(Optional)
315
+ xs or raise
316
+ ys or raise
317
+ optional(
318
+ union(x.type, y.type),
319
+ merge_for_union(xs.tail, ys.tail)
320
+ )
321
+ when x.is_a?(Optional) && y.is_a?(Rest)
322
+ xs or raise
323
+ ys or raise
324
+ optional(
325
+ union(x.type, y.type),
326
+ merge_for_union(xs.tail, ys.tail)
327
+ )
328
+ when x.is_a?(Rest) && y.is_a?(Required)
329
+ xs or raise
330
+ ys or raise
331
+ optional(
332
+ union(x.type, y.type),
333
+ merge_for_union(xs, ys.tail)
334
+ )
335
+ when x.is_a?(Rest) && !y
336
+ xs or raise
337
+ when x.is_a?(Rest) && y.is_a?(Optional)
338
+ xs or raise
339
+ ys or raise
340
+ optional(
341
+ union(x.type, y.type),
342
+ merge_for_union(xs, ys.tail)
343
+ )
344
+ when x.is_a?(Rest) && y.is_a?(Rest)
345
+ xs or raise
346
+ ys or raise
347
+ rest(
348
+ union(x.type, y.type)
349
+ )
350
+ end
351
+ end
352
+
353
+ # Calculates xs & ys.
354
+ # Raises when failed.
355
+ #
356
+ def self.merge_for_intersection(xs, ys)
357
+ x = xs&.head
358
+ y = ys&.head
359
+
360
+ case
361
+ when x.is_a?(Required) && y.is_a?(Required)
362
+ xs or raise
363
+ ys or raise
364
+ required(
365
+ intersection(x.type, y.type),
366
+ merge_for_intersection(xs.tail, ys.tail)
367
+ )
368
+ when x.is_a?(Required) && !y
369
+ raise
370
+ when x.is_a?(Required) && y.is_a?(Optional)
371
+ xs or raise
372
+ ys or raise
373
+ required(
374
+ intersection(x.type, y.type),
375
+ merge_for_intersection(xs.tail, ys.tail)
376
+ )
377
+ when x.is_a?(Required) && y.is_a?(Rest)
378
+ xs or raise
379
+ ys or raise
380
+ required(
381
+ intersection(x.type, y.type),
382
+ merge_for_intersection(xs.tail, ys)
383
+ )
384
+ when !x && y.is_a?(Required)
385
+ raise
386
+ when !x && !y
387
+ nil
388
+ when !x && y.is_a?(Optional)
389
+ nil
390
+ when !x && y.is_a?(Rest)
391
+ nil
392
+ when x.is_a?(Optional) && y.is_a?(Required)
393
+ xs or raise
394
+ ys or raise
395
+ required(
396
+ intersection(x.type, y.type),
397
+ merge_for_intersection(xs.tail, ys.tail)
398
+ )
399
+ when x.is_a?(Optional) && !y
400
+ nil
401
+ when x.is_a?(Optional) && y.is_a?(Optional)
402
+ xs or raise
403
+ ys or raise
404
+ optional(
405
+ intersection(x.type, y.type),
406
+ merge_for_intersection(xs.tail, ys.tail)
407
+ )
408
+ when x.is_a?(Optional) && y.is_a?(Rest)
409
+ xs or raise
410
+ ys or raise
411
+ optional(
412
+ intersection(x.type, y.type),
413
+ merge_for_intersection(xs.tail, ys)
414
+ )
415
+ when x.is_a?(Rest) && y.is_a?(Required)
416
+ xs or raise
417
+ ys or raise
418
+ required(
419
+ intersection(x.type, y.type),
420
+ merge_for_intersection(xs, ys.tail)
421
+ )
422
+ when x.is_a?(Rest) && !y
423
+ nil
424
+ when x.is_a?(Rest) && y.is_a?(Optional)
425
+ xs or raise
426
+ ys or raise
427
+ optional(
428
+ intersection(x.type, y.type),
429
+ merge_for_intersection(xs, ys.tail)
430
+ )
431
+ when x.is_a?(Rest) && y.is_a?(Rest)
432
+ rest(intersection(x.type, y.type))
433
+ end
434
+ end
435
+ end
436
+
437
+ class KeywordParams
438
+ attr_reader :requireds
439
+ attr_reader :optionals
440
+ attr_reader :rest
441
+
442
+ def initialize(requireds: {}, optionals: {}, rest: nil)
443
+ @requireds = requireds
444
+ @optionals = optionals
445
+ @rest = rest
446
+ end
447
+
448
+ def ==(other)
449
+ other.is_a?(KeywordParams) &&
450
+ other.requireds == requireds &&
451
+ other.optionals == optionals &&
452
+ other.rest == rest
453
+ end
454
+
455
+ alias eql? ==
456
+
457
+ def hash
458
+ self.class.hash ^ requireds.hash ^ optionals.hash ^ rest.hash
459
+ end
460
+
461
+ def update(requireds: self.requireds, optionals: self.optionals, rest: self.rest)
462
+ KeywordParams.new(
463
+ requireds: requireds,
464
+ optionals: optionals,
465
+ rest: rest
466
+ )
467
+ end
468
+
469
+ def empty?
470
+ requireds.empty? && optionals.empty? && rest.nil?
471
+ end
472
+
473
+ def each(&block)
474
+ if block
475
+ requireds.each(&block)
476
+ optionals.each(&block)
477
+ if rest
478
+ yield [nil, rest]
479
+ end
480
+ else
481
+ enum_for :each
482
+ end
483
+ end
484
+
485
+ def each_type
486
+ if block_given?
487
+ each do |_, type|
488
+ yield type
489
+ end
490
+ else
491
+ enum_for :each_type
492
+ end
493
+ end
494
+
495
+ def map_type(&block)
496
+ if block
497
+ rs = requireds.transform_values(&block)
498
+ os = optionals.transform_values(&block)
499
+ r = rest&.yield_self(&block)
500
+
501
+ if requireds == rs && optionals == os && rest == r
502
+ self
503
+ else
504
+ update(requireds: rs, optionals: os, rest: r)
505
+ end
506
+ else
507
+ enum_for(:map_type)
508
+ end
509
+ end
510
+
511
+ def subst(s)
512
+ map_type do |type|
513
+ ty = type.subst(s)
514
+ if ty == type
515
+ type
516
+ else
517
+ ty
518
+ end
519
+ end
520
+ end
521
+
522
+ def size
523
+ requireds.size + optionals.size + (rest ? 1 : 0)
524
+ end
525
+
526
+ def keywords
527
+ Set[] + requireds.keys + optionals.keys
528
+ end
529
+
530
+ include Utils
531
+
532
+ # For overloading
533
+ def +(other)
534
+ requireds = {} #: Hash[Symbol, AST::Types::t]
535
+ optionals = {} #: Hash[Symbol, AST::Types::t]
536
+
537
+ all_keys = Set[] + self.requireds.keys + self.optionals.keys + other.requireds.keys + other.optionals.keys
538
+ all_keys.each do |key|
539
+ case
540
+ when t = self.requireds[key]
541
+ case
542
+ when s = other.requireds[key]
543
+ requireds[key] = union(t, s)
544
+ when s = other.optionals[key]
545
+ optionals[key] = union(t, s, null: true)
546
+ when s = other.rest
547
+ optionals[key] = union(t, s, null: true)
548
+ else
549
+ optionals[key] = union(t, null: true)
550
+ end
551
+ when t = self.optionals[key]
552
+ case
553
+ when s = other.requireds[key]
554
+ optionals[key] = union(t, s, null: true)
555
+ when s = other.optionals[key]
556
+ optionals[key] = union(t, s)
557
+ when s = other.rest
558
+ optionals[key] = union(t, s)
559
+ else
560
+ optionals[key] = t
561
+ end
562
+ when t = self.rest
563
+ case
564
+ when s = other.requireds[key]
565
+ optionals[key] = union(t, s, null: true)
566
+ when s = other.optionals[key]
567
+ optionals[key] = union(t, s)
568
+ when s = other.rest
569
+ # cannot happen
570
+ else
571
+ # nop
572
+ end
573
+ else
574
+ case
575
+ when s = other.requireds[key]
576
+ optionals[key] = union(s, null: true)
577
+ when s = other.optionals[key]
578
+ optionals[key] = s
579
+ when s = other.rest
580
+ # nop
581
+ else
582
+ # cannot happen
583
+ end
584
+ end
585
+ end
586
+
587
+ if self.rest && other.rest
588
+ rest = union(self.rest, other.rest)
589
+ else
590
+ rest = self.rest || other.rest
591
+ end
592
+
593
+ KeywordParams.new(requireds: requireds, optionals: optionals, rest: rest)
594
+ end
595
+
596
+ # For union
597
+ def |(other)
598
+ requireds = {} #: Hash[Symbol, AST::Types::t]
599
+ optionals = {} #: Hash[Symbol, AST::Types::t]
600
+
601
+ all_keys = Set[] + self.requireds.keys + self.optionals.keys + other.requireds.keys + other.optionals.keys
602
+ all_keys.each do |key|
603
+ case
604
+ when t = self.requireds[key]
605
+ case
606
+ when s = other.requireds[key]
607
+ requireds[key] = union(t, s)
608
+ when s = other.optionals[key]
609
+ optionals[key] = union(t, s)
610
+ when s = other.rest
611
+ optionals[key] = union(t, s)
612
+ else
613
+ optionals[key] = t
614
+ end
615
+ when t = self.optionals[key]
616
+ case
617
+ when s = other.requireds[key]
618
+ optionals[key] = union(t, s)
619
+ when s = other.optionals[key]
620
+ optionals[key] = union(t, s)
621
+ when s = other.rest
622
+ optionals[key] = union(t, s)
623
+ else
624
+ optionals[key] = t
625
+ end
626
+ when t = self.rest
627
+ case
628
+ when s = other.requireds[key]
629
+ optionals[key] = union(t, s)
630
+ when s = other.optionals[key]
631
+ optionals[key] = union(t, s)
632
+ when s = other.rest
633
+ # cannot happen
634
+ else
635
+ # nop
636
+ end
637
+ else
638
+ case
639
+ when s = other.requireds[key]
640
+ optionals[key] = s
641
+ when s = other.optionals[key]
642
+ optionals[key] = s
643
+ when s = other.rest
644
+ # nop
645
+ else
646
+ # cannot happen
647
+ end
648
+ end
649
+ end
650
+
651
+ rest =
652
+ if self.rest && other.rest
653
+ union(self.rest, other.rest)
654
+ else
655
+ self.rest || other.rest
656
+ end
657
+
658
+ KeywordParams.new(requireds: requireds, optionals: optionals, rest: rest)
659
+ end
660
+
661
+ # For intersection
662
+ def &(other)
663
+ requireds = {} #: Hash[Symbol, AST::Types::t]
664
+ optionals = {} #: Hash[Symbol, AST::Types::t]
665
+
666
+ all_keys = Set[] + self.requireds.keys + self.optionals.keys + other.requireds.keys + other.optionals.keys
667
+ all_keys.each do |key|
668
+ case
669
+ when t = self.requireds[key]
670
+ case
671
+ when s = other.requireds[key]
672
+ requireds[key] = intersection(t, s)
673
+ when s = other.optionals[key]
674
+ requireds[key] = intersection(t, s)
675
+ when s = other.rest
676
+ requireds[key] = intersection(t, s)
677
+ else
678
+ return nil
679
+ end
680
+ when t = self.optionals[key]
681
+ case
682
+ when s = other.requireds[key]
683
+ requireds[key] = intersection(t, s)
684
+ when s = other.optionals[key]
685
+ optionals[key] = intersection(t, s)
686
+ when s = other.rest
687
+ optionals[key] = intersection(t, s)
688
+ else
689
+ # nop
690
+ end
691
+ when t = self.rest
692
+ case
693
+ when s = other.requireds[key]
694
+ requireds[key] = intersection(t, s)
695
+ when s = other.optionals[key]
696
+ optionals[key] = intersection(t, s)
697
+ when s = other.rest
698
+ # cannot happen
699
+ else
700
+ # nop
701
+ end
702
+ else
703
+ case
704
+ when s = other.requireds[key]
705
+ return nil
706
+ when s = other.optionals[key]
707
+ # nop
708
+ when s = other.rest
709
+ # nop
710
+ else
711
+ # cannot happen
712
+ end
713
+ end
714
+ end
715
+
716
+ rest =
717
+ if self.rest && other.rest
718
+ intersection(self.rest, other.rest)
719
+ else
720
+ nil
721
+ end
722
+
723
+ KeywordParams.new(requireds: requireds, optionals: optionals, rest: rest)
724
+ end
725
+ end
726
+
727
+ def required
728
+ array = [] #: Array[AST::Types::t]
729
+
730
+ positional_params&.each do |param|
731
+ case param
732
+ when PositionalParams::Required
733
+ array << param.type
734
+ else
735
+ break
736
+ end
737
+ end
738
+
739
+ array
740
+ end
741
+
742
+ def optional
743
+ array = [] #: Array[AST::Types::t]
744
+
745
+ positional_params&.each do |param|
746
+ case param
747
+ when PositionalParams::Required
748
+ # skip
749
+ when PositionalParams::Optional
750
+ array << param.type
751
+ else
752
+ break
753
+ end
754
+ end
755
+
756
+ array
757
+ end
758
+
759
+ def rest
760
+ positional_params&.each do |param|
761
+ case param
762
+ when PositionalParams::Required, PositionalParams::Optional
763
+ # skip
764
+ when PositionalParams::Rest
765
+ return param.type
766
+ end
767
+ end
768
+
769
+ nil
770
+ end
771
+
772
+ attr_reader :positional_params
773
+ attr_reader :keyword_params
774
+
775
+ def self.build(required: [], optional: [], rest: nil, required_keywords: {}, optional_keywords: {}, rest_keywords: nil)
776
+ positional_params = PositionalParams.build(required: required, optional: optional, rest: rest)
777
+ keyword_params = KeywordParams.new(requireds: required_keywords, optionals: optional_keywords, rest: rest_keywords)
778
+ new(positional_params: positional_params, keyword_params: keyword_params)
779
+ end
780
+
781
+ def initialize(positional_params:, keyword_params:)
782
+ @positional_params = positional_params
783
+ @keyword_params = keyword_params
784
+ end
785
+
786
+ def update(positional_params: self.positional_params, keyword_params: self.keyword_params)
787
+ self.class.new(positional_params: positional_params, keyword_params: keyword_params)
788
+ end
789
+
790
+ def first_param
791
+ positional_params&.head
792
+ end
793
+
794
+ def with_first_param(param)
795
+ update(
796
+ positional_params: PositionalParams.new(
797
+ head: param,
798
+ tail: positional_params
799
+ )
800
+ )
801
+ end
802
+
803
+ def has_positional?
804
+ positional_params ? true : false
805
+ end
806
+
807
+ def self.empty
808
+ self.new(positional_params: nil, keyword_params: KeywordParams.new)
809
+ end
810
+
811
+ def ==(other)
812
+ other.is_a?(self.class) &&
813
+ other.positional_params == positional_params &&
814
+ other.keyword_params == keyword_params
815
+ end
816
+
817
+ alias eql? ==
818
+
819
+ def hash
820
+ self.class.hash ^ positional_params.hash ^ keyword_params.hash
821
+ end
822
+
823
+ def flat_unnamed_params
824
+ if positional_params
825
+ positional_params.each.with_object([]) do |param, types|
826
+ case param
827
+ when PositionalParams::Required
828
+ types << [:required, param.type]
829
+ when PositionalParams::Optional
830
+ types << [:optional, param.type]
831
+ end
832
+ end
833
+ else
834
+ []
835
+ end
836
+ end
837
+
838
+ def flat_keywords
839
+ required_keywords.merge(optional_keywords)
840
+ end
841
+
842
+ def required_keywords
843
+ keyword_params.requireds
844
+ end
845
+
846
+ def optional_keywords
847
+ keyword_params.optionals
848
+ end
849
+
850
+ def rest_keywords
851
+ keyword_params.rest
852
+ end
853
+
854
+ def has_keywords?
855
+ !keyword_params.empty?
856
+ end
857
+
858
+ def each_positional_param(&block)
859
+ if block_given?
860
+ if positional_params
861
+ positional_params.each(&block)
862
+ end
863
+ else
864
+ enum_for :each_positional_param
865
+ end
866
+ end
867
+
868
+ def without_keywords
869
+ update(keyword_params: KeywordParams.new)
870
+ end
871
+
872
+ def drop_first
873
+ case
874
+ when positional_params
875
+ update(positional_params: positional_params.tail)
876
+ when has_keywords?
877
+ without_keywords()
878
+ else
879
+ raise "Cannot drop from empty params"
880
+ end
881
+ end
882
+
883
+ def each_type(&block)
884
+ if block
885
+ positional_params&.each_type(&block)
886
+ keyword_params.each_type(&block)
887
+ else
888
+ enum_for :each_type
889
+ end
890
+ end
891
+
892
+ def free_variables()
893
+ @fvs ||= Set.new.tap do |set|
894
+ each_type do |type|
895
+ set.merge(type.free_variables)
896
+ end
897
+ end
898
+ end
899
+
900
+ def closed?
901
+ each_type.all? { _1.free_variables.empty? }
902
+ end
903
+
904
+ def subst(s)
905
+ return self if s.empty?
906
+ return self if empty?
907
+ return self if each_type.none? {|t| s.apply?(t) }
908
+
909
+ pp = positional_params
910
+ kp = keyword_params
911
+
912
+ if positional_params && positional_params.each_type.any? {|t| s.apply?(t) }
913
+ pp = positional_params.subst(s)
914
+ end
915
+ if keyword_params && keyword_params.each_type.any? {|t| s.apply?(t) }
916
+ kp = keyword_params.subst(s)
917
+ end
918
+
919
+ self.class.new(positional_params: pp, keyword_params: kp)
920
+ end
921
+
922
+ def size
923
+ (positional_params&.size || 0) + keyword_params.size
924
+ end
925
+
926
+ def to_s
927
+ required = self.required.map {|ty| ty.to_s }
928
+ optional = self.optional.map {|ty| "?#{ty}" }
929
+ rest = self.rest ? ["*#{self.rest}"] : [] #: Array[String]
930
+ required_keywords = keyword_params.requireds.map {|name, type| "#{name}: #{type}" }
931
+ optional_keywords = keyword_params.optionals.map {|name, type| "?#{name}: #{type}"}
932
+ rest_keywords = keyword_params.rest ? ["**#{keyword_params.rest}"] : [] #: Array[String]
933
+ "(#{(required + optional + rest + required_keywords + optional_keywords + rest_keywords).join(", ")})"
934
+ end
935
+
936
+ def map_type(&block)
937
+ self.class.new(
938
+ positional_params: positional_params&.map_type(&block),
939
+ keyword_params: keyword_params.map_type(&block)
940
+ )
941
+ end
942
+
943
+ def empty?
944
+ !has_positional? && !has_keywords?
945
+ end
946
+
947
+ # Returns true if all arguments are non-required.
948
+ def optional?
949
+ required.empty? && required_keywords.empty?
950
+ end
951
+
952
+ # self + params returns a new params for overloading.
953
+ #
954
+ def +(other)
955
+ pp = PositionalParams.merge_for_overload(positional_params, other.positional_params)
956
+ kp = keyword_params + other.keyword_params
957
+ Params.new(positional_params: pp, keyword_params: kp)
958
+ end
959
+
960
+ # Returns the intersection between self and other.
961
+ # Returns nil if the intersection cannot be computed.
962
+ #
963
+ # (self & other) <: self
964
+ # (self & other) <: other
965
+ #
966
+ # `self & other` accept `arg` if `arg` is acceptable for both of `self` and `other`.
967
+ #
968
+ def &(other)
969
+ pp = PositionalParams.merge_for_intersection(positional_params, other.positional_params) rescue return
970
+ kp = keyword_params & other.keyword_params or return
971
+ Params.new(positional_params: pp, keyword_params: kp)
972
+ end
973
+
974
+ # Returns the union between self and other.
975
+ #
976
+ # self <: (self | other)
977
+ # other <: (self | other)
978
+ #
979
+ # `self | other` accept `arg` if `self` accepts `arg` or `other` accepts `arg`.
980
+ #
981
+ def |(other)
982
+ pp = PositionalParams.merge_for_union(positional_params, other.positional_params) rescue return
983
+ kp = keyword_params | other.keyword_params or return
984
+ Params.new(positional_params: pp, keyword_params: kp)
985
+ end
986
+ end
987
+
988
+ attr_reader :params
989
+ attr_reader :return_type
990
+ attr_reader :location
991
+
992
+ def initialize(params:, return_type:, location:)
993
+ @params = params
994
+ @return_type = return_type
995
+ @location = location
996
+ end
997
+
998
+ def ==(other)
999
+ other.is_a?(Function) && other.params == params && other.return_type == return_type
1000
+ end
1001
+
1002
+ alias eql? ==
1003
+
1004
+ def hash
1005
+ self.class.hash ^ params.hash ^ return_type.hash
1006
+ end
1007
+
1008
+ def free_variables
1009
+ @fvs ||= Set[].tap do |fvs|
1010
+ # @type var fvs: Set[AST::Types::variable]
1011
+ fvs.merge(params.free_variables) if params
1012
+ fvs.merge(return_type.free_variables)
1013
+ end
1014
+ end
1015
+
1016
+ def subst(s)
1017
+ return self if s.empty?
1018
+
1019
+ ps = params.subst(s) if params
1020
+ ret = return_type.subst(s)
1021
+
1022
+ if ps == params && ret == return_type
1023
+ self
1024
+ else
1025
+ Function.new(
1026
+ params: ps,
1027
+ return_type: ret,
1028
+ location: location
1029
+ )
1030
+ end
1031
+ end
1032
+
1033
+ def each_type(&block)
1034
+ if block
1035
+ params&.each_type(&block)
1036
+ yield return_type
1037
+ else
1038
+ enum_for :each_type
1039
+ end
1040
+ end
1041
+
1042
+ alias each_child each_type
1043
+
1044
+ def map_type(&block)
1045
+ Function.new(
1046
+ params: params&.map_type(&block),
1047
+ return_type: yield(return_type),
1048
+ location: location
1049
+ )
1050
+ end
1051
+
1052
+ def with(params: self.params, return_type: self.return_type)
1053
+ Function.new(
1054
+ params: params,
1055
+ return_type: return_type,
1056
+ location: location
1057
+ )
1058
+ end
1059
+
1060
+ def accept_one_arg?
1061
+ return false unless params
1062
+ return false unless params.keyword_params.requireds.empty?
1063
+ head = params.positional_params or return false
1064
+
1065
+ case head.head
1066
+ when Params::PositionalParams::Required
1067
+ !head.tail.is_a?(Params::PositionalParams::Required)
1068
+ else
1069
+ true
1070
+ end
1071
+ end
1072
+
1073
+ def to_s
1074
+ if params
1075
+ "#{params} -> #{return_type}"
1076
+ else
1077
+ "(?) -> #{return_type}"
1078
+ end
1079
+ end
1080
+
1081
+ def closed?
1082
+ if params
1083
+ params.closed? && return_type.free_variables.empty?
1084
+ else
1085
+ return_type.free_variables.empty?
1086
+ end
1087
+ end
1088
+ end
1089
+ end
1090
+ end