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
data/lib/steep/source.rb CHANGED
@@ -44,7 +44,7 @@ module Steep
44
44
  end
45
45
  end
46
46
 
47
- def self.parse(source_code, path:, labeling: ASTUtils::Labeling.new)
47
+ def self.parse(source_code, path:, factory:, labeling: ASTUtils::Labeling.new)
48
48
  buffer = ::Parser::Source::Buffer.new(path.to_s, 1)
49
49
  buffer.source = source_code
50
50
  node = parser.parse(buffer).yield_self do |n|
@@ -68,10 +68,11 @@ module Steep
68
68
  buffer = AST::Buffer.new(name: path, content: source_code)
69
69
 
70
70
  comments.each do |comment|
71
- src = comment.text.gsub(/\A#/, '')
72
- annotation = Steep::Parser.parse_annotation_opt(src,
73
- buffer: buffer,
74
- offset: comment.location.expression.begin_pos+1)
71
+ src = comment.text.gsub(/\A#\s*/, '')
72
+ location = AST::Location.new(buffer: buffer,
73
+ start_pos: comment.location.expression.begin_pos + 1,
74
+ end_pos: comment.location.expression.end_pos)
75
+ annotation = AnnotationParser.new(factory: factory).parse(src, location: location)
75
76
  if annotation
76
77
  annotations << LocatedAnnotation.new(line: comment.location.line, source: src, annotation: annotation)
77
78
  end
@@ -274,8 +275,12 @@ module Steep
274
275
  end
275
276
  end
276
277
 
277
- def annotations(block:, builder:, current_module:)
278
- AST::Annotation::Collection.new(annotations: mapping[block.__id__] || [], builder: builder, current_module: current_module)
278
+ def annotations(block:, factory:, current_module:)
279
+ AST::Annotation::Collection.new(
280
+ annotations: mapping[block.__id__] || [],
281
+ factory: factory,
282
+ current_module: current_module
283
+ )
279
284
  end
280
285
 
281
286
  def each_annotation
@@ -1,18 +1,18 @@
1
1
  module Steep
2
2
  module Subtyping
3
3
  class Check
4
- attr_reader :builder
4
+ attr_reader :factory
5
5
  attr_reader :cache
6
6
 
7
- def initialize(builder:)
8
- @builder = builder
7
+ def initialize(factory:)
8
+ @factory = factory
9
9
  @cache = {}
10
10
  end
11
11
 
12
- def check(relation, constraints:, assumption: Set.new, trace: Trace.new)
12
+ def check(relation, constraints:, self_type:, assumption: Set.new, trace: Trace.new)
13
13
  Steep.logger.tagged "#{relation.sub_type} <: #{relation.super_type}" do
14
14
  prefix = trace.size
15
- cached = cache[relation]
15
+ cached = cache[[relation, self_type]]
16
16
  if cached && constraints.empty?
17
17
  if cached.success?
18
18
  cached
@@ -23,16 +23,14 @@ module Steep
23
23
  if assumption.member?(relation)
24
24
  success(constraints: constraints)
25
25
  else
26
- trace.add(relation.sub_type, relation.super_type) do
27
- assumption = assumption + Set.new([relation])
28
- check0(relation, assumption: assumption, trace: trace, constraints: constraints).tap do |result|
29
- result = result.else do |failure|
30
- failure.drop(prefix)
31
- end
32
-
33
- Steep.logger.debug "result=#{result.class}"
34
- cache[relation] = result if cacheable?(relation)
26
+ assumption = assumption + Set[relation]
27
+ check0(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints).tap do |result|
28
+ result = result.else do |failure|
29
+ failure.drop(prefix)
35
30
  end
31
+
32
+ Steep.logger.debug "result=#{result.class}"
33
+ cache[[relation, self_type]] = result if cacheable?(relation)
36
34
  end
37
35
  end
38
36
  end
@@ -55,118 +53,105 @@ module Steep
55
53
  Result::Failure.new(error: error, trace: trace)
56
54
  end
57
55
 
58
- def check0(relation, assumption:, trace:, constraints:)
59
- case
60
- when same_type?(relation, assumption: assumption)
61
- success(constraints: constraints)
62
-
63
- when relation.sub_type.is_a?(AST::Types::Any) || relation.super_type.is_a?(AST::Types::Any)
64
- success(constraints: constraints)
65
-
66
- when relation.super_type.is_a?(AST::Types::Void)
67
- success(constraints: constraints)
68
-
69
- when relation.super_type.is_a?(AST::Types::Top)
70
- success(constraints: constraints)
71
-
72
- when relation.sub_type.is_a?(AST::Types::Bot)
73
- success(constraints: constraints)
56
+ def check0(relation, self_type:, assumption:, trace:, constraints:)
57
+ # puts relation
58
+ trace.type(relation.sub_type, relation.super_type) do
59
+ case
60
+ when same_type?(relation, assumption: assumption)
61
+ success(constraints: constraints)
74
62
 
75
- when relation.super_type.is_a?(AST::Types::Boolean)
76
- success(constraints: constraints)
63
+ when relation.sub_type.is_a?(AST::Types::Any) || relation.super_type.is_a?(AST::Types::Any)
64
+ success(constraints: constraints)
77
65
 
78
- when alias?(relation.sub_type)
79
- check0(
80
- Relation.new(sub_type: expand_alias(relation.sub_type), super_type: relation.super_type),
81
- assumption: assumption,
82
- trace: trace,
83
- constraints: constraints
84
- )
66
+ when relation.super_type.is_a?(AST::Types::Void)
67
+ success(constraints: constraints)
85
68
 
86
- when alias?(relation.super_type)
87
- check0(
88
- Relation.new(super_type: expand_alias(relation.super_type), sub_type: relation.sub_type),
89
- assumption: assumption,
90
- trace: trace,
91
- constraints: constraints
92
- )
69
+ when relation.super_type.is_a?(AST::Types::Top)
70
+ success(constraints: constraints)
93
71
 
94
- when relation.sub_type.is_a?(AST::Types::Literal)
95
- check0(
96
- Relation.new(sub_type: relation.sub_type.back_type, super_type: relation.super_type),
97
- assumption: assumption,
98
- trace: trace,
99
- constraints: constraints
100
- )
72
+ when relation.sub_type.is_a?(AST::Types::Bot)
73
+ success(constraints: constraints)
101
74
 
102
- when relation.sub_type.is_a?(AST::Types::Union)
103
- results = relation.sub_type.types.map do |sub_type|
104
- check0(Relation.new(sub_type: sub_type, super_type: relation.super_type),
105
- assumption: assumption,
106
- trace: trace,
107
- constraints: constraints)
108
- end
75
+ when relation.super_type.is_a?(AST::Types::Boolean)
76
+ success(constraints: constraints)
109
77
 
110
- if results.all?(&:success?)
111
- results.first
112
- else
113
- results.find(&:failure?)
114
- end
78
+ when relation.sub_type.is_a?(AST::Types::Self) && !self_type.is_a?(AST::Types::Self)
79
+ check(
80
+ Relation.new(sub_type: self_type, super_type: relation.super_type),
81
+ self_type: self_type,
82
+ assumption: assumption,
83
+ trace: trace,
84
+ constraints: constraints
85
+ )
115
86
 
116
- when relation.super_type.is_a?(AST::Types::Union)
117
- results = relation.super_type.types.map do |super_type|
118
- check0(Relation.new(sub_type: relation.sub_type, super_type: super_type),
119
- assumption: assumption,
120
- trace: trace,
121
- constraints: constraints)
122
- end
87
+ when alias?(relation.sub_type)
88
+ check(
89
+ Relation.new(sub_type: expand_alias(relation.sub_type), super_type: relation.super_type),
90
+ self_type: self_type,
91
+ assumption: assumption,
92
+ trace: trace,
93
+ constraints: constraints
94
+ )
123
95
 
124
- results.find(&:success?) || results.first
96
+ when alias?(relation.super_type)
97
+ check(
98
+ Relation.new(super_type: expand_alias(relation.super_type), sub_type: relation.sub_type),
99
+ self_type: self_type,
100
+ assumption: assumption,
101
+ trace: trace,
102
+ constraints: constraints
103
+ )
125
104
 
126
- when relation.sub_type.is_a?(AST::Types::Intersection)
127
- results = relation.sub_type.types.map do |sub_type|
128
- check0(Relation.new(sub_type: sub_type, super_type: relation.super_type),
129
- assumption: assumption,
130
- trace: trace,
131
- constraints: constraints)
132
- end
105
+ when relation.sub_type.is_a?(AST::Types::Literal)
106
+ check(
107
+ Relation.new(sub_type: relation.sub_type.back_type, super_type: relation.super_type),
108
+ self_type: self_type,
109
+ assumption: assumption,
110
+ trace: trace,
111
+ constraints: constraints
112
+ )
133
113
 
134
- results.find(&:success?) || results.first
114
+ when relation.sub_type.is_a?(AST::Types::Union)
115
+ results = relation.sub_type.types.map do |sub_type|
116
+ check(Relation.new(sub_type: sub_type, super_type: relation.super_type),
117
+ self_type: self_type,
118
+ assumption: assumption,
119
+ trace: trace,
120
+ constraints: constraints)
121
+ end
135
122
 
136
- when relation.super_type.is_a?(AST::Types::Intersection)
137
- results = relation.super_type.types.map do |super_type|
138
- check0(Relation.new(sub_type: relation.sub_type, super_type: super_type),
139
- assumption: assumption,
140
- trace: trace,
141
- constraints: constraints)
142
- end
123
+ if results.all?(&:success?)
124
+ results.first
125
+ else
126
+ results.find(&:failure?)
127
+ end
143
128
 
144
- if results.all?(&:success?)
145
- results.first
146
- else
147
- results.find(&:failure?)
148
- end
129
+ when relation.super_type.is_a?(AST::Types::Union)
130
+ results = relation.super_type.types.map do |super_type|
131
+ check(Relation.new(sub_type: relation.sub_type, super_type: super_type),
132
+ self_type: self_type,
133
+ assumption: assumption,
134
+ trace: trace,
135
+ constraints: constraints)
136
+ end
149
137
 
150
- when relation.super_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.super_type.name)
151
- constraints.add(relation.super_type.name, sub_type: relation.sub_type)
152
- success(constraints: constraints)
138
+ results.find(&:success?) || results.first
153
139
 
154
- when relation.sub_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.sub_type.name)
155
- constraints.add(relation.sub_type.name, super_type: relation.super_type)
156
- success(constraints: constraints)
140
+ when relation.sub_type.is_a?(AST::Types::Intersection)
141
+ results = relation.sub_type.types.map do |sub_type|
142
+ check(Relation.new(sub_type: sub_type, super_type: relation.super_type),
143
+ self_type: self_type,
144
+ assumption: assumption,
145
+ trace: trace,
146
+ constraints: constraints)
147
+ end
157
148
 
