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
@@ -199,7 +199,7 @@ module Steep
199
199
  end
200
200
  end
201
201
 
202
- def solution(checker, variance:, variables:)
202
+ def solution(checker, variance:, variables:, self_type:)
203
203
  vars = []
204
204
  types = []
205
205
 
@@ -210,7 +210,7 @@ module Steep
210
210
  lower_bound = lower_bound(var)
211
211
  relation = Relation.new(sub_type: lower_bound, super_type: upper_bound)
212
212
 
213
- checker.check(relation, constraints: self.class.empty).yield_self do |result|
213
+ checker.check(relation, self_type: self_type, constraints: self.class.empty).yield_self do |result|
214
214
  if result.success?
215
215
  vars << var
216
216
 
@@ -7,6 +7,29 @@ module Steep
7
7
  @array = array
8
8
  end
9
9
 
10
+ def interface(sub, sup, &block)
11
+ push :interface, sub, sup, &block
12
+ end
13
+
14
+ def method(name, sub, sup, &block)
15
+ push :method, sub, sup, name, &block
16
+ end
17
+
18
+ def method_type(name, sub, sup, &block)
19
+ push :method_type, sub, sup, name, &block
20
+ end
21
+
22
+ def type(sub, sup, &block)
23
+ push :type, sub, sup, &block
24
+ end
25
+
26
+ def push(*xs)
27
+ array << xs
28
+ yield
29
+ ensure
30
+ array.pop
31
+ end
32
+
10
33
  def add(sup, sub)
11
34
  array << [sup, sub]
12
35
  yield
@@ -6,22 +6,20 @@ module Steep
6
6
  attr_reader :method_type
7
7
  attr_reader :return_type
8
8
  attr_reader :constructor
9
+ attr_reader :super_method
9
10
 
10
- def initialize(name:, method:, method_type:, return_type:, constructor:)
11
+ def initialize(name:, method:, method_type:, return_type:, constructor:, super_method:)
11
12
  @name = name
12
13
  @method = method
13
14
  @return_type = return_type
14
15
  @method_type = method_type
15
16
  @constructor = constructor
17
+ @super_method = super_method
16
18
  end
17
19
 
18
20
  def block_type
19
21
  method_type&.block
20
22
  end
21
-
22
- def super_method
23
- method&.super_method
24
- end
25
23
  end
26
24
 
27
25
  class BlockContext
@@ -51,8 +49,10 @@ module Steep
51
49
  attr_reader :implement_name
52
50
  attr_reader :current_namespace
53
51
  attr_reader :class_name
52
+ attr_reader :instance_definition
53
+ attr_reader :module_definition
54
54
 
55
- def initialize(instance_type:, module_type:, implement_name:, current_namespace:, const_env:, class_name:)
55
+ def initialize(instance_type:, module_type:, implement_name:, current_namespace:, const_env:, class_name:, instance_definition: nil, module_definition: nil)
56
56
  @instance_type = instance_type
57
57
  @module_type = module_type
58
58
  @defined_instance_methods = Set.new
@@ -61,6 +61,8 @@ module Steep
61
61
  @current_namespace = current_namespace
62
62
  @const_env = const_env
63
63
  @class_name = class_name
64
+ @instance_definition = instance_definition
65
+ @module_definition = module_definition
64
66
  end
65
67
 
66
68
  def const_context
@@ -107,8 +109,12 @@ module Steep
107
109
  )
108
110
  end
109
111
 
110
- def for_new_method(method_name, node, args:, self_type:)
111
- annots = source.annotations(block: node, builder: checker.builder, current_module: current_namespace)
112
+ def check_relation(sub_type:, super_type:, constraints: Subtyping::Constraints.empty)
113
+ checker.check(Subtyping::Relation.new(sub_type: sub_type, super_type: super_type), self_type: self_type, constraints: constraints)
114
+ end
115
+
116
+ def for_new_method(method_name, node, args:, self_type:, definition:)
117
+ annots = source.annotations(block: node, factory: checker.factory, current_module: current_namespace)
112
118
  type_env = TypeInference::TypeEnv.new(subtyping: checker,
113
119
  const_env: module_context&.const_env || self.type_env.const_env)
114
120
 
@@ -116,151 +122,83 @@ module Steep
116
122
  type_env.set(const: name, type: type)
117
123
  end
118
124
 
119
- self_type = expand_alias(annots.self_type || self_type)
120
-
121
- self_interface = self_type && (self_type != AST::Builtin.any_type || nil) && case self_type
122
- when AST::Types::Name::Instance
123
- yield_self do
124
- class_type = AST::Types::Name::Class.new(name: self_type.name, constructor: false)
125
- checker.resolve_instance(self_type,
126
- self_type: self_type,
127
- instance_type: self_type,
128
- module_type: class_type,
129
- with_private: true)
130
- end
131
- else
132
- checker.resolve(self_type)
133
- end
134
- interface_method = self_interface&.yield_self do |interface|
135
- interface.methods[method_name]&.yield_self do |method|
136
- if self_type.is_a?(AST::Types::Name::Base) && method.type_name == self_type.name
137
- method
138
- else
139
- Interface::Method.new(type_name: self_type,
140
- name: method_name,
141
- types: method.types,
142
- super_method: method,
143
- attributes: [])
144
- end
145
- end
146
- end
147
-
148
- annotation_method = annotations.method_type(method_name)&.yield_self do |method_type|
149
- subst = Interface::Substitution.build([],
150
- instance_type: module_context&.instance_type || AST::Types::Instance.new,
151
- module_type: module_context&.module_type || AST::Types::Class.new,
152
- self_type: self_type)
153
- Interface::Method.new(type_name: nil,
154
- name: method_name,
155
- types: [method_type.subst(subst)],
156
- super_method: interface_method&.super_method,
157
- attributes: [])
158
- end
159
-
160
- if interface_method && annotation_method
161
- interface_types = interface_method.types.map do |method_type|
162
- subst = Interface::Substitution.build(method_type.type_params)
163
- method_type.instantiate(subst)
164
- end
165
-
166
- unknowns = []
167
- annotation_types = annotation_method.types.each.with_object([]) do |method_type, array|
168
- fresh = method_type.type_params.map {|var| AST::Types::Var.fresh(var) }
169
- unknowns.push(*fresh)
170
-
171
- subst = Interface::Substitution.build(method_type.type_params, fresh)
172
- array << method_type.instantiate(subst)
173
- end
174
-
175
- constraints = Subtyping::Constraints.new(unknowns: unknowns)
176
- interface_types.each do |type|
177
- constraints.add_var(*type.free_variables.to_a)
178
- end
125
+ definition_method_type = if definition
126
+ definition.methods[method_name]&.yield_self do |method|
127
+ method.method_types
128
+ .map {|method_type| checker.factory.method_type(method_type, self_type: self_type) }
129
+ .select {|method_type| method_type.is_a?(Interface::MethodType) }
130
+ .inject {|t1, t2| t1 + t2}
131
+ end
132
+ end
133
+ annotation_method_type = annotations.method_type(method_name)
179
134
 
180
- result = checker.check_method(method_name,
181
- annotation_method.with_types(annotation_types),
182
- interface_method.with_types(interface_types),
183
- assumption: Set.new,
184
- trace: Subtyping::Trace.new,
185
- constraints: constraints)
135
+ method_type = annotation_method_type || definition_method_type
186
136
 
187
- if result.failure?
188
- typing.add_error Errors::IncompatibleMethodTypeAnnotation.new(
189
- node: node,
190
- annotation_method: annotation_method,
191
- interface_method: interface_method,
192
- result: result
193
- )
137
+ if annots&.return_type && method_type&.return_type
138
+ check_relation(sub_type: annots.return_type, super_type: method_type.return_type).else do |result|
139
+ typing.add_error Errors::MethodReturnTypeAnnotationMismatch.new(node: node,
140
+ method_type: method_type.return_type,
141
+ annotation_type: annots.return_type,
142
+ result: result)
194
143
  end
195
144
  end
196
145
 
197
- method = annotation_method || interface_method
146
+ # constructor_method = method&.attributes&.include?(:constructor)
198
147
 
199
- case
200
- when method && method.types.size == 1
201
- method_type = method.types.first
202
- return_type = method_type.return_type
148
+ if method_type
203
149
  var_types = TypeConstruction.parameter_types(args, method_type)
204
- unless TypeConstruction.valid_parameter_env?(var_types, args.reject {|arg| arg.type == :blockarg }, method_type.params)
150
+ unless TypeConstruction.valid_parameter_env?(var_types, args.reject {|arg| arg.type == :blockarg}, method_type.params)
205
151
  typing.add_error Errors::MethodArityMismatch.new(node: node)
206
152
  end
207
-
208
- if (block_arg = args.find {|arg| arg.type == :blockarg })
209
- if method_type.block
210
- block_type = if method_type.block.optional?
211
- AST::Types::Union.build(types: [method_type.block.type, AST::Builtin.nil_type])
212
- else
213
- method_type.block.type
214
- end
215
- var_types[block_arg.children[0].name] = block_type
216
- end
217
- end
218
-
219
- when method
220
- typing.add_error Errors::MethodDefinitionWithOverloading.new(node: node, method: method)
221
- return_type = union_type(*method.types.map(&:return_type))
222
- var_types = {}
223
- else
224
- var_types = {}
225
153
  end
