steep 0.11.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (299) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +27 -0
  3. data/.gitmodules +3 -0
  4. data/CHANGELOG.md +5 -0
  5. data/README.md +48 -90
  6. data/Rakefile +10 -6
  7. data/Steepfile +1 -0
  8. data/bin/setup +1 -0
  9. data/bin/smoke_runner.rb +9 -14
  10. data/exe/rbs +3 -0
  11. data/exe/ruby-signature +3 -0
  12. data/exe/steep +1 -0
  13. data/lib/steep.rb +32 -26
  14. data/lib/steep/annotation_parser.rb +167 -0
  15. data/lib/steep/ast/annotation/collection.rb +7 -7
  16. data/lib/steep/ast/types.rb +60 -0
  17. data/lib/steep/ast/types/any.rb +1 -1
  18. data/lib/steep/ast/types/factory.rb +535 -0
  19. data/lib/steep/ast/types/name.rb +3 -3
  20. data/lib/steep/ast/types/var.rb +1 -1
  21. data/lib/steep/cli.rb +56 -240
  22. data/lib/steep/drivers/annotations.rb +36 -19
  23. data/lib/steep/drivers/check.rb +55 -91
  24. data/lib/steep/drivers/init.rb +54 -0
  25. data/lib/steep/drivers/langserver.rb +241 -150
  26. data/lib/steep/drivers/print_project.rb +56 -0
  27. data/lib/steep/drivers/signature_error_printer.rb +25 -0
  28. data/lib/steep/drivers/trace_printer.rb +25 -0
  29. data/lib/steep/drivers/utils/driver_helper.rb +26 -0
  30. data/lib/steep/drivers/validate.rb +18 -38
  31. data/lib/steep/drivers/vendor.rb +46 -0
  32. data/lib/steep/drivers/watch.rb +78 -140
  33. data/lib/steep/errors.rb +22 -13
  34. data/lib/steep/interface/interface.rb +91 -0
  35. data/lib/steep/interface/method.rb +0 -4
  36. data/lib/steep/interface/method_type.rb +362 -2
  37. data/lib/steep/interface/substitution.rb +22 -0
  38. data/lib/steep/project.rb +25 -233
  39. data/lib/steep/project/dsl.rb +132 -0
  40. data/lib/steep/project/file.rb +93 -76
  41. data/lib/steep/project/file_loader.rb +63 -0
  42. data/lib/steep/project/options.rb +7 -0
  43. data/lib/steep/project/target.rb +190 -0
  44. data/lib/steep/signature/errors.rb +25 -77
  45. data/lib/steep/signature/validator.rb +122 -0
  46. data/lib/steep/source.rb +12 -7
  47. data/lib/steep/subtyping/check.rb +357 -633
  48. data/lib/steep/subtyping/constraints.rb +2 -2
  49. data/lib/steep/subtyping/trace.rb +23 -0
  50. data/lib/steep/type_construction.rb +509 -455
  51. data/lib/steep/type_inference/constant_env.rb +16 -24
  52. data/lib/steep/type_inference/type_env.rb +26 -18
  53. data/lib/steep/version.rb +1 -1
  54. data/sample/Steepfile +6 -0
  55. data/sample/lib/conference.rb +12 -0
  56. data/sample/sig/conference.rbs +6 -0
  57. data/smoke/alias/Steepfile +4 -0
  58. data/smoke/alias/a.rb +2 -2
  59. data/smoke/alias/{a.rbi → a.rbs} +1 -1
  60. data/smoke/and/Steepfile +4 -0
  61. data/smoke/array/Steepfile +4 -0
  62. data/smoke/array/a.rb +2 -2
  63. data/smoke/array/b.rb +4 -4
  64. data/smoke/array/c.rb +2 -2
  65. data/smoke/block/Steepfile +5 -0
  66. data/smoke/block/{a.rbi → a.rbs} +1 -1
  67. data/smoke/block/{c.rbi → c.rbs} +0 -0
  68. data/smoke/block/d.rb +6 -6
  69. data/smoke/case/Steepfile +4 -0
  70. data/smoke/case/a.rb +4 -3
  71. data/smoke/class/Steepfile +4 -0
  72. data/smoke/class/a.rb +1 -4
  73. data/smoke/class/a.rbs +24 -0
  74. data/smoke/class/h.rb +6 -2
  75. data/smoke/class/{h.rbi → h.rbs} +1 -2
  76. data/smoke/class/i.rb +1 -2
  77. data/smoke/class/i.rbs +9 -0
  78. data/smoke/const/Steepfile +4 -0
  79. data/smoke/dstr/Steepfile +4 -0
  80. data/smoke/ensure/Steepfile +4 -0
  81. data/smoke/ensure/a.rb +1 -1
  82. data/smoke/enumerator/Steepfile +4 -0
  83. data/smoke/enumerator/a.rb +7 -7
  84. data/smoke/enumerator/b.rb +6 -6
  85. data/smoke/extension/Steepfile +4 -0
  86. data/smoke/extension/{a.rbi → a.rbs} +2 -2
  87. data/smoke/extension/{e.rbi → e.rbs} +2 -2
  88. data/smoke/hash/Steepfile +4 -0
  89. data/smoke/hash/{a.rbi → a.rbs} +0 -0
  90. data/smoke/hash/b.rb +2 -2
  91. data/smoke/hash/c.rb +1 -1
  92. data/smoke/hash/e.rbs +3 -0
  93. data/smoke/hash/f.rb +1 -1
  94. data/smoke/hello/Steepfile +4 -0
  95. data/smoke/hello/hello.rbs +7 -0
  96. data/smoke/if/Steepfile +4 -0
  97. data/smoke/implements/Steepfile +4 -0
  98. data/smoke/implements/a.rbs +6 -0
  99. data/smoke/initialize/Steepfile +4 -0
  100. data/smoke/initialize/a.rbs +3 -0
  101. data/smoke/integer/Steepfile +4 -0
  102. data/smoke/integer/a.rb +5 -3
  103. data/smoke/interface/Steepfile +4 -0
  104. data/smoke/interface/{a.rbi → a.rbs} +0 -0
  105. data/smoke/kwbegin/Steepfile +4 -0
  106. data/smoke/lambda/Steepfile +4 -0
  107. data/smoke/lambda/a.rb +9 -2
  108. data/smoke/literal/Steepfile +4 -0
  109. data/smoke/literal/{literal_methods.rbi → literal_methods.rbs} +0 -0
  110. data/smoke/map/Steepfile +4 -0
  111. data/smoke/map/a.rb +1 -1
  112. data/smoke/method/Steepfile +4 -0
  113. data/smoke/method/{a.rbi → a.rbs} +0 -0
  114. data/smoke/method/b.rb +1 -4
  115. data/smoke/method/d.rb +1 -0
  116. data/smoke/method/d.rbs +3 -0
  117. data/smoke/module/Steepfile +4 -0
  118. data/smoke/module/a.rb +1 -1
  119. data/smoke/module/a.rbs +16 -0
  120. data/smoke/module/c.rb +1 -1
  121. data/smoke/regexp/Steepfile +4 -0
  122. data/smoke/regexp/a.rb +2 -2
  123. data/smoke/regexp/b.rb +16 -16
  124. data/smoke/regression/Steepfile +5 -0
  125. data/smoke/regression/array.rb +2 -2
  126. data/smoke/regression/hash.rb +2 -2
  127. data/smoke/regression/poly_new.rb +2 -0
  128. data/smoke/regression/poly_new.rbs +4 -0
  129. data/smoke/regression/set_divide.rb +2 -2
  130. data/smoke/rescue/Steepfile +4 -0
  131. data/smoke/rescue/a.rb +1 -1
  132. data/smoke/self/Steepfile +4 -0
  133. data/smoke/self/a.rbs +4 -0
  134. data/smoke/skip/Steepfile +4 -0
  135. data/smoke/stdout/Steepfile +4 -0
  136. data/smoke/stdout/{a.rbi → a.rbs} +1 -1
  137. data/smoke/super/Steepfile +4 -0
  138. data/smoke/super/a.rbs +10 -0
  139. data/smoke/type_case/Steepfile +4 -0
  140. data/smoke/type_case/a.rb +1 -1
  141. data/smoke/yield/Steepfile +4 -0
  142. data/smoke/yield/a.rb +2 -2
  143. data/steep.gemspec +14 -7
  144. data/vendor/ruby-signature/.github/workflows/ruby.yml +27 -0
  145. data/vendor/ruby-signature/.gitignore +12 -0
  146. data/vendor/ruby-signature/.rubocop.yml +15 -0
  147. data/vendor/ruby-signature/BSDL +22 -0
  148. data/vendor/ruby-signature/COPYING +56 -0
  149. data/vendor/ruby-signature/Gemfile +6 -0
  150. data/vendor/ruby-signature/README.md +93 -0
  151. data/vendor/ruby-signature/Rakefile +66 -0
  152. data/vendor/ruby-signature/bin/annotate-with-rdoc +156 -0
  153. data/vendor/ruby-signature/bin/console +14 -0
  154. data/vendor/ruby-signature/bin/query-rdoc +103 -0
  155. data/vendor/ruby-signature/bin/setup +10 -0
  156. data/vendor/ruby-signature/bin/sort +88 -0
  157. data/vendor/ruby-signature/bin/test_runner.rb +17 -0
  158. data/vendor/ruby-signature/docs/CONTRIBUTING.md +97 -0
  159. data/vendor/ruby-signature/docs/sigs.md +148 -0
  160. data/vendor/ruby-signature/docs/stdlib.md +152 -0
  161. data/vendor/ruby-signature/docs/syntax.md +528 -0
  162. data/vendor/ruby-signature/exe/rbs +3 -0
  163. data/vendor/ruby-signature/exe/ruby-signature +7 -0
  164. data/vendor/ruby-signature/lib/ruby/signature.rb +64 -0
  165. data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +29 -0
  166. data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +29 -0
  167. data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +391 -0
  168. data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +364 -0
  169. data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +52 -0
  170. data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +54 -0
  171. data/vendor/ruby-signature/lib/ruby/signature/cli.rb +534 -0
  172. data/vendor/ruby-signature/lib/ruby/signature/constant.rb +28 -0
  173. data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +152 -0
  174. data/vendor/ruby-signature/lib/ruby/signature/definition.rb +172 -0
  175. data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +921 -0
  176. data/vendor/ruby-signature/lib/ruby/signature/environment.rb +283 -0
  177. data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +138 -0
  178. data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +126 -0
  179. data/vendor/ruby-signature/lib/ruby/signature/errors.rb +189 -0
  180. data/vendor/ruby-signature/lib/ruby/signature/location.rb +104 -0
  181. data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +125 -0
  182. data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +93 -0
  183. data/vendor/ruby-signature/lib/ruby/signature/parser.y +1343 -0
  184. data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +441 -0
  185. data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +579 -0
  186. data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +383 -0
  187. data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +48 -0
  188. data/vendor/ruby-signature/lib/ruby/signature/test.rb +28 -0
  189. data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +63 -0
  190. data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +290 -0
  191. data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +58 -0
  192. data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +324 -0
  193. data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +185 -0
  194. data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +256 -0
  195. data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +72 -0
  196. data/vendor/ruby-signature/lib/ruby/signature/types.rb +932 -0
  197. data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +140 -0
  198. data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +49 -0
  199. data/vendor/ruby-signature/lib/ruby/signature/version.rb +5 -0
  200. data/vendor/ruby-signature/lib/ruby/signature/writer.rb +271 -0
  201. data/vendor/ruby-signature/ruby-signature.gemspec +45 -0
  202. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +3 -0
  203. data/vendor/ruby-signature/stdlib/base64/base64.rbs +15 -0
  204. data/vendor/ruby-signature/stdlib/builtin/array.rbs +1997 -0
  205. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +280 -0
  206. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +177 -0
  207. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +35 -0
  208. data/vendor/ruby-signature/stdlib/builtin/class.rbs +145 -0
  209. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +116 -0
  210. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +400 -0
  211. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +37 -0
  212. data/vendor/ruby-signature/stdlib/builtin/data.rbs +5 -0
  213. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +2 -0
  214. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +419 -0
  215. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +606 -0
  216. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +404 -0
  217. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +260 -0
  218. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +781 -0
  219. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +582 -0
  220. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +193 -0
  221. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +40 -0
  222. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +68 -0
  223. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +12 -0
  224. data/vendor/ruby-signature/stdlib/builtin/file.rbs +476 -0
  225. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +59 -0
  226. data/vendor/ruby-signature/stdlib/builtin/float.rbs +696 -0
  227. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +121 -0
  228. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +1029 -0
  229. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +710 -0
  230. data/vendor/ruby-signature/stdlib/builtin/io.rbs +683 -0
  231. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +574 -0
  232. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +135 -0
  233. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +141 -0
  234. data/vendor/ruby-signature/stdlib/builtin/math.rbs +66 -0
  235. data/vendor/ruby-signature/stdlib/builtin/method.rbs +182 -0
  236. data/vendor/ruby-signature/stdlib/builtin/module.rbs +248 -0
  237. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +82 -0
  238. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +409 -0
  239. data/vendor/ruby-signature/stdlib/builtin/object.rbs +824 -0
  240. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +426 -0
  241. data/vendor/ruby-signature/stdlib/builtin/process.rbs +354 -0
  242. data/vendor/ruby-signature/stdlib/builtin/random.rbs +93 -0
  243. data/vendor/ruby-signature/stdlib/builtin/range.rbs +226 -0
  244. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +424 -0
  245. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +10 -0
  246. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +131 -0
  247. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +14 -0
  248. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +55 -0
  249. data/vendor/ruby-signature/stdlib/builtin/string.rbs +770 -0
  250. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +13 -0
  251. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +40 -0
  252. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +230 -0
  253. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +1112 -0
  254. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +23 -0
  255. data/vendor/ruby-signature/stdlib/builtin/time.rbs +739 -0
  256. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +91 -0
  257. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +46 -0
  258. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +159 -0
  259. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +17 -0
  260. data/vendor/ruby-signature/stdlib/erb/erb.rbs +18 -0
  261. data/vendor/ruby-signature/stdlib/find/find.rbs +44 -0
  262. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +21 -0
  263. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +23 -0
  264. data/vendor/ruby-signature/stdlib/prime/prime.rbs +188 -0
  265. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +9 -0
  266. data/vendor/ruby-signature/stdlib/set/set.rbs +77 -0
  267. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +53 -0
  268. metadata +244 -54
  269. data/.travis.yml +0 -7
  270. data/lib/steep/ast/signature/alias.rb +0 -19
  271. data/lib/steep/ast/signature/class.rb +0 -33
  272. data/lib/steep/ast/signature/const.rb +0 -17
  273. data/lib/steep/ast/signature/env.rb +0 -138
  274. data/lib/steep/ast/signature/extension.rb +0 -21
  275. data/lib/steep/ast/signature/gvar.rb +0 -17
  276. data/lib/steep/ast/signature/interface.rb +0 -31
  277. data/lib/steep/ast/signature/members.rb +0 -115
  278. data/lib/steep/ast/signature/module.rb +0 -21
  279. data/lib/steep/drivers/print_interface.rb +0 -94
  280. data/lib/steep/drivers/scaffold.rb +0 -321
  281. data/lib/steep/drivers/utils/each_signature.rb +0 -31
  282. data/lib/steep/interface/abstract.rb +0 -68
  283. data/lib/steep/interface/builder.rb +0 -637
  284. data/lib/steep/interface/instantiated.rb +0 -163
  285. data/lib/steep/interface/ivar_chain.rb +0 -26
  286. data/lib/steep/parser.y +0 -1278
  287. data/lib/steep/project/listener.rb +0 -53
  288. data/smoke/class/a.rbi +0 -24
  289. data/smoke/class/d.rb +0 -9
  290. data/smoke/class/e.rb +0 -12
  291. data/smoke/class/i.rbi +0 -9
  292. data/smoke/hash/e.rbi +0 -3
  293. data/smoke/hello/hello.rbi +0 -7
  294. data/smoke/implements/a.rbi +0 -6
  295. data/smoke/initialize/a.rbi +0 -3
  296. data/smoke/module/a.rbi +0 -16
  297. data/smoke/self/a.rbi +0 -4
  298. data/smoke/super/a.rbi +0 -10
  299. data/stdlib/builtin.rbi +0 -787
@@ -0,0 +1,932 @@
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