158
- when relation.super_type.is_a?(AST::Types::Var) || relation.sub_type.is_a?(AST::Types::Var)
159
- failure(error: Result::Failure::UnknownPairError.new(relation: relation),
160
- trace: trace)
149
+ results.find(&:success?) || results.first
161
150
 
162
- when relation.sub_type.is_a?(AST::Types::Name::Base) && relation.super_type.is_a?(AST::Types::Name::Base)
163
- if (pairs = extract_nominal_pairs(relation))
164
- results = pairs.flat_map do |(sub, sup)|
165
- Relation.new(sub_type: sub, super_type: sup).yield_self do |rel|
166
- [rel, rel.flip]
167
- end
168
- end.map do |relation|
169
- check0(relation,
151
+ when relation.super_type.is_a?(AST::Types::Intersection)
152
+ results = relation.super_type.types.map do |super_type|
153
+ check(Relation.new(sub_type: relation.sub_type, super_type: super_type),
154
+ self_type: self_type,
170
155
  assumption: assumption,
171
156
  trace: trace,
172
157
  constraints: constraints)
@@ -177,83 +162,131 @@ module Steep
177
162
  else
178
163
  results.find(&:failure?)
179
164
  end
180
- else
181
- sub_interface = resolve(relation.sub_type)
182
- super_interface = resolve(relation.super_type)
183
165
 
184
- check_interface(sub_interface, super_interface, assumption: assumption, trace: trace, constraints: constraints)
185
- end
166
+ when relation.super_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.super_type.name)
167
+ constraints.add(relation.super_type.name, sub_type: relation.sub_type)
168
+ success(constraints: constraints)
186
169
 