226
154
 
227
- if annots.return_type && return_type
228
- return_type_relation = Subtyping::Relation.new(sub_type: annots.return_type,
229
- super_type: return_type)
230
- checker.check(return_type_relation, constraints: Subtyping::Constraints.empty).else do |result|
231
- typing.add_error Errors::MethodReturnTypeAnnotationMismatch.new(node: node,
232
- method_type: return_type,
233
- annotation_type: annots.return_type,
234
- result: result)
155
+ if (block_arg = args.find {|arg| arg.type == :blockarg})
156
+ if method_type&.block
157
+ block_type = if method_type.block.optional?
158
+ AST::Types::Union.build(types: [method_type.block.type, AST::Builtin.nil_type])
159
+ else
160
+ method_type.block.type
161
+ end
162
+ var_types[block_arg.children[0].name] = block_type
235
163
  end
236
164
  end
237
165
 
238
- constructor_method = method&.attributes&.include?(:constructor)
166
+ super_method = if definition
167
+ if (this_method = definition.methods[method_name])
168
+ if module_context&.class_name == checker.factory.type_name(this_method.defined_in.name.absolute!)
169
+ this_method.super_method
170
+ else
171
+ this_method
172
+ end
173
+ end
174
+ end
239
175
 
240
176
  method_context = MethodContext.new(
241
177
  name: method_name,
242
- method: method,
178
+ method: definition && definition.methods[method_name],
243
179
  method_type: method_type,
244
- return_type: annots.return_type || return_type,
245
- constructor: constructor_method
180
+ return_type: annots.return_type || method_type&.return_type || AST::Builtin.any_type,
181
+ constructor: false,
182
+ super_method: super_method
246
183
  )
247
184
 
248
- var_types.each do |name, type|
249
- type_env.set(lvar: name, type: type)
185
+ if var_types
186
+ var_types.each do |name, type|
187
+ type_env.set(lvar: name, type: type)
188
+ end
250
189
  end
251
190
 
252
- ivar_types = {}
253
- ivar_types.merge!(self_interface.ivars) if self_interface
254
- ivar_types.merge!(annots.ivar_types)
255
-
256
- ivar_types.each do |name, type|
257
- type_env.set(ivar: name, type: type)
191
+ if definition
192
+ definition.instance_variables.each do |name, decl|
193
+ type_env.set(ivar: name, type: checker.factory.type(decl.type))
194
+ end
258
195
  end
259
196
 
260
197
  type_env = type_env.with_annotations(
261
198
  lvar_types: annots.lvar_types,
262
199
  ivar_types: annots.ivar_types,
263
200
  const_types: annots.const_types,
201
+ self_type: annots.self_type || self_type
264
202
  )
265
203
 
266
204
  self.class.new(
@@ -269,7 +207,7 @@ module Steep
269
207
  annotations: annots,
270
208
  type_env: type_env,
271
209
  block_context: nil,
272
- self_type: self_type,
210
+ self_type: annots.self_type || self_type,
273
211
  method_context: method_context,
274
212
  typing: typing,
275
213
  module_context: module_context,
@@ -281,13 +219,13 @@ module Steep
281
219
  new_module_name = Names::Module.from_node(node.children.first) or raise "Unexpected module name: #{node.children.first}"
282
220
  new_namespace = nested_namespace_for_module(new_module_name)
283
221
 
284
- annots = source.annotations(block: node, builder: checker.builder, current_module: new_namespace)
222
+ annots = source.annotations(block: node, factory: checker.factory, current_module: new_namespace)
285
223
  module_type = AST::Builtin::Module.instance_type
286
224
 
287
225
  implement_module_name = yield_self do
288
226
  if (annotation = annots.implement_module_annotation)
289
227
  absolute_name(annotation.name.name).yield_self do |absolute_name|
290
- if checker.builder.signatures.module_name?(absolute_name)
228
+ if checker.factory.module_name?(absolute_name)
291
229
  AST::Annotation::Implements::Module.new(name: absolute_name,
292
230
  args: annotation.name.args)
293
231
  else
@@ -297,10 +235,11 @@ module Steep
297
235
  end
298
236
  else
299
237
  absolute_name(new_module_name).yield_self do |absolute_name|
300
- if checker.builder.signatures.module_name?(absolute_name)
301
- signature = checker.builder.signatures.find_module(absolute_name)
238
+ if checker.factory.module_name?(absolute_name)
239
+ absolute_name_ = checker.factory.type_name_1(absolute_name)
240
+ decl = checker.factory.env.find_class(absolute_name_)
302
241
  AST::Annotation::Implements::Module.new(name: absolute_name,
303
- args: signature.params&.variables || [])
242
+ args: decl.type_params.each.map(&:name))
304
243
  end
305
244
  end
306
245
  end
@@ -308,22 +247,25 @@ module Steep
308
247
 
309
248
  if implement_module_name
310
249
  module_name = implement_module_name.name
311
- module_args = implement_module_name.args.map {|x| AST::Types::Var.new(name: x) }
312
-
313
- abstract = checker.builder.build_module(module_name)
314
-
315
- instance_type = AST::Types::Name::Instance.new(name: module_name, args: module_args)
316
-
317
- unless abstract.supers.empty?
318
- instance_type = AST::Types::Intersection.build(
319
- types: [instance_type, AST::Builtin::Object.instance_type] + abstract.supers.map {|x| absolute_type(x) }
320
- )
321
- end
250
+ module_args = implement_module_name.args.map {|x| AST::Types::Var.new(name: x)}
251
+
252
+ type_name_ = checker.factory.type_name_1(implement_module_name.name)
253
+ module_decl = checker.factory.definition_builder.env.find_class(type_name_)
254
+ instance_def = checker.factory.definition_builder.build_instance(type_name_)
255
+ module_def = checker.factory.definition_builder.build_singleton(type_name_)
256
+
257
+ instance_type = AST::Types::Intersection.build(
258
+ types: [
259
+ AST::Types::Name::Instance.new(name: module_name, args: module_args),
260
+ AST::Builtin::Object.instance_type,
261
+ module_decl.self_type&.yield_self {|ty|
262
+ absolute_type = checker.factory.env.absolute_type(ty, namespace: module_decl.name.absolute!.namespace)
263
+ checker.factory.type(absolute_type)
264
+ }
265
+ ].compact
266
+ )
322
267
 
323
- module_type = AST::Types::Intersection.build(types: [
324
- AST::Builtin::Module.instance_type,
325
- AST::Types::Name::Module.new(name: module_name)
326
- ])
268
+ module_type = AST::Types::Name::Class.new(name: module_name, constructor: nil)
327
269
  end
328
270
 
329
271
  if annots.instance_type
@@ -339,7 +281,7 @@ module Steep
339
281
  else
340
282
  Names::Module.new(name: new_namespace.path.last, namespace: new_namespace.parent)
341
283
  end
342
- module_const_env = TypeInference::ConstantEnv.new(builder: checker.builder, context: const_context)
284
+ module_const_env = TypeInference::ConstantEnv.new(factory: checker.factory, context: const_context)
343
285
 
344
286
  module_context_ = ModuleContext.new(
345
287
  instance_type: instance_type,
@@ -347,13 +289,15 @@ module Steep
347
289
  implement_name: implement_module_name,
348
290
  current_namespace: new_namespace,
349
291
  const_env: module_const_env,
350
- class_name: absolute_name(new_module_name)
292
+ class_name: absolute_name(new_module_name),
293
+ instance_definition: instance_def,
294
+ module_definition: module_def
351
295
  )
352
296
 
353
297
  module_type_env = TypeInference::TypeEnv.build(annotations: annots,
354
298
  subtyping: checker,
355
299
  const_env: module_const_env,
356
- signatures: checker.builder.signatures)
300
+ signatures: checker.factory.env)
357
301
 
358
302
  self.class.new(
359
303
  checker: checker,
@@ -374,12 +318,12 @@ module Steep
374
318
  super_class_name = node.children[1] && Names::Module.from_node(node.children[1])
375
319
  new_namespace = nested_namespace_for_module(new_class_name)
376
320
 
377
- annots = source.annotations(block: node, builder: checker.builder, current_module: new_namespace)
321
+ annots = source.annotations(block: node, factory: checker.factory, current_module: new_namespace)
378
322
 
379
323
  implement_module_name = yield_self do
380
324
  if (annotation = annots.implement_module_annotation)
381
325
  absolute_name(annotation.name.name).yield_self do |absolute_name|
382
- if checker.builder.signatures.class_name?(absolute_name)
326
+ if checker.factory.class_name?(absolute_name)
383
327
  AST::Annotation::Implements::Module.new(name: absolute_name,
384
328
  args: annotation.name.args)
385
329
  else
@@ -390,16 +334,17 @@ module Steep
390
334
  else
391
335
  name = nil
392
336
  name ||= absolute_name(new_class_name).yield_self do |absolute_name|
393
- absolute_name if checker.builder.signatures.class_name?(absolute_name)
337
+ absolute_name if checker.factory.class_name?(absolute_name)
394
338
  end
395
339
  name ||= super_class_name && absolute_name(super_class_name).yield_self do |absolute_name|
396
- absolute_name if checker.builder.signatures.class_name?(absolute_name)
340
+ absolute_name if checker.factory.class_name?(absolute_name)
397
341
  end
398
342
 
399
343
  if name
400
- signature = checker.builder.signatures.find_class(name)
344
+ absolute_name_ = checker.factory.type_name_1(name)
345
+ decl = checker.factory.env.find_class(absolute_name_)
401
346
  AST::Annotation::Implements::Module.new(name: name,
402
- args: signature.params&.variables || [])
347
+ args: decl.type_params.each.map(&:name))
403
348
  end