187
- when relation.sub_type.is_a?(AST::Types::Proc) && relation.super_type.is_a?(AST::Types::Proc)
188
- check_method_params(:__proc__,
189
- relation.sub_type.params, relation.super_type.params,
190
- assumption: assumption,
191
- trace: trace,
192
- constraints: constraints).then do
193
- check0(Relation.new(sub_type: relation.sub_type.return_type, super_type: relation.super_type.return_type),
194
- assumption: assumption,
195
- trace: trace,
196
- constraints: constraints)
197
- end
170
+ when relation.sub_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.sub_type.name)
171
+ constraints.add(relation.sub_type.name, super_type: relation.super_type)
172
+ success(constraints: constraints)
198
173
 
199
- when relation.sub_type.is_a?(AST::Types::Tuple) && relation.super_type.is_a?(AST::Types::Tuple)
200
- if relation.sub_type.types.size >= relation.super_type.types.size
201
- pairs = relation.sub_type.types.take(relation.super_type.types.size).zip(relation.super_type.types)
202
- results = pairs.flat_map do |t1, t2|
203
- relation = Relation.new(sub_type: t1, super_type: t2)
204
- [check0(relation, assumption: assumption, trace: trace, constraints: constraints),
205
- check0(relation.flip, assumption: assumption, trace: trace, constraints: constraints)]
206
- end
174
+ when relation.super_type.is_a?(AST::Types::Var) || relation.sub_type.is_a?(AST::Types::Var)
175
+ failure(error: Result::Failure::UnknownPairError.new(relation: relation),
176
+ trace: trace)
207
177
 
208
- if results.all?(&:success?)
209
- success(constraints: constraints)
178
+ when relation.sub_type.is_a?(AST::Types::Name::Base) && relation.super_type.is_a?(AST::Types::Name::Base)
179
+ if (pairs = extract_nominal_pairs(relation))
180
+ results = pairs.flat_map do |(sub, sup)|
181
+ Relation.new(sub_type: sub, super_type: sup).yield_self do |rel|
182
+ [rel, rel.flip]
183
+ end
184
+ end.map do |relation|
185
+ check(relation,
186
+ self_type: self_type,
187
+ assumption: assumption,
188
+ trace: trace,
189
+ constraints: constraints)
190
+ end
191
+
192
+ if results.all?(&:success?)
193
+ results.first
194
+ else
195
+ results.find(&:failure?)
196
+ end
210
197
  else
211
- results.find(&:failure?)
198
+ sub_interface = factory.interface(relation.sub_type, private: false)
199
+ super_interface = factory.interface(relation.super_type, private: false)
200
+
201
+ check_interface(sub_interface, super_interface, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)
212
202
  end
213
- else
214
- failure(error: Result::Failure::UnknownPairError.new(relation: relation),
215
- trace: trace)
216
- end
217
203
 
218
- when relation.sub_type.is_a?(AST::Types::Tuple)
219
- sub_interface = resolve(relation.sub_type)
220
- super_interface = resolve(relation.super_type)
221
-
222
- check_interface(sub_interface,
223
- super_interface,
224
- assumption: assumption,
225
- trace: trace,
226
- constraints: constraints)
227
-
228
- when relation.sub_type.is_a?(AST::Types::Record) && relation.super_type.is_a?(AST::Types::Record)
229
- if Set.new(relation.sub_type.elements.keys).superset?(Set.new(relation.super_type.elements.keys))
230
- keys = relation.super_type.elements.keys
231
- type_pairs = keys.map {|key| [relation.sub_type.elements[key], relation.super_type.elements[key]] }
232
- results = type_pairs.flat_map do |t1, t2|
233
- relation = Relation.new(sub_type: t1, super_type: t2)
234
- [check0(relation, assumption: assumption, trace: trace, constraints: constraints),
235
- check0(relation.flip, assumption: assumption, trace: trace, constraints: constraints)]
204
+ when relation.sub_type.is_a?(AST::Types::Proc) && relation.super_type.is_a?(AST::Types::Proc)
205
+ check_method_params(:__proc__,
206
+ relation.sub_type.params, relation.super_type.params,
207
+ self_type: self_type,
208
+ assumption: assumption,
209
+ trace: trace,
210
+ constraints: constraints).then do
211
+ check(Relation.new(sub_type: relation.sub_type.return_type, super_type: relation.super_type.return_type),
212
+ self_type: self_type,
213
+ assumption: assumption,
214
+ trace: trace,
215
+ constraints: constraints)
236
216
  end
237
217
 
238
- if results.all?(&:success?)
239
- success(constraints: constraints)
218
+ when relation.sub_type.is_a?(AST::Types::Tuple) && relation.super_type.is_a?(AST::Types::Tuple)
219
+ if relation.sub_type.types.size >= relation.super_type.types.size
220
+ pairs = relation.sub_type.types.take(relation.super_type.types.size).zip(relation.super_type.types)
221
+ results = pairs.flat_map do |t1, t2|
222
+ relation = Relation.new(sub_type: t1, super_type: t2)
223
+ [check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints),
224
+ check(relation.flip, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)]
225
+ end
226
+
227
+ if results.all?(&:success?)
228
+ success(constraints: constraints)
229
+ else
230
+ results.find(&:failure?)
231
+ end
240
232
  else
241
- results.find(&:failure?)
233
+ failure(error: Result::Failure::UnknownPairError.new(relation: relation),
234
+ trace: trace)
235
+ end
236
+
237
+ when relation.sub_type.is_a?(AST::Types::Tuple) && relation.super_type.is_a?(AST::Types::Name::Base)
238
+ tuple_interface = factory.interface(relation.sub_type, private: false)
239
+ type_interface = factory.interface(relation.super_type, private: false)
240
+
241
+ check_interface(tuple_interface,
242
+ type_interface,
243
+ self_type: self_type,
244
+ assumption: assumption,
245
+ trace: trace,
246
+ constraints: constraints)
247
+
248
+ when relation.sub_type.is_a?(AST::Types::Record) && relation.super_type.is_a?(AST::Types::Record)
249
+ if Set.new(relation.sub_type.elements.keys).superset?(Set.new(relation.super_type.elements.keys))
250
+ keys = relation.super_type.elements.keys
251
+ type_pairs = keys.map {|key| [relation.sub_type.elements[key], relation.super_type.elements[key]] }
252
+ results = type_pairs.flat_map do |t1, t2|
253
+ relation = Relation.new(sub_type: t1, super_type: t2)
254
+ [check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints),
255
+ check(relation.flip, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)]
256
+ end
257
+
258
+ if results.all?(&:success?)
259
+ success(constraints: constraints)
260
+ else
261
+ results.find(&:failure?)
262
+ end
263
+ else
264
+ failure(error: Result::Failure::UnknownPairError.new(relation: relation),
265
+ trace: trace)
242
266
  end
267
+
268
+ when relation.sub_type.is_a?(AST::Types::Record) && relation.super_type.is_a?(AST::Types::Name::Base)
269
+ record_interface = factory.interface(relation.sub_type, private: false)
270
+ type_interface = factory.interface(relation.super_type, private: false)
271
+
272
+ check_interface(record_interface,
273
+ type_interface,
274
+ self_type: self_type,
275
+ assumption: assumption,
276
+ trace: trace,
277
+ constraints: constraints)
278
+
243
279
  else
244
280
  failure(error: Result::Failure::UnknownPairError.new(relation: relation),
245
281
  trace: trace)
246
282
  end
247
- else
248
- failure(error: Result::Failure::UnknownPairError.new(relation: relation),
249
- trace: trace)
250
283
  end
251
284
  end
252
-
285
+
253
286
  def extract_nominal_pairs(relation)
254
287
  sub_type = relation.sub_type
255
288
  super_type = relation.super_type
256
-
289
+
257
290
  case
258
291
  when sub_type.is_a?(AST::Types::Name::Instance) && super_type.is_a?(AST::Types::Name::Instance)
259
292
  if sub_type.name == super_type.name && sub_type.args.size == super_type.args.size
@@ -299,119 +332,188 @@ module Steep
299
332
  end
300
333
  end
301
334
 
302
- def check_interface(sub_type, super_type, assumption:, trace:, constraints:)
303
- method_pairs = []
335
+ def check_interface(sub_interface, super_interface, self_type:, assumption:, trace:, constraints:)
336
+ trace.interface sub_interface, super_interface do
337
+ method_triples = []
304
338
 
305
- super_type.methods.each do |name, sup_method|
306
- sub_method = sub_type.methods[name]
339
+ super_interface.methods.each do |name, sup_method|
340
+ sub_method = sub_interface.methods[name]
307
341
 
308
- if sub_method
309
- method_pairs << [sub_method, sup_method]
310
- else
311
- return failure(error: Result::Failure::MethodMissingError.new(name: name),
312
- trace: trace)
342
+ if sub_method
343
+ method_triples << [name, sub_method, sup_method]
344
+ else
345
+ return failure(error: Result::Failure::MethodMissingError.new(name: name),
346
+ trace: trace)
347
+ end
313
348
  end
314
- end
315
349
 
316
- method_pairs.each do |(sub_method, sup_method)|
317
- result = check_method(sub_method.name, sub_method, sup_method, assumption: assumption, trace: trace, constraints: constraints)
318
- return result if result.failure?
319
- end
350
+ method_triples.each do |(method_name, sub_method, sup_method)|
351
+ result = check_method(method_name,
352
+ sub_method,
353
+ sup_method,
354
+ self_type: self_type,
355
+ assumption: assumption,
356
+ trace: trace,
357
+ constraints: constraints)
358
+ return result if result.failure?
359
+ end
320
360
 
321
- success(constraints: constraints)
361
+ success(constraints: constraints)
362
+ end
322
363
  end
323
364
 