404
349
  end
405
350
  end
@@ -410,12 +355,22 @@ module Steep
410
355
 
411
356
  if implement_module_name
412
357
  class_name = implement_module_name.name
413
- class_args = implement_module_name.args.map {|x| AST::Types::Var.new(name: x) }
358
+ class_args = implement_module_name.args.map {|x| AST::Types::Var.new(name: x)}
414
359
 
415
- _ = checker.builder.build_instance(class_name)
360
+ type_name_ = checker.factory.type_name_1(implement_module_name.name)
361
+ instance_def = checker.factory.definition_builder.build_instance(type_name_)
362
+ module_def = checker.factory.definition_builder.build_singleton(type_name_)
416
363
 
417
364
  instance_type = AST::Types::Name::Instance.new(name: class_name, args: class_args)
418
- module_type = AST::Types::Name::Class.new(name: class_name, constructor: true)
365
+ module_type = AST::Types::Name::Class.new(name: class_name, constructor: nil)
366
+ end
367
+
368
+ if annots.instance_type
369
+ instance_type = annots.instance_type
370
+ end
371
+
372
+ if annots.module_type
373
+ module_type = annots.module_type
419
374
  end
420
375
 
421
376
  const_context = if new_namespace.empty?
@@ -423,7 +378,7 @@ module Steep
423
378
  else
424
379
  Names::Module.new(name: new_namespace.path.last, namespace: new_namespace.parent)
425
380
  end
426
- class_const_env = TypeInference::ConstantEnv.new(builder: checker.builder, context: const_context)
381
+ class_const_env = TypeInference::ConstantEnv.new(factory: checker.factory, context: const_context)
427
382
 
428
383
  module_context = ModuleContext.new(
429
384
  instance_type: annots.instance_type || instance_type,
@@ -431,15 +386,16 @@ module Steep
431
386
  implement_name: implement_module_name,
432
387
  current_namespace: new_namespace,
433
388
  const_env: class_const_env,
434
- class_name: absolute_name(new_class_name)
389
+ class_name: absolute_name(new_class_name),
390
+ module_definition: module_def,
391
+ instance_definition: instance_def
435
392
  )
436
393
 
437
- class_type_env = TypeInference::TypeEnv.build(
438
- annotations: annots,
439
- const_env: class_const_env,
440
- signatures: checker.builder.signatures,
441
- subtyping: checker
442
- )
394
+ class_type_env = TypeInference::TypeEnv.build(annotations: annots,
395
+ subtyping: checker,
396
+ const_env: class_const_env,
397
+ signatures: checker.factory.env)
398
+
443
399
 
444
400
  self.class.new(
445
401
  checker: checker,
@@ -456,7 +412,7 @@ module Steep
456
412
  end
457
413
 
458
414
  def for_branch(node, truthy_vars: Set.new, type_case_override: nil)
459
- annots = source.annotations(block: node, builder: checker.builder, current_module: current_namespace)
415
+ annots = source.annotations(block: node, factory: checker.factory, current_module: current_namespace)
460
416
 
461
417
  type_env = self.type_env
462
418
 
@@ -467,12 +423,12 @@ module Steep
467
423
  env[var] = type
468
424
  end
469
425
  end
470
- type_env = type_env.with_annotations(lvar_types: lvar_types) do |var, relation, result|
426
+ type_env = type_env.with_annotations(lvar_types: lvar_types, self_type: self_type) do |var, relation, result|
471
427
  raise "Unexpected annotate failure: #{relation}"
472
428
  end
473
429
 
474
430
  if type_case_override
475
- type_env = type_env.with_annotations(lvar_types: type_case_override) do |var, relation, result|
431
+ type_env = type_env.with_annotations(lvar_types: type_case_override, self_type: self_type) do |var, relation, result|
476
432
  typing.add_error(
477
433
  Errors::IncompatibleTypeCase.new(node: node,
478
434
  var_name: var,
@@ -486,7 +442,8 @@ module Steep
486
442
  lvar_types: annots.lvar_types,
487
443
  ivar_types: annots.ivar_types,
488
444
  const_types: annots.const_types,
489
- gvar_types: {}
445
+ gvar_types: {},
446
+ self_type: self_type
490
447
  ) do |var, relation, result|
491
448
  typing.add_error(
492
449
  Errors::IncompatibleAnnotation.new(node: node,
@@ -585,6 +542,7 @@ module Steep
585
542
  else
586
543
  module_type
587
544
  end
545
+
588
546
  typing.add_typing(node, type)
589
547
  else
590
548
  type_send(node, send_node: node, block_params: nil, block_body: nil)
@@ -640,38 +598,33 @@ module Steep
640
598
  when !lhs_type
641
599
  fallback_to_any(node)
642
600
  else
643
- lhs_interface = checker.resolve(lhs_type)
601
+ lhs_interface = checker.factory.interface(lhs_type, private: false)
644
602
  op_method = lhs_interface.methods[op]
645
603
 
646
604
  if op_method
647
605
  args = TypeInference::SendArgs.from_nodes([rhs])
648
- return_type_or_error = type_method_call(node,
649
- receiver_type: lhs_type,
650
- method: op_method,
651
- args: args,
652
- block_params: nil,
653
- block_body: nil)
654
-
655
- if return_type_or_error.is_a?(Errors::Base)
656
- typing.add_error return_type_or_error
657
- else
658
- result = checker.check(
659
- Subtyping::Relation.new(sub_type: return_type_or_error, super_type: lhs_type),
660
- constraints: Subtyping::Constraints.empty
661
- )
662
- if result.failure?
663
- typing.add_error(
664
- Errors::IncompatibleAssignment.new(
665
- node: node,
666
- lhs_type: lhs_type,
667
- rhs_type: return_type_or_error,
668
- result: result
669
- )
606
+ return_type, _ = type_method_call(node,
607
+ receiver_type: lhs_type,
608
+ method_name: op,
609
+ method: op_method,
610
+ args: args,
611
+ block_params: nil,
612
+ block_body: nil,
613
+ topdown_hint: true)
614
+
615
+ result = check_relation(sub_type: return_type, super_type: lhs_type)
616
+ if result.failure?
617
+ typing.add_error(
618
+ Errors::IncompatibleAssignment.new(
619
+ node: node,
620
+ lhs_type: lhs_type,
621
+ rhs_type: return_type,
622
+ result: result
670
623
  )
671
- end
624
+ )
672
625
  end
673
626
  else
674
- typing.add_error Errors::NoMethod.new(node: node, method: op, type: lhs_type)
627
+ typing.add_error Errors::NoMethod.new(node: node, method: op, type: expand_self(lhs_type))
675
628
  end
676
629
 
677
630
  typing.add_typing(node, lhs_type)
@@ -682,32 +635,35 @@ module Steep
682
635
  yield_self do
683
636
  if self_type && method_context&.method
684
637
  if method_context.super_method
685
- each_child_node(node) do |child| synthesize(child) end
638
+ each_child_node(node) do |child|
639
+ synthesize(child)
640
+ end
686
641
 
687
- super_method = method_context.super_method
642
+ super_method = Interface::Interface::Combination.overload(
643
+ method_context.super_method.method_types.map {|method_type|
644
+ checker.factory.method_type(method_type, self_type: self_type)
645
+ },
646
+ incompatible: false
647
+ )
688
648
  args = TypeInference::SendArgs.from_nodes(node.children.dup)
689
649
 
690
- return_type_or_error = type_method_call(node,
691
- receiver_type: self_type,
692
- method: super_method,
693
- args: args,
694
- block_params: nil,
695
- block_body: nil)
650
+ return_type, _ = type_method_call(node,
651
+ receiver_type: self_type,
652
+ method_name: method_context.name,
653
+ method: super_method,
654
+ args: args,
655
+ block_params: nil,
656
+ block_body: nil,
657
+ topdown_hint: true)
696
658
 
697
- if return_type_or_error.is_a?(Errors::Base)
698
- fallback_to_any node do
699
- return_type_or_error
700
- end
701
- else
702
- typing.add_typing node, return_type_or_error
703
- end
659
+ typing.add_typing node, return_type
704
660
  else
705
661
  fallback_to_any node do
706
662
  Errors::UnexpectedSuper.new(node: node, method: method_context.name)
707
663
  end
708
664
  end
709
665
  else
710
- typing.add_typing node, AST::Builtin.any_type
666
+ fallback_to_any node
711
667
  end
712
668
  end
713
669
 
@@ -725,7 +681,8 @@ module Steep
725
681
  new = for_new_method(node.children[0],
726
682
  node,
727
683
  args: node.children[1].children,
728
- self_type: module_context&.instance_type)
684
+ self_type: module_context&.instance_type,
685
+ definition: module_context&.instance_definition)
729
686
 
730
687
  each_child_node(node.children[1]) do |arg|
731
688
  new.synthesize(arg)
@@ -746,17 +703,14 @@ module Steep
746
703
  else
747
704
  return_type = expand_alias(new.method_context&.return_type)
748
705
  if return_type && !return_type.is_a?(AST::Types::Void)
749
- result = checker.check(
750
- Subtyping::Relation.new(sub_type: AST::Builtin.nil_type, super_type: return_type),
751
- constraints: Subtyping::Constraints.empty
752
- )
706
+ result = check_relation(sub_type: AST::Builtin.nil_type, super_type: return_type)
753
707
  if result.failure?
754
708
  typing.add_error(Errors::MethodBodyTypeMismatch.new(node: node,
755
709
  expected: new.method_context&.return_type,
756
710
  actual: AST::Builtin.nil_type,
757
711
  result: result))
758
712
  end
759
- end
713
+ end
760
714
  end
761
715
 
762
716
  if module_context
@@ -767,10 +721,21 @@ module Steep
767
721
 
768
722
  when :defs
769
723
  synthesize(node.children[0]).tap do |self_type|
724
+ self_type = expand_self(self_type)
725
+ definition = case self_type
726
+ when AST::Types::Name::Instance
727
+ name = checker.factory.type_name_1(self_type.name)
728
+ checker.factory.definition_builder.build_singleton(name)
729
+ when AST::Types::Name::Module, AST::Types::Name::Class
730
+ name = checker.factory.type_name_1(self_type.name)
731
+ checker.factory.definition_builder.build_singleton(name)
732
+ end
733
+
770
734
  new = for_new_method(node.children[1],
771
735
  node,
772
736
  args: node.children[2].children,
773
- self_type: self_type)
737
+ self_type: self_type,
738
+ definition: definition)
774
739
 
775
740
  each_child_node(node.children[2]) do |arg|
776
741
  new.synthesize(arg)
@@ -814,11 +779,7 @@ module Steep
814
779
 
815
780
  if (ret_type = expand_alias(method_context&.return_type))
816
781
  unless ret_type.is_a?(AST::Types::Void)
817
- result = checker.check(
818
- Subtyping::Relation.new(sub_type: value_type,
819
- super_type: ret_type),
820
- constraints: Subtyping::Constraints.empty
821
- )
782
+ result = check_relation(sub_type: value_type, super_type: ret_type)
822
783
 
823
784
  if result.failure?
824
785
  typing.add_error(Errors::ReturnTypeMismatch.new(node: node,
@@ -935,7 +896,7 @@ module Steep
935
896
 
936
897
  when :int
937
898
  yield_self do
938
- literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_) }
899
+ literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_)}
939
900
 
940
901
  if literal_type
941
902
  typing.add_typing(node, literal_type)
@@ -946,7 +907,7 @@ module Steep
946
907
 
947
908
  when :sym
948
909
  yield_self do
949
- literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_) }
910
+ literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_)}
950
911
 