324
- def check_method(name, sub_method, super_method, assumption:, trace:, constraints:)
325
- trace.add(sub_method, super_method) do
326
- all_results = super_method.types.map do |super_type|
327
- sub_method.types.map do |sub_type|
328
- trace.add(sub_type, super_type) do
329
- case
330
- when super_type.type_params.empty? && sub_type.type_params.empty?
331
- check_method_type(name,
332
- sub_type,
333
- super_type,
334
- assumption: assumption,
335
- trace: trace,
336
- constraints: constraints)
337
-
338
- when super_type.type_params.empty?
339
- yield_self do
340
- sub_args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
341
- sub_type = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params,
342
- sub_args))
343
-
344
- constraints.add_var(*sub_args)
345
-
346
- match_method_type(name, sub_type, super_type, trace: trace).yield_self do |pairs|
347
- case pairs
348
- when Array
349
- subst = pairs.each.with_object(Interface::Substitution.empty) do |(sub, sup), subst|
350
- case
351
- when sub.is_a?(AST::Types::Var) && sub_args.include?(sub)
352
- subst.add!(sub.name, sup)
353
- when sup.is_a?(AST::Types::Var) && sub_args.include?(sup)
354
- subst.add!(sup.name, sub)
355
- end
356
- end
357
-
358
- check_method_type(name,
359
- sub_type.subst(subst),
365
+ def check_method(name, sub_method, super_method, self_type:, assumption:, trace:, constraints:)
366
+ trace.method name, sub_method, super_method do
367
+ case
368
+ when sub_method.overload? && super_method.overload?
369
+ super_method.types.map do |super_type|
370
+ sub_method.types.map do |sub_type|
371
+ check_generic_method_type name,
372
+ sub_type,
360
373
  super_type,
374
+ self_type: self_type,
361
375
  assumption: assumption,
362
376
  trace: trace,
363
- constraints: constraints)
377
+ constraints: constraints
378
+ end.yield_self do |results|
379
+ results.find(&:success?) || results[0]
380
+ end
381
+ end.yield_self do |results|
382
+ if results.all?(&:success?) || sub_method.incompatible?
383
+ success constraints: constraints
384
+ else
385
+ results.select(&:failure?).last
386
+ end
387
+ end
388
+ else
389
+ raise "aaaaaaaaaaaaaa"
390
+ end
391
+ end
392
+ end
393
+
394
+ def check_generic_method_type(name, sub_type, super_type, self_type:, assumption:, trace:, constraints:)
395
+ trace.method_type name, sub_type, super_type do
396
+ case
397
+ when sub_type.type_params.empty? && super_type.type_params.empty?
398
+ check_method_type name,
399
+ sub_type,
400
+ super_type,
401
+ self_type: self_type,
402
+ assumption: assumption,
403
+ trace: trace,
404
+ constraints: constraints
405
+
406
+ when !sub_type.type_params.empty? && super_type.type_params.empty?
407
+ # Check if super_type is an instance of sub_type.
408
+ yield_self do
409
+ sub_args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
410
+ sub_type = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params, sub_args))
411
+
412
+ constraints.add_var(*sub_args)
413
+
414
+ match_method_type(name, sub_type, super_type, trace: trace).yield_self do |pairs|
415
+ case pairs
416
+ when Array
417
+ subst = pairs.each.with_object(Interface::Substitution.empty) do |(sub, sup), subst|
418
+ case
419
+ when sub.is_a?(AST::Types::Var) && sub_args.include?(sub)
420
+ if subst.key?(sub.name) && subst[sub.name] != sup
421
+ return failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
422
+ trace: trace)
364
423
  else
365
- pairs
424
+ subst.add!(sub.name, sup)
425
+ end
426
+ when sup.is_a?(AST::Types::Var) && sub_args.include?(sup)
427
+ if subst.key?(sup.name) && subst[sup.name] != sub
428
+ return failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
429
+ trace: trace)
430
+ else
431
+ subst.add!(sup.name, sub)
366
432
  end
367
433
  end
368
434
  end
369
435
 
370
- when super_type.type_params.size == sub_type.type_params.size
371
- yield_self do
372
- args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
436
+ check_method_type(name,
437
+ sub_type.subst(subst),
438
+ super_type,
439
+ self_type: self_type,
440
+ assumption: assumption,
441
+ trace: trace,
442
+ constraints: constraints)
443
+ else
444
+ pairs
445
+ end
446
+ end
447
+ end
448
+
449
+ when sub_type.type_params.empty? && !super_type.type_params.empty?
450
+ # Check if sub_type is an instance of super_type && no constraints on type variables (any).
451
+ yield_self do
452
+ sub_args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
453
+ sub_type = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params, sub_args))
373
454
 
374
- sub_type_ = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params, args))
375
- super_type_ = super_type.instantiate(Interface::Substitution.build(super_type.type_params, args))
455
+ constraints.add_var(*sub_args)
376
456
 
377
- constraints.add_var(*args)
457
+ match_method_type(name, sub_type, super_type, trace: trace).yield_self do |pairs|
458
+ case pairs
459
+ when Array
460
+ result = check_method_type(name,
461
+ sub_type,
462
+ super_type,
463
+ self_type: self_type,
464
+ assumption: assumption,
465
+ trace: trace,
466
+ constraints: constraints)
378
467
 
379
- check_method_type(name,
380
- sub_type_,
381
- super_type_,
382
- assumption: assumption,
383
- trace: trace,
384
- constraints: constraints)
468
+ if result.success? && sub_args.map(&:name).none? {|var| constraints.has_constraint?(var) }
469
+ result
470
+ else
471
+ failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
472
+ trace: trace)
385
473
  end
474
+
386
475
  else
387
- failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
388
- trace: trace)
476
+ pairs
389
477
  end
390
478
  end
391
479
  end
392
- end
393
480
 