951
912
  if literal_type
952
913
  typing.add_typing(node, literal_type)
@@ -957,7 +918,7 @@ module Steep
957
918
 
958
919
  when :str
959
920
  yield_self do
960
- literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_) }
921
+ literal_type = expand_alias(hint) {|hint_| test_literal_type(node.children[0], hint_)}
961
922
 
962
923
  if literal_type
963
924
  typing.add_typing(node, literal_type)
@@ -1058,11 +1019,7 @@ module Steep
1058
1019
  end
1059
1020
 
1060
1021
  when :self
1061
- if self_type
1062
- typing.add_typing(node, self_type)
1063
- else
1064
- fallback_to_any node
1065
- end
1022
+ typing.add_typing node, AST::Types::Self.new
1066
1023
 
1067
1024
  when :const
1068
1025
  const_name = Names::Module.from_node(node)
@@ -1080,7 +1037,7 @@ module Steep
1080
1037
  const_name = Names::Module.from_node(node)
1081
1038
  if const_name
1082
1039
  value_type = synthesize(node.children.last)
1083
- type = type_env.assign(const: const_name, type: value_type) do |error|
1040
+ type = type_env.assign(const: const_name, type: value_type, self_type: self_type) do |error|
1084
1041
  case error
1085
1042
  when Subtyping::Result::Failure
1086
1043
  const_type = type_env.get(const: const_name)
@@ -1128,13 +1085,17 @@ module Steep
1128
1085
  yield_self do
1129
1086
  if method_context&.method
1130
1087
  if method_context.super_method
1131
- if method_context.method.incompatible?
1132
- typing.add_error Errors::IncompatibleZuper.new(node: node, method: method_context.name)
1133
- typing.add_typing node, AST::Builtin.any_type
1134
- else
1135
- types = method_context.super_method.types.map(&:return_type)
1136
- typing.add_typing(node, union_type(*types))
1137
- end
1088
+ types = method_context.super_method.method_types.map {|method_type|
1089
+ case method_type
1090
+ when Ruby::Signature::MethodType
1091
+ checker.factory.method_type(method_type, self_type: self_type).return_type
1092
+ when :any
1093
+ AST::Builtin.any_type
1094
+ else
1095
+ raise "Unexpected method_type: #{method_type.inspect}"
1096
+ end
1097
+ }
1098
+ typing.add_typing(node, union_type(*types))
1138
1099
  else
1139
1100
  typing.add_error(Errors::UnexpectedSuper.new(node: node, method: method_context.name))
1140
1101
  fallback_to_any node
@@ -1150,11 +1111,8 @@ module Steep
1150
1111
  typing.add_error Errors::FallbackAny.new(node: node) unless hint
1151
1112
 
1152
1113
  array_type = if hint
1153
- relation = Subtyping::Relation.new(
1154
- sub_type: AST::Builtin::Array.instance_type(AST::Builtin.any_type),
1155
- super_type: hint
1156
- )
1157
- if checker.check(relation, constraints: Subtyping::Constraints.empty).success?
1114
+ if check_relation(sub_type: AST::Builtin::Array.instance_type(AST::Builtin.any_type),
1115
+ super_type: hint).success?
1158
1116
  hint
1159
1117
  end
1160
1118
  end
@@ -1165,14 +1123,13 @@ module Steep
1165
1123
 
1166
1124
  expand_alias(hint) do |hint|
1167
1125
  is_tuple = hint.is_a?(AST::Types::Tuple)
1168
- is_tuple &&= node.children.all? {|child| child.type != :splat }
1126
+ is_tuple &&= node.children.all? {|child| child.type != :splat}
1169
1127
  is_tuple &&= node.children.size >= hint.types.size
1170
1128
  is_tuple &&= hint.types.map.with_index do |child_type, index|
1171
1129
  child_node = node.children[index]
1172
1130
  [synthesize(child_node, hint: child_type), child_type]
1173
1131
  end.all? do |node_type, hint_type|
1174
- relation = Subtyping::Relation.new(sub_type: node_type, super_type: hint_type)
1175
- result = checker.check(relation, constraints: Subtyping::Constraints.empty)
1132
+ result = check_relation(sub_type: node_type, super_type: hint_type)
1176
1133
  result.success?
1177
1134
  end
1178
1135
  end
@@ -1288,9 +1245,9 @@ module Steep
1288
1245
  end
1289
1246
 
1290
1247
  if (body = clause.children.last)
1291
- if var_names && var_types && test_types.all? {|type| type.is_a?(AST::Types::Name::Class) }
1248
+ if var_names && var_types && test_types.all? {|type| type.is_a?(AST::Types::Name::Class)}
1292
1249
  var_types_in_body = test_types.flat_map {|test_type|
1293
- filtered_types = var_types.select {|var_type| var_type.is_a?(AST::Types::Name::Base) && var_type.name == test_type.name }
1250
+ filtered_types = var_types.select {|var_type| var_type.is_a?(AST::Types::Name::Base) && var_type.name == test_type.name}
1294
1251
  if filtered_types.empty?
1295
1252
  to_instance_type(test_type)
1296
1253
  else
@@ -1344,10 +1301,8 @@ module Steep
1344
1301
  types = pairs.map(&:first)
1345
1302
  envs = pairs.map(&:last)
1346
1303
 
1347
- if var_types
1348
- unless var_types.empty? || whens.last
1349
- types.push AST::Builtin.nil_type
1350
- end
1304
+ unless var_types&.empty? || whens.last
1305
+ types.push AST::Builtin.nil_type
1351
1306
  end
1352
1307
 
1353
1308
  type_env.join!(envs.compact)
@@ -1365,7 +1320,7 @@ module Steep
1365
1320
  if exn_classes
1366
1321
  case exn_classes.type
1367
1322
  when :array
1368
- exn_types = exn_classes.children.map {|child| synthesize(child) }
1323
+ exn_types = exn_classes.children.map {|child| synthesize(child)}
1369
1324
  else
1370
1325
  Steep.logger.error "Unexpected exception list: #{exn_classes.type}"
1371
1326
  end
@@ -1458,7 +1413,7 @@ module Steep
1458
1413
  end
1459
1414
 
1460
1415
  when :irange, :erange
1461
- types = node.children.map {|n| synthesize(n) }
1416
+ types = node.children.map {|n| synthesize(n)}
1462
1417
  type = AST::Builtin::Range.instance_type(union_type(*types))
1463
1418
  typing.add_typing(node, type)
1464
1419
 
@@ -1493,20 +1448,18 @@ module Steep
1493
1448
  when :gvasgn
1494
1449
  yield_self do
1495
1450
  name, rhs = node.children
1496
- type = checker.builder.absolute_type(checker.builder.signatures.find_gvar(name)&.type,
1497
- current: AST::Namespace.root)
1498
-
1499
- if type
1500
- check(rhs, type) do |_, rhs_type, result|
1501
- typing.add_error(Errors::IncompatibleAssignment.new(node: node,
1502
- lhs_type: type,
1503
- rhs_type: rhs_type,
1504
- result: result))
1505
- end
1506
- else
1507
- synthesize(rhs)
1451
+ type = type_env.get(gvar: name) do
1508
1452
  fallback_to_any node
1509
1453
  end
1454
+
1455
+ check(rhs, type) do |_, rhs_type, result|
1456
+ typing.add_error(Errors::IncompatibleAssignment.new(
1457
+ node: node,
1458
+ lhs_type: type,
1459
+ rhs_type: rhs_type,
1460
+ result: result)
1461
+ )
1462
+ end
1510
1463
  end
1511
1464
 
1512
1465
  when :gvar
@@ -1527,16 +1480,14 @@ module Steep
1527
1480
  if hint.one_arg?
1528
1481
  # Assumes Symbol#to_proc implementation
1529
1482
  param_type = hint.params.required[0]
1530
- interface = checker.resolve(param_type)
1483
+ interface = checker.factory.interface(param_type, private: true)
1531
1484
  method = interface.methods[value.children[0]]
1532
- if method
1533
- return_types = method.types.flat_map do |method_type|
1534
- if method_type.params.each_type.count == 0
1535
- [method_type.return_type]
1536
- end
1537
- end
1485
+ if method&.overload?
1486
+ return_types = method.types.select {|method_type|
1487
+ method_type.params.each_type.count == 0
1488
+ }.map(&:return_type)
1538
1489
 
1539
- if return_types.any?
1490
+ unless return_types.empty?
1540
1491
  type = AST::Types::Proc.new(params: Interface::Params.empty.update(required: [param_type]),
1541
1492
  return_type: AST::Types::Union.build(types: return_types))
1542
1493
  end
@@ -1547,6 +1498,7 @@ module Steep
1547
1498
  end
1548
1499
 
1549
1500
  type ||= synthesize(node.children[0], hint: hint)
1501
+
1550
1502
  typing.add_typing node, type
1551
1503
  end
1552
1504
 
@@ -1579,11 +1531,7 @@ module Steep
1579
1531
  def check(node, type, constraints: Subtyping::Constraints.empty)
1580
1532
  type_ = synthesize(node, hint: type)
1581
1533
 
1582
- result = checker.check(
1583
- Subtyping::Relation.new(sub_type: type_,
1584
- super_type: type),
1585
- constraints: constraints
1586
- )
1534
+ result = check_relation(sub_type: type_, super_type: type, constraints: constraints)
1587
1535
  if result.failure?
1588
1536
  yield(type, type_, result)
1589
1537
  end
@@ -1609,7 +1557,7 @@ module Steep
1609
1557
 
1610
1558
  def assign_type_to_variable(var, type, node)
1611
1559
  name = var.name
1612
- type_env.assign(lvar: name, type: type) do |result|
1560
+ type_env.assign(lvar: name, type: type, self_type: self_type) do |result|
1613
1561
  var_type = type_env.get(lvar: name)
1614
1562
  typing.add_error(Errors::IncompatibleAssignment.new(node: node,
1615
1563
  lhs_type: var_type,
@@ -1620,7 +1568,7 @@ module Steep
1620
1568
 
1621
1569
  def type_ivasgn(name, rhs, node)
1622
1570
  rhs_type = synthesize(rhs, hint: type_env.get(ivar: name) { fallback_to_any(node) })
1623
- ivar_type = type_env.assign(ivar: name, type: rhs_type) do |error|
1571
+ ivar_type = type_env.assign(ivar: name, type: rhs_type, self_type: self_type) do |error|
1624
1572
  case error
1625
1573
  when Subtyping::Result::Failure
1626
1574
  type = type_env.get(ivar: name)
@@ -1641,7 +1589,7 @@ module Steep
1641
1589
  rhs_type = expand_alias(rhs_original)
1642
1590
 
1643
1591
  case
1644
- when rhs.type == :array && lhs.children.all? {|a| a.type == :lvasgn || a.type == :ivasgn } && lhs.children.size == rhs.children.size
1592
+ when rhs.type == :array && lhs.children.all? {|a| a.type == :lvasgn || a.type == :ivasgn} && lhs.children.size == rhs.children.size
1645
1593
  pairs = lhs.children.zip(rhs.children)
1646
1594
  pairs.each do |(l, r)|
1647
1595
  case
@@ -1654,14 +1602,14 @@ module Steep
1654
1602
 
1655
1603
  typing.add_typing(node, rhs_type)
1656
1604
 
1657
- when rhs_type.is_a?(AST::Types::Tuple) && lhs.children.all? {|a| a.type == :lvasgn || a.type == :ivasgn }
1605
+ when rhs_type.is_a?(AST::Types::Tuple) && lhs.children.all? {|a| a.type == :lvasgn || a.type == :ivasgn}
1658
1606
  lhs.children.each.with_index do |asgn, index|
1659
1607
  type = rhs_type.types[index]
1660
1608
 
1661
1609
  case
1662
1610
  when asgn.type == :lvasgn && asgn.children[0].name != :_
1663
1611
  type ||= AST::Builtin.nil_type
1664
- type_env.assign(lvar: asgn.children[0].name, type: type) do |result|
1612
+ type_env.assign(lvar: asgn.children[0].name, type: type, self_type: self_type) do |result|
1665
1613
  var_type = type_env.get(lvar: asgn.children[0].name)
1666
1614
  typing.add_error(Errors::IncompatibleAssignment.new(node: node,
1667
1615
  lhs_type: var_type,
@@ -1694,7 +1642,7 @@ module Steep
1694
1642
  assign_type_to_variable(assignment.children.first, element_type, assignment)
1695
1643
  when :ivasgn
1696
1644
  assignment.children.first.yield_self do |ivar|
1697
- type_env.assign(ivar: ivar, type: element_type) do |error|
1645
+ type_env.assign(ivar: ivar, type: element_type, self_type: self_type) do |error|
1698
1646
  case error
1699
1647
  when Subtyping::Result::Failure
1700
1648
  type = type_env.get(ivar: ivar)
@@ -1713,7 +1661,7 @@ module Steep
1713
1661
  typing.add_typing node, rhs_type
1714
1662
 
1715
1663
  when rhs_type.is_a?(AST::Types::Union) &&
1716
- rhs_type.types.all? {|type| AST::Builtin::Array.instance_type?(type) }
1664
+ rhs_type.types.all? {|type| AST::Builtin::Array.instance_type?(type)}
1717
1665
 
1718
1666
  types = rhs_type.types.flat_map do |type|
1719
1667
  type.args.first
@@ -1752,7 +1700,7 @@ module Steep
1752
1700
  end
1753
1701
 
1754
1702
  def type_lambda(node, block_params:, block_body:, type_hint:)
1755
- block_annotations = source.annotations(block: node, builder: checker.builder, current_module: current_namespace)
1703
+ block_annotations = source.annotations(block: node, factory: checker.factory, current_module: current_namespace)
1756
1704
  params = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations)
1757
1705
 
1758
1706
  case type_hint
@@ -1766,57 +1714,76 @@ module Steep
1766
1714
  node_type_hint: nil,
1767
1715
  block_params: params,
1768
1716
  block_body: block_body,
1769
- block_annotations: block_annotations)
1717
+ block_annotations: block_annotations,
1718
+ topdown_hint: true)
1770
1719
 
1771
1720
  typing.add_typing node, block_type
1772
1721
  end
1773
1722
 
1774
1723
  def type_send(node, send_node:, block_params:, block_body:, unwrap: false)
1775
1724
  receiver, method_name, *arguments = send_node.children
1776
- receiver_type = receiver ? synthesize(receiver) : self_type
1725
+ receiver_type = receiver ? synthesize(receiver) : AST::Types::Self.new
1777
1726
 
1778
1727
  if unwrap
1779
1728
  receiver_type = unwrap(receiver_type)
1780
1729
  end
1781
1730
 
1782
- return_type = case expand_alias(receiver_type)
1731
+ receiver_type = expand_alias(receiver_type)
1732
+
1733
+ return_type = case receiver_type
1783
1734
  when AST::Types::Any
1784
1735
  typing.add_typing node, AST::Builtin.any_type
1785
1736
 
1786
1737
  when nil
1787
1738
  fallback_to_any node
1788
1739
 
1789
- else
1790
- begin
1791
- interface = checker.resolve(receiver_type, with_private: !receiver)
1792
-
1793
- method = interface.methods[method_name]
1794
-
1795
- if method
1796
- args = TypeInference::SendArgs.from_nodes(arguments)
1797
- return_type_or_error = type_method_call(node,
1798
- method: method,
1799
- args: args,
1800
- block_params: block_params,
1801
- block_body: block_body,
1802
- receiver_type: receiver_type)
1740
+ when AST::Types::Void, AST::Types::Bot, AST::Types::Top
1741
+ fallback_to_any node do
1742
+ Errors::NoMethod.new(node: node, method: method_name, type: receiver_type)
1743
+ end
1803
1744
 
1804
- if return_type_or_error.is_a?(Errors::Base)
1745
+ else
1746
+ case expanded_receiver_type = expand_self(receiver_type)
1747
+ when AST::Types::Self
1748
+ Steep.logger.error "`self` type cannot be resolved to concrete type"
1749
+ fallback_to_any node do
1750
+ Errors::NoMethod.new(node: node, method: method_name, type: receiver_type)
1751
+ end
1752
+ else
1753
+ begin
1754
+ interface = checker.factory.interface(receiver_type,
1755
+ private: !receiver,
1756
+ self_type: expanded_receiver_type)
1757
+
1758
+ method = interface.methods[method_name]
1759
+
1760
+ if method
1761
+ args = TypeInference::SendArgs.from_nodes(arguments)
1762
+ return_type, _ = type_method_call(node,
1763
+ method: method,
1764
+ method_name: method_name,
1765
+ args: args,
1766
+ block_params: block_params,
1767
+ block_body: block_body,
1768
+ receiver_type: receiver_type,
1769
+ topdown_hint: true)
1770
+
1771
+ typing.add_typing node, return_type
1772
+ else
1805
1773
  fallback_to_any node do
1806
- return_type_or_error
1774
+ Errors::NoMethod.new(node: node, method: method_name, type: expanded_receiver_type)
1807
1775
  end
1808
- else
1809
- typing.add_typing node, return_type_or_error
1810
1776
  end
1811
- else
1777
+ rescue => exn
1778
+ $stderr.puts exn.inspect
1779
+ exn.backtrace.each do |t|
1780
+ $stderr.puts t
1781
+ end
1782
+
1812
1783
  fallback_to_any node do
1813
- Errors::NoMethod.new(node: node, method: method_name, type: receiver_type)
1784
+ Errors::NoMethod.new(node: node, method: method_name, type: expanded_receiver_type)
1814
1785
  end
1815
1786
  end
1816
- rescue Subtyping::Check::CannotResolveError
1817
- fallback_to_any node do
1818
- Errors::NoMethod.new(node: node, method: method_name, type: receiver_type)
1819
- end
1820
1787
  end
1821
1788
  end
1822
1789
 
@@ -1837,7 +1804,7 @@ module Steep
1837
1804
  unless typing.has_type?(block_body)
1838
1805
  block_annotations = source.annotations(block: node, builder: checker.builder, current_module: current_namespace)
1839
1806
  params = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations)
1840
- pairs = params.each.map {|param| [param, AST::Builtin.any_type] }
1807
+ pairs = params.each.map {|param| [param, AST::Builtin.any_type]}
1841
1808
 
1842
1809
  for_block, _ = for_block(block_annotations: block_annotations,
1843
1810
  param_pairs: pairs,
@@ -1899,58 +1866,158 @@ module Steep
1899
1866
  ), return_type]