394
- all_results.each do |results|
395
- if results.any?(&:success?)
396
- #ok
397
- else
398
- return results.find(&:failure?)
481
+ when super_type.type_params.size == sub_type.type_params.size
482
+ # Check if they have the same shape
483
+ yield_self do
484
+ args = sub_type.type_params.map {|x| AST::Types::Var.fresh(x) }
485
+
486
+ sub_type_ = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params, args))
487
+ super_type_ = super_type.instantiate(Interface::Substitution.build(super_type.type_params, args))
488
+
489
+ constraints.add_var(*args)
490
+
491
+ check_method_type(name,
492
+ sub_type_,
493
+ super_type_,
494
+ self_type: self_type,
495
+ assumption: assumption,
496
+ trace: trace,
497
+ constraints: constraints)
399
498
  end
400
- end
401
499
 
402
- success(constraints: constraints)
500
+ else
501
+ # Or error
502
+ failure(error: Result::Failure::PolyMethodSubtyping.new(name: name),
503
+ trace: trace)
504
+ end
403
505
  end
404
506
  end
405
507
 
406
- def check_method_type(name, sub_type, super_type, assumption:, trace:, constraints:)
508
+ def check_method_type(name, sub_type, super_type, self_type:, assumption:, trace:, constraints:)
407
509
  Steep.logger.tagged("#{name}: #{sub_type} <: #{super_type}") do
408
- check_method_params(name, sub_type.params, super_type.params, assumption: assumption, trace: trace, constraints: constraints).then do
510
+ check_method_params(name, sub_type.params, super_type.params, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints).then do
409
511
  check_block_given(name, sub_type.block, super_type.block, trace: trace, constraints: constraints).then do
410
- check_block_params(name, sub_type.block, super_type.block, assumption: assumption, trace: trace, constraints: constraints).then do
411
- check_block_return(sub_type.block, super_type.block, assumption: assumption, trace: trace, constraints:constraints).then do
512
+ check_block_params(name, sub_type.block, super_type.block, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints).then do
513
+ check_block_return(sub_type.block, super_type.block, self_type: self_type, assumption: assumption, trace: trace, constraints:constraints).then do
412
514
  relation = Relation.new(super_type: super_type.return_type,
413
515
  sub_type: sub_type.return_type)
414
- check(relation, assumption: assumption, trace: trace, constraints: constraints)
516
+ check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)
415
517
  end
416
518
  end
417
519
  end
@@ -435,14 +537,14 @@ module Steep
435
537
  end
436
538
  end
437
539
 
438
- def check_method_params(name, sub_params, super_params, assumption:, trace:, constraints:)
540
+ def check_method_params(name, sub_params, super_params, self_type:, assumption:, trace:, constraints:)
439
541
  match_params(name, sub_params, super_params, trace: trace).yield_self do |pairs|
440
542
  case pairs
441
543
  when Array
442
544
  pairs.each do |(sub_type, super_type)|
443
545
  relation = Relation.new(super_type: sub_type, sub_type: super_type)
444
546
 
445
- result = check(relation, assumption: assumption, trace: trace, constraints: constraints)
547
+ result = check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)
446
548
  return result if result.failure?
447
549
  end
448
550
 
@@ -571,11 +673,12 @@ module Steep
571
673
  pairs
572
674
  end
573
675
 
574
- def check_block_params(name, sub_block, super_block, assumption:, trace:, constraints:)
676
+ def check_block_params(name, sub_block, super_block, self_type:, assumption:, trace:, constraints:)
575
677
  if sub_block && super_block
576
678
  check_method_params(name,
577
679
  super_block.type.params,
578
680
  sub_block.type.params,
681
+ self_type: self_type,
579
682
  assumption: assumption,
580
683
  trace: trace,
581
684
  constraints: constraints)
@@ -584,397 +687,18 @@ module Steep
584
687
  end
585
688
  end
586
689
 
587
- def check_block_return(sub_block, super_block, assumption:, trace:, constraints:)
690
+ def check_block_return(sub_block, super_block, self_type:, assumption:, trace:, constraints:)
588
691
  if sub_block && super_block
589
692
  relation = Relation.new(sub_type: super_block.type.return_type,
590
693
  super_type: sub_block.type.return_type)
591
- check(relation, assumption: assumption, trace: trace, constraints: constraints)
694
+ check(relation, self_type: self_type, assumption: assumption, trace: trace, constraints: constraints)
592
695
  else
593
696
  success(constraints: constraints)
594
697
  end
595
698
  end
596
699
 