1900
1867
  end
1901
1868
 
1902
- def type_method_call(node, receiver_type:, method:, args:, block_params:, block_body:)
1903
- results = method.types.map do |method_type|
1904
- Steep.logger.tagged method_type.location&.source do
1905
- zips = args.zips(method_type.params, method_type.block&.type)
1906
-
1907
- type_or_error, child_typing = if zips.any?
1908
- zips.map do |arg_pairs|
1909
- child_typing = typing.new_child
1910
-
1911
- result = self.with_new_typing(child_typing).try_method_type(
1912
- node,
1913
- receiver_type: receiver_type,
1914
- method_type: method_type,
1915
- args: args,
1916
- arg_pairs: arg_pairs,
1917
- block_params: block_params,
1918
- block_body: block_body,
1919
- child_typing: child_typing
1920
- )
1921
-
1922
- unless result.is_a?(Errors::Base)
1923
- break [[result, child_typing]]
1924
- end
1925
-
1926
- [result, child_typing]
1927
- end.first
1928
- else
1929
- Steep.logger.debug(node.inspect)
1930
- [
1931
- Errors::IncompatibleArguments.new(node: node, receiver_type: receiver_type, method_type: method_type),
1932
- typing.new_child
1933
- ]
1934
- end
1935
-
1936
- [child_typing, type_or_error, method_type]
1937
- end
1869
+ def expand_self(type)
1870
+ if type.is_a?(AST::Types::Self) && self_type
1871
+ self_type
1872
+ else
1873
+ type
1938
1874
  end
1875
+ end
1939
1876
 
1940
- if (success = results.find {|_, type_or_error| !type_or_error.is_a?(Errors::Base) })
1941
- child_typing, type, _ = success
1942
- child_typing.save!
1943
- type
1944
- else
1945
- child_typing, error, method_type = results.first
1946
- typing.add_error error
1947
- child_typing.save!
1877
+ def type_method_call(node, method_name:, receiver_type:, method:, args:, block_params:, block_body:, topdown_hint:)
1878
+ case
1879
+ when method.union?
1880
+ yield_self do
1881
+ results = method.types.map do |method|
1882
+ typing.new_child do |child_typing|
1883
+ type, error = with_new_typing(child_typing).type_method_call(node,
1884
+ method_name: method_name,
1885
+ receiver_type: receiver_type,
1886
+ method: method,
1887
+ args: args,
1888
+ block_params: block_params,
1889
+ block_body: block_body,
1890
+ topdown_hint: false)
1891
+ [
1892
+ type,
1893
+ child_typing,
1894
+ error
1895
+ ]
1896
+ end
1897
+ end
1948
1898
 
1949
- case method_type.return_type
1950
- when AST::Types::Var
1951
- AST::Builtin.any_type
1952
- else
1953
- method_type.return_type
1899
+ if (type, typing, error = results.find {|_, _, error| error })
1900
+ typing.save!
1901
+ [type, error]
1902
+ else
1903
+ _, typing, _ = results.first
1904
+ typing.save!
1905
+
1906
+ [union_type(*results.map(&:first)), nil]
1907
+ end
1908
+ end
1909
+
1910
+ when method.intersection?
1911
+ yield_self do
1912
+ results = method.types.map do |method|
1913
+ typing.new_child do |child_typing|
1914
+ type, error = with_new_typing(child_typing).type_method_call(node,
1915
+ method_name: method_name,
1916
+ receiver_type: receiver_type,
1917
+ method: method,
1918
+ args: args,
1919
+ block_params: block_params,
1920
+ block_body: block_body,
1921
+ topdown_hint: false)
1922
+ [
1923
+ type,
1924
+ child_typing,
1925
+ error
1926
+ ]
1927
+ end
1928
+ end
1929
+
1930
+ successes = results.select {|_, _, error| !error }
1931
+ unless successes.empty?
1932
+ types = successes.map {|type, typing, _| type }
1933
+ typing = successes[0][1]
1934
+ typing.save!
1935
+
1936
+ [AST::Types::Intersection.build(types: types), nil]
1937
+ else
1938
+ type, typing, error = results.first
1939
+ typing.save!
1940
+
1941
+ [type, error]
1942
+ end
1943
+ end
1944
+
1945
+ when method.overload?
1946
+ yield_self do
1947
+ results = method.types.flat_map do |method_type|
1948
+ Steep.logger.tagged method_type.to_s do
1949
+ case method_type
1950
+ when Interface::MethodType
1951
+ zips = args.zips(method_type.params, method_type.block&.type)
1952
+
1953
+ zips.map do |arg_pairs|
1954
+ typing.new_child do |child_typing|
1955
+ result = self.with_new_typing(child_typing).try_method_type(
1956
+ node,
1957
+ receiver_type: receiver_type,
1958
+ method_type: method_type,
1959
+ args: args,
1960
+ arg_pairs: arg_pairs,
1961
+ block_params: block_params,
1962
+ block_body: block_body,
1963
+ child_typing: child_typing,
1964
+ topdown_hint: topdown_hint
1965
+ )
1966
+
1967
+ [result, child_typing, method_type]
1968
+ end
1969
+ end
1970
+ when :any
1971
+ typing.new_child do |child_typing|
1972
+ this = self.with_new_typing(child_typing)
1973
+
1974
+ args.args.each do |arg|
1975
+ this.synthesize(arg)
1976
+ end
1977
+
1978
+ if block_body
1979
+ this.synthesize(block_body)
1980
+ end
1981
+
1982
+ child_typing.add_typing node, AST::Builtin.any_type
1983
+
1984
+ [[AST::Builtin.any_type, child_typing, :any]]
1985
+ end
1986
+ end
1987
+ end
1988
+ end
1989
+
1990
+ unless results.empty?
1991
+ result, call_typing, method_type = results.find {|result, _, _| !result.is_a?(Errors::Base) } || results.last
1992
+ else
1993
+ method_type = method.types.last
1994
+ result = Errors::IncompatibleArguments.new(node: node, receiver_type: receiver_type, method_type: method_type)
1995
+ call_typing = typing.new_child
1996
+ end
1997
+ call_typing.save!
1998
+
1999
+ case result
2000
+ when Errors::Base
2001
+ if method.types.size == 1
2002
+ typing.add_error result
2003
+ type = case method_type.return_type
2004
+ when AST::Types::Var
2005
+ AST::Builtin.any_type
2006
+ else
2007
+ method_type.return_type
2008
+ end
2009
+ else
2010
+ typing.add_error Errors::UnresolvedOverloading.new(node: node,
2011
+ receiver_type: expand_self(receiver_type),
2012
+ method_name: method_name,
2013
+ method_types: method.types)
2014
+ type = AST::Builtin.any_type
2015
+ end
2016
+
2017
+ [type, result]
2018
+ else # Type
2019
+ [result, nil]
2020
+ end
1954
2021
  end
1955
2022
  end
1956
2023
  end
@@ -2049,7 +2116,7 @@ module Steep
2049
2116
  Steep.logger.warn("Method call with rest keywords type is detected. Rough approximation to be improved.")
2050
2117
 
2051
2118
  value_types = params.required_keywords.values +
2052
- params.optional_keywords.values.map {|type| AST::Types::Union.build(types: [type, AST::Builtin.nil_type]) } +
2119
+ params.optional_keywords.values.map {|type| AST::Types::Union.build(types: [type, AST::Builtin.nil_type])} +
2053
2120
  [params.rest_keywords]
2054
2121
 
2055
2122
  hash_type = AST::Builtin::Hash.instance_type(
@@ -2070,17 +2137,12 @@ module Steep
2070
2137
 
2071
2138
  node_type = synthesize(node, hint: hash_type)
2072
2139
 
2073
- relation = Subtyping::Relation.new(
2074
- sub_type: node_type,
2075
- super_type: hash_type
2076
- )
2077
-
2078
- checker.check(relation, constraints: constraints).else do
2140
+ check_relation(sub_type: node_type, super_type: hash_type).else do
2079
2141
  return Errors::ArgumentTypeMismatch.new(
2080
2142
  node: node,
2081
2143
  receiver_type: receiver_type,
2082
- expected: relation.super_type,
2083
- actual: relation.sub_type
2144
+ expected: hash_type,
2145
+ actual: node_type
2084
2146
  )
2085
2147
  end
2086
2148
  end
@@ -2088,8 +2150,8 @@ module Steep
2088
2150
  nil
2089
2151
  end
2090
2152
 
2091
- def try_method_type(node, receiver_type:, method_type:, args:, arg_pairs:, block_params:, block_body:, child_typing:)
2092
- fresh_types = method_type.type_params.map {|x| AST::Types::Var.fresh(x) }
2153
+ def try_method_type(node, receiver_type:, method_type:, args:, arg_pairs:, block_params:, block_body:, child_typing:, topdown_hint:)
2154
+ fresh_types = method_type.type_params.map {|x| AST::Types::Var.fresh(x)}
2093
2155
  fresh_vars = Set.new(fresh_types.map(&:name))
2094
2156
  instantiation = Interface::Substitution.build(method_type.type_params, fresh_types)
2095
2157
 
@@ -2122,20 +2184,15 @@ module Steep
2122
2184
  type = construction.synthesize(arg_node.children[0])
2123
2185
  child_typing.add_typing(arg_node, type)
2124
2186
  else
2125
- construction.synthesize(arg_node, hint: param_type)
2187
+ construction.synthesize(arg_node, hint: topdown_hint ? param_type : nil)
2126
2188
  end
2127
2189
 
2128
- relation = Subtyping::Relation.new(
2129
- sub_type: arg_type,
2130
- super_type: param_type
2131
- )
2132
-
2133
- checker.check(relation, constraints: constraints).else do |result|
2190
+ check_relation(sub_type: arg_type, super_type: param_type, constraints: constraints).else do |result|
2134
2191
  return Errors::ArgumentTypeMismatch.new(
2135
2192
  node: arg_node,
2136
2193
  receiver_type: receiver_type,
2137
- expected: relation.super_type,
2138
- actual: relation.sub_type
2194
+ expected: param_type,
2195
+ actual: arg_type
2139
2196
  )
2140
2197
  end
2141
2198
  else
@@ -2152,16 +2209,15 @@ module Steep
2152
2209
  end
2153
2210
 
2154
2211
  if block_params && method_type.block
2155
- block_annotations = source.annotations(block: node, builder: checker.builder, current_module: current_namespace)
2212
+ block_annotations = source.annotations(block: node, factory: checker.factory, current_module: current_namespace)
2156
2213
  block_params_ = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations)