597
- class CannotResolveError < StandardError
598
- attr_reader :type
599
-
600
- def initialize(type:)
601
- @type = type
602
- super "Type #{type} cannot resolve to interface"
603
- end
604
- end
605
-
606
- def resolve_instance(type, self_type:, instance_type:, module_type:, with_private: false)
607
- abstract_interface = builder.build_instance(type.name).without_private(!with_private)
608
-
609
- module_type = module_type || case builder.signatures.find_class_or_module(type.name)
610
- when AST::Signature::Class
611
- AST::Types::Name::Class.new(name: type.name, constructor: nil)
612
- when AST::Signature::Module
613
- AST::Types::Name::Module.new(name: type.name)
614
- end
615
-
616
- abstract_interface.instantiate(
617
- type: self_type,
618
- args: type.args,
619
- instance_type: instance_type || type,
620
- module_type: module_type
621
- )
622
- end
623
-
624
- def resolve(type, self_type: type, instance_type: nil, module_type: nil, with_private: false)
625
- Steep.logger.debug("Check#resolve: type=#{type}")
626
- case type
627
- when AST::Types::Any, AST::Types::Var, AST::Types::Class, AST::Types::Instance
628
- raise CannotResolveError.new(type: type)
629
-
630
- when AST::Types::Nil, AST::Types::Literal, AST::Types::Boolean
631
- resolve(type.back_type,
632
- self_type: self_type,
633
- instance_type: instance_type,
634
- module_type: module_type,
635
- with_private: with_private)
636
-
637
- when AST::Types::Name::Instance
638
- resolve_instance(type,
639
- self_type: self_type,
640
- instance_type: instance_type,
641
- module_type: module_type,
642
- with_private: with_private)
643
-
644
- when AST::Types::Name::Class
645
- yield_self do
646
- abstract_interface = builder.build_class(type.name, constructor: type.constructor).without_private(!with_private)
647
-
648
- unless instance_type
649
- type_params = builder.signatures.find_class(type.name).params&.variables || []
650
- instance_type = AST::Types::Name::Instance.new(name: type.name,
651
- args: type_params.map {|var| AST::Types::Var.new(name: var) })
652
- end
653
-
654
- interface = abstract_interface.instantiate(
655
- type: self_type,
656
- args: [],
657
- instance_type: instance_type,
658
- module_type: AST::Builtin::Class.class_type
659
- )
660
-
661
- if type_params
662
- interface.subst(Interface::Substitution.build(type_params, type_params.map { AST::Builtin.any_type }))
663
- else
664
- interface
665
- end
666
- end
667
-
668
- when AST::Types::Name::Module
669
- yield_self do
670
- abstract_interface = builder.build_module(type.name).without_private(!with_private)
671
-
672
- unless instance_type
673
- type_params = builder.signatures.find_module(type.name).params&.variables || []
674
- instance_type = AST::Types::Name::Instance.new(name: type.name,
675
- args: type_params.map {|var| AST::Types::Var.new(name: var) })
676
- end
677
-
678
- interface = abstract_interface.instantiate(
679
- type: self_type,
680
- args: [],
681
- instance_type: instance_type,
682
- module_type: AST::Builtin::Module.class_type
683
- )
684
-
685
- if type_params
686
- interface.subst(Interface::Substitution.build(type_params, type_params.map { AST::Builtin.any_type }))
687
- else
688
- interface
689
- end
690
- end
691
-
692
- when AST::Types::Name::Interface
693
- yield_self do
694
- abstract_interface = builder.build_interface(type.name)
695
-
696
- abstract_interface.instantiate(
697
- type: self_type,
698
- args: type.args,
699
- instance_type: nil,
700
- module_type: nil
701
- )
702
- end
703
-
704
- when AST::Types::Name::Alias
705
- resolve(expand_alias(type),
706
- self_type: self_type,
707
- instance_type: instance_type,
708
- module_type: module_type,
709
- with_private: with_private)
710
-
711
- when AST::Types::Union
712
- interfaces = type.types.map do |member_type|
713
- fresh = AST::Types::Var.fresh(:___)
714
-
715
- resolve(member_type, self_type: type, instance_type: fresh, module_type: fresh, with_private: with_private).select_method_type do |method_type|
716
- !method_type.each_type.include?(fresh)
717
- end
718
- end
719
-
720
- methods = interfaces.inject(nil) do |methods, i|
721
- if methods
722
- intersection = {}
723
- i.methods.each do |name, new_method|
724
- existing_method = methods[name]
725
-
726
- if existing_method
727
- case
728
- when new_method == existing_method
729
- intersection[name] = new_method
730
- when check_method(name, new_method, existing_method,
731
- assumption: Set.new,
732
- trace: Trace.new,
733
- constraints: Constraints.empty).success?
734
- intersection[name] = existing_method
735
- when check_method(name, existing_method, new_method,
736
- assumption: Set.new,
737
- trace: Trace.new,
738
- constraints: Constraints.empty).success?
739
- intersection[name] = new_method
740
- else
741
- merged_method_types = []
742
-
743
- existing_method.types.each do |existing_method_type|
744
- new_method.types.each do |new_method_type|
745
- if existing_method_type.params == new_method_type.params &&
746
- existing_method_type.block == new_method_type.block &&
747
- existing_method_type.type_params == new_method_type.type_params
748
- merged_method_types << existing_method_type.with(
749
- return_type: AST::Types::Union.build(
750
- types: [
751
- existing_method_type.return_type,
752
- new_method_type.return_type
753
- ]
754
- ),
755
- location: nil
756
- )
757
- end
758
- end
759
- end
760
-
761
- unless merged_method_types.empty?
762
- intersection[name] = Interface::Method.new(
763
- type_name: nil,
764
- name: name,
765
- types: merged_method_types,
766
- super_method: nil,
767
- attributes: []
768
- )
769
- end
770
- end
771
- end
772
- end
773
- intersection
774
- else
775
- i.methods
776
- end
777
- end
778
-
779
- Interface::Instantiated.new(type: type,
780
- methods: methods,
781
- ivar_chains: {})
782
-
783
- when AST::Types::Intersection
784
- interfaces = type.types.map do |type|
785
- resolve(type, with_private: with_private)
786
- end
787
-
788
- methods = interfaces.inject(nil) do |methods, i|
789
- if methods
790
- i.methods.each do |name, method|
791
- if methods.key?(name)
792
- case
793
- when method == methods[name]
794
- when check_method(name, method, methods[name],
795
- assumption: Set.new,
796
- trace: Trace.new,
797
- constraints: Constraints.empty).success?
798
- methods[name] = method
799
- when check_method(name, methods[name], method,
800
- assumption: Set.new,
801
- trace: Trace.new,
802
- constraints: Constraints.empty).success?
803
- methods[name] = methods[name]
804
- else
805
- methods[name] = Interface::Method.new(
806
- type_name: nil,
807
- name: name,
808
- types: methods[name].types + method.types,
809
- super_method: nil,
810
- attributes: []
811
- )
812
- end
813
- else
814
- methods[name] = i.methods[name]
815
- end
816
- end
817
- methods
818
- else
819
- i.methods
820
- end
821
- end
822
-
823
- ivar_chains = interfaces.each.with_object({}) do |interface, chains|
824
- chains.merge!(interface.ivar_chains)
825
- end
826
-
827
- Interface::Instantiated.new(type: type,
828
- methods: methods,
829
- ivar_chains: ivar_chains)
830
- when AST::Types::Void
831
- Interface::Instantiated.new(type: type,
832
- methods: {},
833
- ivar_chains: {})
834
-
835
- when AST::Types::Tuple
836
- yield_self do
837
- element_type = AST::Types::Union.build(types: type.types)
838
- array_type = AST::Builtin::Array.instance_type(element_type)
839
- array_interface = resolve(array_type, self_type: self_type, with_private: with_private)
840
-
841
- array_interface.methods[:[]] = array_interface.methods[:[]].yield_self do |aref|
842
- types = type.types.map.with_index {|elem_type, index|
843
- Interface::MethodType.new(
844
- type_params: [],
845
- params: Interface::Params.new(required: [AST::Types::Literal.new(value: index)],
846
- optional: [],
847
- rest: nil,
848
- required_keywords: {},
849
- optional_keywords: {},
850
- rest_keywords: nil),
851
- block: nil,
852
- return_type: elem_type,
853
- location: nil
854
- )
855
- } + aref.types
856
- aref.with_types(types)
857
- end
858
-
859
- array_interface.methods[:[]=] = array_interface.methods[:[]=].yield_self do |aref|
860
- types = type.types.map.with_index {|elem_type, index|
861
- Interface::MethodType.new(
862
- type_params: [],
863
- params: Interface::Params.new(required: [AST::Types::Literal.new(value: index), elem_type],
864
- optional: [],
865
- rest: nil,
866
- required_keywords: {},
867
- optional_keywords: {},
868
- rest_keywords: nil),
869
- block: nil,
870
- return_type: elem_type,
871
- location: nil
872
- )
873
- } + aref.types
874
- aref.with_types(types)
875
- end
876
-
877
- array_interface
878
- end
879
-
880
- when AST::Types::Record
881
- yield_self do
882
- key_type = AST::Types::Union.build(types: type.elements.keys.map {|val| AST::Types::Literal.new(value: val) })
883
- value_type = AST::Types::Union.build(types: type.elements.values)
884
- hash_interface = resolve(AST::Builtin::Hash.instance_type(key_type, value_type), self_type: self_type, with_private: with_private)
885
-
886
- hash_interface.methods[:[]] = hash_interface.methods[:[]].yield_self do |ref|
887
- types = type.elements.map do |key, value_type|
888
- Interface::MethodType.new(
889
- type_params: [],
890
- params: Interface::Params.new(required: [AST::Types::Literal.new(value: key)],
891
- optional: [],
892
- rest: nil,
893
- required_keywords: {},
894
- optional_keywords: {},
895
- rest_keywords: nil),
896
- return_type: value_type,
897
- block: nil,
898
- location: nil
899
- )
900
- end
901
-
902
- ref.with_types(types + ref.types)
903
- end
904
-
905
- hash_interface.methods[:[]=] = hash_interface.methods[:[]=].yield_self do |method|
906
- types = type.elements.map do |key, value_type|
907
- Interface::MethodType.new(
908
- type_params: [],
909
- params: Interface::Params.new(required: [AST::Types::Literal.new(value: key), value_type],
910
- optional: [],
911
- rest: nil,
912
- required_keywords: {},
913
- optional_keywords: {},
914
- rest_keywords: nil),
915
- return_type: value_type,
916
- block: nil,
917
- location: nil
918
- )
919
- end
920
-
921
- method.with_types(types + method.types)
922
- end
923
-
924
- hash_interface
925
- end
926
-
927
- when AST::Types::Proc
928
- yield_self do
929
- proc_interface = resolve(type.back_type, self_type: self_type, with_private: with_private)
930
- apply_type = Interface::MethodType.new(
931
- type_params: [],
932
- params: type.params,
933
- block: nil,
934
- return_type: type.return_type,
935
- location: nil
936
- )
937
-
938
- proc_interface.methods[:[]] = proc_interface.methods[:[]].yield_self do |aref|
939
- aref.with_types([apply_type])
940
- end
941
- proc_interface.methods[:call] = proc_interface.methods[:call].yield_self do |aref|
942
- aref.with_types([apply_type])
943
- end
944
-
945
- proc_interface
946
- end
947
-
948
- end
949
- end
950
-
951
- def expand_alias(type)
952
- expanded = case type
953
- when AST::Types::Union
954
- AST::Types::Union.build(
955
- types: type.types.map {|ty| expand_alias(ty) },
956
- location: type.location
957
- )
958
- when AST::Types::Intersection
959
- AST::Types::Intersection.build(
960
- types: type.types.map {|ty| expand_alias(ty) },
961
- location: type.location
962
- )
963
- when AST::Types::Name::Alias
964
- alias_sig = builder.signatures.find_alias(type.name, namespace: AST::Namespace.root)
965
- expanded_alias = builder.absolute_type(alias_sig.type, current: alias_sig.name.namespace)
966
- args = type.args.map {|ty| expand_alias(ty) }
967
- s = Interface::Substitution.build(alias_sig.params&.variables || [], args)
968
- expand_alias(expanded_alias.subst(s))
969
- else
970
- type
971
- end
972
-
973
- if block_given?
974
- yield expanded
975
- else
976
- expanded
977
- end
700
+ def expand_alias(type, &block)
701
+ factory.expand_alias(type, &block)
978
702
  end
979
703
  end
980
704
  end