2157
- block_param_hint = block_params_.params_type(hint: method_type.block.type.params)
2158
-
2159
- relation = Subtyping::Relation.new(
2160
- sub_type: AST::Types::Proc.new(params: block_param_hint, return_type: AST::Types::Any.new),
2161
- super_type: method_type.block.type
2214
+ block_param_hint = block_params_.params_type(
2215
+ hint: topdown_hint ? method_type.block.type.params : nil
2162
2216
  )
2163
2217
 
2164
- checker.check(relation, constraints: constraints).else do |result|
2218
+ check_relation(sub_type: AST::Types::Proc.new(params: block_param_hint, return_type: AST::Types::Any.new),
2219
+ super_type: method_type.block.type,
2220
+ constraints: constraints).else do |result|
2165
2221
  return Errors::IncompatibleBlockParameters.new(
2166
2222
  node: node,
2167
2223
  method_type: method_type
@@ -2175,22 +2231,22 @@ module Steep
2175
2231
  Steep.logger.debug "Constraints = #{constraints}"
2176
2232
 
2177
2233
  begin
2178
- method_type.subst(constraints.solution(checker, variance: variance, variables: occurence.params)).yield_self do |method_type|
2234
+ method_type.subst(constraints.solution(checker, self_type: self_type, variance: variance, variables: occurence.params)).yield_self do |method_type|
2179
2235
  block_type = construction.type_block(block_param_hint: method_type.block.type.params,
2180
2236
  block_type_hint: method_type.block.type.return_type,
2181
2237
  node_type_hint: method_type.return_type,
2182
2238
  block_params: block_params_,
2183
2239
  block_body: block_body,
2184
- block_annotations: block_annotations)
2185
-
2186
- relation = Subtyping::Relation.new(sub_type: block_type.return_type,
2187
- super_type: method_type.block.type.return_type)
2240
+ block_annotations: block_annotations,
2241
+ topdown_hint: topdown_hint)
2188
2242
 
2189
- result = checker.check(relation, constraints: constraints)
2243
+ result = check_relation(sub_type: block_type.return_type,
2244
+ super_type: method_type.block.type.return_type,
2245
+ constraints: constraints)
2190
2246
 
2191
2247
  case result
2192
2248
  when Subtyping::Result::Success
2193
- method_type.return_type.subst(constraints.solution(checker, variance: variance, variables: fresh_vars)).yield_self do |ret_type|
2249
+ method_type.return_type.subst(constraints.solution(checker, self_type: self_type, variance: variance, variables: fresh_vars)).yield_self do |ret_type|
2194
2250
  if block_annotations.break_type
2195
2251
  AST::Types::Union.new(types: [block_annotations.break_type, ret_type])
2196
2252
  else
@@ -2217,21 +2273,24 @@ module Steep
2217
2273
 
2218
2274
  when method_type.block && args.block_pass_arg
2219
2275
  begin
2220
- method_type.subst(constraints.solution(checker, variance: variance, variables: occurence.params)).yield_self do |method_type|
2221
- block_type = synthesize(args.block_pass_arg, hint: method_type.block.type)
2222
- relation = Subtyping::Relation.new(sub_type: block_type,
2223
- super_type: method_type.block.yield_self do |expected_block|
2224
- if expected_block.optional?
2225
- AST::Builtin.optional(expected_block.type)
2226
- else
2227
- expected_block.type
2228
- end
2229
- end)
2230
- result = checker.check(relation, constraints: constraints)
2276
+ method_type.subst(constraints.solution(checker, self_type: self_type, variance: variance, variables: occurence.params)).yield_self do |method_type|
2277
+ block_type = synthesize(args.block_pass_arg,
2278
+ hint: topdown_hint ? method_type.block.type : nil)
2279
+ result = check_relation(
2280
+ sub_type: block_type,
2281
+ super_type: method_type.block.yield_self {|expected_block|
2282
+ if expected_block.optional?
2283
+ AST::Builtin.optional(expected_block.type)
2284
+ else
2285
+ expected_block.type
2286
+ end
2287
+ },
2288
+ constraints: constraints
2289
+ )
2231
2290
 
2232
2291
  case result
2233
2292
  when Subtyping::Result::Success
2234
- method_type.return_type.subst(constraints.solution(checker, variance: variance, variables: fresh_vars))
2293
+ method_type.return_type.subst(constraints.solution(checker, self_type: self_type, variance: variance, variables: fresh_vars))
2235
2294
 
2236
2295
  when Subtyping::Result::Failure
2237
2296
  Errors::BlockTypeMismatch.new(node: node,
@@ -2252,7 +2311,7 @@ module Steep
2252
2311
 
2253
2312
  when (!method_type.block || method_type.block.optional?) && !block_params && !block_body && !args.block_pass_arg
2254
2313
  # OK, without block
2255
- method_type.subst(constraints.solution(checker, variance: variance, variables: fresh_vars)).return_type
2314
+ method_type.subst(constraints.solution(checker, variance: variance, variables: fresh_vars, self_type: self_type)).return_type
2256
2315
 
2257
2316
  when !method_type.block && (block_params || args.block_pass_arg)
2258
2317
  Errors::UnexpectedBlockGiven.new(
@@ -2272,7 +2331,7 @@ module Steep
2272
2331
  end
2273
2332
  end
2274
2333
 
2275
- def type_block(block_param_hint:, block_type_hint:, node_type_hint:, block_params:, block_body:, block_annotations:)
2334
+ def type_block(block_param_hint:, block_type_hint:, node_type_hint:, block_params:, block_body:, block_annotations:, topdown_hint:)
2276
2335
  block_param_pairs = block_param_hint && block_params.zip(block_param_hint)
2277
2336
 
2278
2337
  param_types_hash = {}
@@ -2299,10 +2358,10 @@ module Steep
2299
2358
  end
2300
2359
 
2301
2360
  break_type = if block_annotations.break_type
2302
- union_type(node_type_hint, block_annotations.break_type)
2303
- else
2304
- node_type_hint
2305
- end
2361
+ union_type(node_type_hint, block_annotations.break_type)
2362
+ else
2363
+ node_type_hint
2364
+ end
2306
2365
  Steep.logger.debug("return_type = #{break_type}")
2307
2366
 
2308
2367
  block_context = BlockContext.new(body_type: block_annotations.block_type)
@@ -2338,7 +2397,7 @@ module Steep
2338
2397
  end
2339
2398
  body_type
2340
2399
  else
2341
- for_block_body.synthesize(block_body, hint: block_type_hint)
2400
+ for_block_body.synthesize(block_body, hint: topdown_hint ? block_type_hint : nil)
2342
2401
  end
2343
2402
  else
2344
2403
  return_type = AST::Builtin.any_type
@@ -2457,22 +2516,14 @@ module Steep
2457
2516
  end
2458
2517
 
2459
2518
  def validate_method_definitions(node, module_name)
2460
- signature = checker.builder.signatures.find_class_or_module(module_name.name)
2461
- expected_instance_method_names = Set.new()
2462
- expected_module_method_names = Set.new
2463
-
2464
- signature.members.each do |member|
2465
- case member
2466
- when AST::Signature::Members::Method
2467
- if member.instance_method?
2468
- expected_instance_method_names << member.name
2469
- end
2470
- if member.module_method?
2471
- expected_module_method_names << member.name
2472
- end
2473
- when AST::Signature::Members::Attr
2474
- expected_instance_method_names << member.name
2475
- expected_instance_method_names << "#{member.name}=".to_sym if member.accessor?
2519
+ expected_instance_method_names = (module_context.instance_definition&.methods || {}).each.with_object(Set[]) do |(name, method), set|
2520
+ if method.implemented_in == module_context.instance_definition.declaration
2521
+ set << name
2522
+ end
2523
+ end
2524
+ expected_module_method_names = (module_context.module_definition&.methods || {}).each.with_object(Set[]) do |(name, method), set|
2525
+ if method.implemented_in == module_context.module_definition.declaration
2526
+ set << name
2476
2527
  end
2477
2528
  end
2478
2529
 
@@ -2570,7 +2621,7 @@ module Steep
2570
2621
  return false
2571
2622
  end
2572
2623
 
2573
- !nodes.empty? && nodes.all? {|child| child.type == :class || child.type == :module }
2624
+ !nodes.empty? && nodes.all? {|child| child.type == :class || child.type == :module}
2574
2625
  end
2575
2626
 
2576
2627
  def fallback_any_rec(node)
@@ -2587,7 +2638,7 @@ module Steep
2587
2638
  expand_alias(type) do |expanded|
2588
2639
  case
2589
2640
  when expanded.is_a?(AST::Types::Union)
2590
- types = expanded.types.reject {|type| type.is_a?(AST::Types::Nil) }
2641
+ types = expanded.types.reject {|type| type.is_a?(AST::Types::Nil)}
2591
2642
  AST::Types::Union.build(types: types)
2592
2643
  else
2593
2644
  type
@@ -2623,18 +2674,27 @@ module Steep
2623
2674
  end
2624
2675
  end
2625
2676
 
2626
- def expand_alias(type)
2627
- if type
2628
- expanded = checker.expand_alias(type)
2629
- end
2677
+ def deep_expand_alias(type, recursive: Set.new, &block)
2678
+ raise "Recursive type definition: #{type}" if recursive.member?(type)
2679
+
2680
+ ty = case type
2681
+ when AST::Types::Name::Alias
2682
+ deep_expand_alias(expand_alias(type), recursive: recursive << type)
2683
+ else
2684
+ type
2685
+ end
2630
2686
 
2631
2687
  if block_given?
2632
- yield expanded, type
2688
+ yield ty
2633
2689
  else
2634
- expanded
2690
+ ty
2635
2691
  end
2636
2692
  end
2637
2693
 
2694
+ def expand_alias(type, &block)
2695
+ checker.factory.expand_alias(type, &block)
2696
+ end
2697
+
2638
2698
  def test_literal_type(literal, hint)
2639
2699
  case hint
2640
2700
  when AST::Types::Literal
@@ -2642,7 +2702,7 @@ module Steep
2642
2702
  hint
2643
2703
  end
2644
2704
  when AST::Types::Union
2645
- if hint.types.any? {|ty| ty.is_a?(AST::Types::Literal) && ty.value == literal }
2705
+ if hint.types.any? {|ty| ty.is_a?(AST::Types::Literal) && ty.value == literal}
2646
2706
  hint
2647
2707
  end
2648
2708
  end
@@ -2650,10 +2710,7 @@ module Steep
2650
2710
 
2651
2711
  def select_super_type(sub_type, super_type)
2652
2712
  if super_type
2653
- result = checker.check(
2654
- Subtyping::Relation.new(sub_type: sub_type, super_type: super_type),
2655
- constraints: Subtyping::Constraints.empty
2656
- )
2713
+ result = check_relation(sub_type: sub_type, super_type: super_type)
2657
2714
 
2658
2715
  if result.success?
2659
2716
  super_type
@@ -2671,10 +2728,10 @@ module Steep
2671
2728
 
2672
2729
  def to_instance_type(type, args: nil)
2673
2730
  args = args || case type
2674
- when AST::Types::Name::Class
2675
- checker.builder.signatures.find_class(type.name).params&.variables || []
2676
- when AST::Types::Name::Module
2677
- checker.builder.signatures.find_module(type.name).params&.variables || []
2731
+ when AST::Types::Name::Class, AST::Types::Name::Module
2732
+ checker.factory.env.find_class(checker.factory.type_name_1(type.name)).type_params.each.map { AST::Builtin.any_type }
2733
+ else
2734
+ raise "unexpected type to to_instance_type: #{type}"
2678
2735
  end
2679
2736
 
2680
2737
  AST::Types::Name::Instance.new(name: type.name, args: args)
@@ -2699,14 +2756,11 @@ module Steep
2699
2756
  return nil
2700
2757
  end
2701
2758
 
2702
- key_hint = AST::Types::Literal.new(value: key_value)
2703
2759
  value_hint = hint.elements[key_value]
2704
-
2705
2760
  value_type = new_construction.synthesize(value, hint: value_hint)
2706
2761
 
2707
2762
  if value_hint
2708
- relation = Subtyping::Relation.new(sub_type: value_type, super_type: value_hint)
2709
- if checker.check(relation, constraints: Subtyping::Constraints.empty).success?
2763
+ if check_relation(sub_type: value_type, super_type: value_hint).success?
2710
2764
  value_type = value_hint
2711
2765
  end
2712
2766
  end