steep 0.37.0 → 0.42.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/CHANGELOG.md +34 -0
  4. data/Rakefile +5 -2
  5. data/bin/output_rebaseline.rb +34 -0
  6. data/bin/output_test.rb +53 -0
  7. data/lib/steep.rb +95 -14
  8. data/lib/steep/ast/types/bot.rb +1 -1
  9. data/lib/steep/ast/types/class.rb +4 -0
  10. data/lib/steep/ast/types/factory.rb +10 -0
  11. data/lib/steep/ast/types/logic.rb +16 -3
  12. data/lib/steep/ast/types/top.rb +1 -1
  13. data/lib/steep/cli.rb +31 -7
  14. data/lib/steep/diagnostic/helper.rb +17 -0
  15. data/lib/steep/diagnostic/lsp_formatter.rb +16 -0
  16. data/lib/steep/diagnostic/ruby.rb +619 -0
  17. data/lib/steep/diagnostic/signature.rb +357 -0
  18. data/lib/steep/drivers/annotations.rb +19 -28
  19. data/lib/steep/drivers/check.rb +182 -60
  20. data/lib/steep/drivers/diagnostic_printer.rb +99 -0
  21. data/lib/steep/drivers/langserver.rb +3 -8
  22. data/lib/steep/drivers/print_project.rb +10 -9
  23. data/lib/steep/drivers/stats.rb +124 -32
  24. data/lib/steep/drivers/trace_printer.rb +5 -1
  25. data/lib/steep/drivers/utils/jobs_count.rb +9 -0
  26. data/lib/steep/drivers/validate.rb +31 -13
  27. data/lib/steep/drivers/watch.rb +69 -48
  28. data/lib/steep/drivers/worker.rb +16 -8
  29. data/lib/steep/expectations.rb +159 -0
  30. data/lib/steep/index/rbs_index.rb +334 -0
  31. data/lib/steep/index/signature_symbol_provider.rb +162 -0
  32. data/lib/steep/index/source_index.rb +100 -0
  33. data/lib/steep/project.rb +0 -30
  34. data/lib/steep/project/dsl.rb +5 -3
  35. data/lib/steep/project/options.rb +4 -4
  36. data/lib/steep/project/pattern.rb +56 -0
  37. data/lib/steep/project/target.rb +9 -214
  38. data/lib/steep/range_extension.rb +29 -0
  39. data/lib/steep/server/base_worker.rb +43 -7
  40. data/lib/steep/server/change_buffer.rb +63 -0
  41. data/lib/steep/server/interaction_worker.rb +73 -56
  42. data/lib/steep/server/master.rb +245 -109
  43. data/lib/steep/server/type_check_worker.rb +122 -0
  44. data/lib/steep/server/worker_process.rb +17 -15
  45. data/lib/steep/{project → services}/completion_provider.rb +3 -3
  46. data/lib/steep/services/content_change.rb +61 -0
  47. data/lib/steep/services/file_loader.rb +48 -0
  48. data/lib/steep/{project → services}/hover_content.rb +14 -16
  49. data/lib/steep/services/path_assignment.rb +29 -0
  50. data/lib/steep/services/signature_service.rb +369 -0
  51. data/lib/steep/services/stats_calculator.rb +69 -0
  52. data/lib/steep/services/type_check_service.rb +342 -0
  53. data/lib/steep/signature/validator.rb +174 -32
  54. data/lib/steep/subtyping/check.rb +248 -47
  55. data/lib/steep/subtyping/constraints.rb +2 -2
  56. data/lib/steep/type_construction.rb +565 -295
  57. data/lib/steep/type_inference/constant_env.rb +5 -1
  58. data/lib/steep/type_inference/local_variable_type_env.rb +26 -12
  59. data/lib/steep/type_inference/logic_type_interpreter.rb +99 -26
  60. data/lib/steep/type_inference/type_env.rb +43 -17
  61. data/lib/steep/typing.rb +8 -2
  62. data/lib/steep/version.rb +1 -1
  63. data/smoke/alias/a.rb +0 -3
  64. data/smoke/alias/b.rb +0 -1
  65. data/smoke/alias/c.rb +0 -2
  66. data/smoke/alias/test_expectations.yml +96 -0
  67. data/smoke/and/a.rb +0 -3
  68. data/smoke/and/test_expectations.yml +31 -0
  69. data/smoke/array/a.rb +0 -3
  70. data/smoke/array/b.rb +0 -2
  71. data/smoke/array/c.rb +0 -1
  72. data/smoke/array/test_expectations.yml +103 -0
  73. data/smoke/block/a.rb +0 -2
  74. data/smoke/block/b.rb +0 -2
  75. data/smoke/block/d.rb +0 -4
  76. data/smoke/block/test_expectations.yml +125 -0
  77. data/smoke/case/a.rb +0 -3
  78. data/smoke/case/test_expectations.yml +47 -0
  79. data/smoke/class/a.rb +0 -3
  80. data/smoke/class/c.rb +0 -1
  81. data/smoke/class/f.rb +0 -1
  82. data/smoke/class/g.rb +0 -2
  83. data/smoke/class/i.rb +0 -2
  84. data/smoke/class/test_expectations.yml +120 -0
  85. data/smoke/const/a.rb +0 -3
  86. data/smoke/const/b.rb +7 -0
  87. data/smoke/const/b.rbs +5 -0
  88. data/smoke/const/test_expectations.yml +139 -0
  89. data/smoke/diagnostics-rbs-duplicated/Steepfile +5 -0
  90. data/smoke/diagnostics-rbs-duplicated/a.rbs +5 -0
  91. data/smoke/diagnostics-rbs-duplicated/test_expectations.yml +13 -0
  92. data/smoke/diagnostics-rbs/Steepfile +8 -0
  93. data/smoke/diagnostics-rbs/duplicated-method-definition.rbs +20 -0
  94. data/smoke/diagnostics-rbs/generic-parameter-mismatch.rbs +7 -0
  95. data/smoke/diagnostics-rbs/invalid-method-overload.rbs +3 -0
  96. data/smoke/diagnostics-rbs/invalid-type-application.rbs +7 -0
  97. data/smoke/diagnostics-rbs/invalid_variance_annotation.rbs +3 -0
  98. data/smoke/diagnostics-rbs/recursive-alias.rbs +5 -0
  99. data/smoke/diagnostics-rbs/recursive-class.rbs +8 -0
  100. data/smoke/diagnostics-rbs/superclass-mismatch.rbs +7 -0
  101. data/smoke/diagnostics-rbs/test_expectations.yml +231 -0
  102. data/smoke/diagnostics-rbs/unknown-method-alias.rbs +3 -0
  103. data/smoke/diagnostics-rbs/unknown-type-name-2.rbs +5 -0
  104. data/smoke/diagnostics-rbs/unknown-type-name.rbs +13 -0
  105. data/smoke/diagnostics/Steepfile +5 -0
  106. data/smoke/diagnostics/a.rbs +26 -0
  107. data/smoke/diagnostics/argument_type_mismatch.rb +1 -0
  108. data/smoke/diagnostics/block_body_type_mismatch.rb +1 -0
  109. data/smoke/diagnostics/block_type_mismatch.rb +3 -0
  110. data/smoke/diagnostics/break_type_mismatch.rb +1 -0
  111. data/smoke/diagnostics/else_on_exhaustive_case.rb +12 -0
  112. data/smoke/diagnostics/incompatible_annotation.rb +6 -0
  113. data/smoke/diagnostics/incompatible_argument.rb +1 -0
  114. data/smoke/diagnostics/incompatible_assignment.rb +8 -0
  115. data/smoke/diagnostics/method_arity_mismatch.rb +11 -0
  116. data/smoke/diagnostics/method_body_type_mismatch.rb +6 -0
  117. data/smoke/diagnostics/method_definition_missing.rb +2 -0
  118. data/smoke/diagnostics/method_return_type_annotation_mismatch.rb +7 -0
  119. data/smoke/diagnostics/missing_keyword.rb +1 -0
  120. data/smoke/diagnostics/no_method.rb +1 -0
  121. data/smoke/diagnostics/required_block_missing.rb +1 -0
  122. data/smoke/diagnostics/return_type_mismatch.rb +6 -0
  123. data/smoke/diagnostics/test_expectations.yml +477 -0
  124. data/smoke/diagnostics/unexpected_block_given.rb +1 -0
  125. data/smoke/diagnostics/unexpected_dynamic_method.rb +3 -0
  126. data/smoke/diagnostics/unexpected_jump.rb +4 -0
  127. data/smoke/diagnostics/unexpected_jump_value.rb +3 -0
  128. data/smoke/diagnostics/unexpected_keyword.rb +1 -0
  129. data/smoke/diagnostics/unexpected_splat.rb +1 -0
  130. data/smoke/diagnostics/unexpected_yield.rb +6 -0
  131. data/smoke/diagnostics/unknown_constant_assigned.rb +7 -0
  132. data/smoke/diagnostics/unresolved_overloading.rb +1 -0
  133. data/smoke/diagnostics/unsatisfiable_constraint.rb +7 -0
  134. data/smoke/diagnostics/unsupported_syntax.rb +2 -0
  135. data/smoke/dstr/a.rb +0 -1
  136. data/smoke/dstr/test_expectations.yml +13 -0
  137. data/smoke/ensure/a.rb +0 -4
  138. data/smoke/ensure/test_expectations.yml +62 -0
  139. data/smoke/enumerator/a.rb +0 -6
  140. data/smoke/enumerator/b.rb +0 -3
  141. data/smoke/enumerator/test_expectations.yml +135 -0
  142. data/smoke/extension/a.rb +0 -1
  143. data/smoke/extension/b.rb +0 -2
  144. data/smoke/extension/c.rb +0 -1
  145. data/smoke/extension/f.rb +2 -0
  146. data/smoke/extension/f.rbs +3 -0
  147. data/smoke/extension/test_expectations.yml +73 -0
  148. data/smoke/hash/b.rb +0 -1
  149. data/smoke/hash/c.rb +0 -3
  150. data/smoke/hash/d.rb +0 -1
  151. data/smoke/hash/e.rb +0 -1
  152. data/smoke/hash/test_expectations.yml +81 -0
  153. data/smoke/hello/hello.rb +0 -2
  154. data/smoke/hello/test_expectations.yml +25 -0
  155. data/smoke/if/a.rb +0 -2
  156. data/smoke/if/test_expectations.yml +34 -0
  157. data/smoke/implements/a.rb +0 -2
  158. data/smoke/implements/test_expectations.yml +23 -0
  159. data/smoke/initialize/test_expectations.yml +1 -0
  160. data/smoke/integer/a.rb +0 -7
  161. data/smoke/integer/test_expectations.yml +101 -0
  162. data/smoke/interface/a.rb +0 -2
  163. data/smoke/interface/test_expectations.yml +23 -0
  164. data/smoke/kwbegin/a.rb +0 -1
  165. data/smoke/kwbegin/test_expectations.yml +17 -0
  166. data/smoke/lambda/a.rb +1 -4
  167. data/smoke/lambda/test_expectations.yml +39 -0
  168. data/smoke/literal/a.rb +0 -5
  169. data/smoke/literal/b.rb +0 -2
  170. data/smoke/literal/test_expectations.yml +106 -0
  171. data/smoke/map/test_expectations.yml +1 -0
  172. data/smoke/method/a.rb +0 -5
  173. data/smoke/method/b.rb +0 -1
  174. data/smoke/method/test_expectations.yml +90 -0
  175. data/smoke/module/a.rb +0 -2
  176. data/smoke/module/b.rb +0 -2
  177. data/smoke/module/c.rb +0 -1
  178. data/smoke/module/d.rb +0 -1
  179. data/smoke/module/f.rb +0 -2
  180. data/smoke/module/test_expectations.yml +75 -0
  181. data/smoke/regexp/a.rb +0 -38
  182. data/smoke/regexp/b.rb +0 -26
  183. data/smoke/regexp/test_expectations.yml +615 -0
  184. data/smoke/regression/set_divide.rb +0 -4
  185. data/smoke/regression/test_expectations.yml +43 -0
  186. data/smoke/rescue/a.rb +0 -5
  187. data/smoke/rescue/test_expectations.yml +79 -0
  188. data/smoke/self/a.rb +0 -2
  189. data/smoke/self/test_expectations.yml +23 -0
  190. data/smoke/skip/skip.rb +0 -2
  191. data/smoke/skip/test_expectations.yml +23 -0
  192. data/smoke/stdout/test_expectations.yml +1 -0
  193. data/smoke/super/a.rb +0 -4
  194. data/smoke/super/test_expectations.yml +79 -0
  195. data/smoke/toplevel/a.rb +0 -1
  196. data/smoke/toplevel/test_expectations.yml +15 -0
  197. data/smoke/tsort/Steepfile +2 -0
  198. data/smoke/tsort/a.rb +0 -3
  199. data/smoke/tsort/test_expectations.yml +63 -0
  200. data/smoke/type_case/a.rb +0 -4
  201. data/smoke/type_case/test_expectations.yml +48 -0
  202. data/smoke/unexpected/Steepfile +5 -0
  203. data/smoke/unexpected/test_expectations.yml +25 -0
  204. data/smoke/unexpected/unexpected.rb +1 -0
  205. data/smoke/unexpected/unexpected.rbs +3 -0
  206. data/smoke/yield/a.rb +0 -3
  207. data/smoke/yield/b.rb +6 -0
  208. data/smoke/yield/test_expectations.yml +68 -0
  209. data/steep.gemspec +4 -3
  210. metadata +144 -29
  211. data/bin/smoke_runner.rb +0 -139
  212. data/lib/steep/drivers/signature_error_printer.rb +0 -25
  213. data/lib/steep/errors.rb +0 -565
  214. data/lib/steep/project/file_loader.rb +0 -68
  215. data/lib/steep/project/signature_file.rb +0 -33
  216. data/lib/steep/project/source_file.rb +0 -129
  217. data/lib/steep/server/code_worker.rb +0 -137
  218. data/lib/steep/server/signature_worker.rb +0 -152
  219. data/lib/steep/server/utils.rb +0 -69
  220. data/lib/steep/signature/errors.rb +0 -82
  221. data/lib/steep/type_assignability.rb +0 -367
@@ -15,9 +15,13 @@ module Steep
15
15
  @table = RBS::ConstantTable.new(builder: factory.definition_builder)
16
16
  end
17
17
 
18
+ def lookup_constant(name)
19
+ table.resolve_constant_reference(name, context: context)
20
+ end
21
+
18
22
  def lookup(name)
19
23
  cache[name] ||= begin
20
- constant = table.resolve_constant_reference(name, context: context)
24
+ constant = lookup_constant(name)
21
25
 
22
26
  if constant
23
27
  factory.type(constant.type)
@@ -1,9 +1,6 @@
1
1
  module Steep
2
2
  module TypeInference
3
3
  class LocalVariableTypeEnv
4
- attr_reader :subtyping
5
- attr_reader :self_type
6
-
7
4
  class Entry
8
5
  attr_reader :type
9
6
  attr_reader :annotations
@@ -39,31 +36,46 @@ module Steep
39
36
  end
40
37
  end
41
38
 
39
+ attr_reader :subtyping
40
+ attr_reader :self_type
41
+ attr_reader :instance_type
42
+ attr_reader :class_type
42
43
  attr_reader :declared_types
43
44
  attr_reader :assigned_types
44
45
 
45
- def self.empty(subtyping:, self_type:)
46
- new(subtyping: subtyping, declared_types: {}, assigned_types: {}, self_type: self_type)
46
+ def self.empty(subtyping:, self_type:, instance_type:, class_type:)
47
+ new(
48
+ subtyping: subtyping,
49
+ declared_types: {},
50
+ assigned_types: {},
51
+ self_type: self_type,
52
+ instance_type: instance_type,
53
+ class_type: class_type
54
+ )
47
55
  end
48
56
 
49
- def initialize(subtyping:, declared_types:, assigned_types:, self_type:)
57
+ def initialize(subtyping:, declared_types:, assigned_types:, self_type:, instance_type:, class_type:)
50
58
  @subtyping = subtyping
51
59
  @self_type = self_type
52
60
 
53
61
  @declared_types = declared_types
54
62
  @assigned_types = assigned_types
63
+ @class_type = class_type
64
+ @instance_type = instance_type
55
65
 
56
66
  unless (intersection = Set.new(declared_types.keys) & Set.new(assigned_types.keys)).empty?
57
67
  raise "Declared types and assigned types should be disjoint: #{intersection}"
58
68
  end
59
69
  end
60
70
 
61
- def update(declared_types: self.declared_types, assigned_types: self.assigned_types, self_type: self.self_type)
71
+ def update(declared_types: self.declared_types, assigned_types: self.assigned_types, self_type: self.self_type, instance_type: self.instance_type, class_type: self.class_type)
62
72
  self.class.new(
63
73
  subtyping: subtyping,
64
74
  declared_types: declared_types,
65
75
  assigned_types: assigned_types,
66
- self_type: self_type
76
+ self_type: self_type,
77
+ instance_type: instance_type,
78
+ class_type: class_type
67
79
  )
68
80
  end
69
81
 
@@ -73,7 +85,7 @@ module Steep
73
85
  if declared_type
74
86
  relation = Subtyping::Relation.new(sub_type: type, super_type: declared_type)
75
87
  constraints = Subtyping::Constraints.new(unknowns: Set.new)
76
- subtyping.check(relation, constraints: constraints, self_type: self_type).else do |result|
88
+ subtyping.check(relation, constraints: constraints, self_type: self_type, instance_type: instance_type, class_type: class_type).else do |result|
77
89
  yield declared_type, type, result
78
90
  end
79
91
  end
@@ -89,7 +101,7 @@ module Steep
89
101
  if declared_type
90
102
  relation = Subtyping::Relation.new(sub_type: type, super_type: declared_type)
91
103
  constraints = Subtyping::Constraints.new(unknowns: Set.new)
92
- subtyping.check(relation, constraints: constraints, self_type: self_type).else do |result|
104
+ subtyping.check(relation, constraints: constraints, self_type: self_type, instance_type: instance_type, class_type: class_type).else do |result|
93
105
  yield declared_type, type, result
94
106
  end
95
107
 
@@ -113,7 +125,7 @@ module Steep
113
125
  if outer_type
114
126
  relation = Subtyping::Relation.new(sub_type: inner_type, super_type: outer_type)
115
127
  constraints = Subtyping::Constraints.new(unknowns: Set.new)
116
- subtyping.check(relation, constraints: constraints, self_type: self_type).else do |result|
128
+ subtyping.check(relation, constraints: constraints, self_type: self_type, instance_type: instance_type, class_type: class_type).else do |result|
117
129
  if block_given?
118
130
  yield var, outer_type, inner_type, result
119
131
  end
@@ -194,7 +206,9 @@ module Steep
194
206
  subtyping: subtyping,
195
207
  self_type: self_type,
196
208
  declared_types: declared_types,
197
- assigned_types: assigned_types
209
+ assigned_types: assigned_types,
210
+ instance_type: instance_type,
211
+ class_type: class_type
198
212
  )
199
213
  end
200
214
 
@@ -41,8 +41,11 @@ module Steep
41
41
 
42
42
  if type.is_a?(AST::Types::Logic::Base)
43
43
  vars.each do |var_name|
44
- truthy_env = truthy_env.assign!(var_name, node: node, type: AST::Builtin.true_type)
45
- falsy_env = truthy_env.assign!(var_name, node: node, type: AST::Builtin.false_type)
44
+ var_type = truthy_env[var_name]
45
+ truthy_type, falsy_type = factory.unwrap_optional(var_type)
46
+ falsy_type ||= AST::Builtin.nil_type
47
+ truthy_env = truthy_env.assign!(var_name, node: node, type: truthy_type) {|_, type, _| type }
48
+ falsy_env = truthy_env.assign!(var_name, node: node, type: falsy_type) {|_, type, _| type }
46
49
  end
47
50
 
48
51
  case type
@@ -77,11 +80,16 @@ module Steep
77
80
 
78
81
  if arg_type.is_a?(AST::Types::Name::Singleton)
79
82
  receiver_vars.each do |var_name|
80
- var_type = env[var_name]
81
- truthy_type, falsy_type = type_case_select(var_type, arg_type.name)
82
-
83
- truthy_env = truthy_env.assign!(var_name, node: node, type: truthy_type)
84
- falsy_env = falsy_env.assign!(var_name, node: node, type: falsy_type)
83
+ case var_name
84
+ when :_, :__any__, :__skip__
85
+ # skip
86
+ else
87
+ var_type = env[var_name]
88
+ truthy_type, falsy_type = type_case_select(var_type, arg_type.name)
89
+
90
+ truthy_env = truthy_env.assign!(var_name, node: node, type: truthy_type)
91
+ falsy_env = falsy_env.assign!(var_name, node: node, type: falsy_type)
92
+ end
85
93
  end
86
94
  end
87
95
  end
@@ -93,7 +101,7 @@ module Steep
93
101
 
94
102
  if receiver
95
103
  _, arg_vars = decompose_value(arg)
96
- receiver_type = typing.type_of(node: receiver)
104
+ receiver_type = factory.deep_expand_alias(typing.type_of(node: receiver))
97
105
 
98
106
  if receiver_type.is_a?(AST::Types::Name::Singleton)
99
107
  arg_vars.each do |var_name|
@@ -106,6 +114,23 @@ module Steep
106
114
  end
107
115
  end
108
116
  end
117
+ when AST::Types::Logic::ArgEqualsReceiver
118
+ case value_node.type
119
+ when :send
120
+ receiver, _, arg = value_node.children
121
+
122
+ if receiver
123
+ _, arg_vars = decompose_value(arg)
124
+
125
+ arg_vars.each do |var_name|
126
+ var_type = factory.deep_expand_alias(env[var_name])
127
+ truthy_types, falsy_types = literal_var_type_case_select(receiver, var_type)
128
+
129
+ truthy_env = truthy_env.assign!(var_name, node: node, type: AST::Types::Union.build(types: truthy_types, location: nil))
130
+ falsy_env = falsy_env.assign!(var_name, node: node, type: AST::Types::Union.build(types: falsy_types, location: nil))
131
+ end
132
+ end
133
+ end
109
134
  when AST::Types::Logic::Not
110
135
  receiver, * = value_node.children
111
136
  receiver_type = typing.type_of(node: receiver)
@@ -156,6 +181,60 @@ module Steep
156
181
  end
157
182
  end
158
183
 
184
+ def literal_var_type_case_select(value_node, arg_type)
185
+ case arg_type
186
+ when AST::Types::Union
187
+ truthy_types = []
188
+ falsy_types = []
189
+
190
+ arg_type.types.each do |type|
191
+ ts, fs = literal_var_type_case_select(value_node, type)
192
+ truthy_types.push(*ts)
193
+ falsy_types.push(*fs)
194
+ end
195
+
196
+ [truthy_types, falsy_types]
197
+ else
198
+ value_type = typing.type_of(node: value_node)
199
+ types = [arg_type]
200
+
201
+ case value_node.type
202
+ when :nil
203
+ types.partition do |type|
204
+ type.is_a?(AST::Types::Nil) || AST::Builtin::NilClass.instance_type?(type)
205
+ end
206
+ when :true
207
+ types.partition do |type|
208
+ AST::Builtin::TrueClass.instance_type?(type) ||
209
+ (type.is_a?(AST::Types::Literal) && type.value == true)
210
+ end
211
+ when :false
212
+ types.partition do |type|
213
+ AST::Builtin::FalseClass.instance_type?(type) ||
214
+ (type.is_a?(AST::Types::Literal) && type.value == false)
215
+ end
216
+ when :int, :str, :sym
217
+ types.each.with_object([[], []]) do |type, pair|
218
+ true_types, false_types = pair
219
+
220
+ case
221
+ when type.is_a?(AST::Types::Literal)
222
+ if type.value == value_node.children[0]
223
+ true_types << type
224
+ else
225
+ false_types << type
226
+ end
227
+ else
228
+ true_types << AST::Types::Literal.new(value: value_node.children[0])
229
+ false_types << type
230
+ end
231
+ end
232
+ else
233
+ [[arg_type], [arg_type]]
234
+ end
235
+ end
236
+ end
237
+
159
238
  def type_case_select(type, klass)
160
239
  truth_types, false_types = type_case_select0(type, klass)
161
240
 
@@ -186,20 +265,6 @@ module Steep
186
265
 
187
266
  [truthy_types, falsy_types]
188
267
 
189
- when AST::Types::Name::Instance
190
- relation = Subtyping::Relation.new(sub_type: type, super_type: instance_type)
191
- if subtyping.check(relation, constraints: Subtyping::Constraints.empty, self_type: AST::Types::Self.new).success?
192
- [
193
- [type],
194
- []
195
- ]
196
- else
197
- [
198
- [],
199
- [type]
200
- ]
201
- end
202
-
203
268
  when AST::Types::Name::Alias
204
269
  ty = factory.expand_alias(type)
205
270
  type_case_select0(ty, klass)
@@ -217,10 +282,18 @@ module Steep
217
282
  ]
218
283
 
219
284
  else
220
- [
221
- [],
222
- [type]
223
- ]
285
+ relation = Subtyping::Relation.new(sub_type: type, super_type: instance_type)
286
+ if subtyping.check(relation, constraints: Subtyping::Constraints.empty, self_type: AST::Types::Self.new, instance_type: AST::Types::Instance.new, class_type: AST::Types::Class.new).success?
287
+ [
288
+ [type],
289
+ []
290
+ ]
291
+ else
292
+ [
293
+ [],
294
+ [type]
295
+ ]
296
+ end
224
297
  end
225
298
  end
226
299
  end
@@ -38,19 +38,23 @@ module Steep
38
38
  end
39
39
  end
40
40
 
41
- def with_annotations(ivar_types: {}, const_types: {}, gvar_types: {}, self_type:, &block)
41
+ def with_annotations(ivar_types: {}, const_types: {}, gvar_types: {}, self_type:, instance_type:, class_type:, &block)
42
42
  dup.tap do |env|
43
- merge!(original_env: env.ivar_types, override_env: ivar_types, self_type: self_type, &block)
44
- merge!(original_env: env.gvar_types, override_env: gvar_types, self_type: self_type, &block)
43
+ merge!(original_env: env.ivar_types, override_env: ivar_types, self_type: self_type, instance_type: instance_type, class_type: class_type, &block)
44
+ merge!(original_env: env.gvar_types, override_env: gvar_types, self_type: self_type, instance_type: instance_type, class_type: class_type, &block)
45
45
 
46
46
  const_types.each do |name, annotated_type|
47
47
  original_type = self.const_types[name] || const_env.lookup(name)
48
48
  if original_type
49
- assert_annotation name,
50
- original_type: original_type,
51
- annotated_type: annotated_type,
52
- self_type: self_type,
53
- &block
49
+ assert_annotation(
50
+ name,
51
+ original_type: original_type,
52
+ annotated_type: annotated_type,
53
+ self_type: self_type,
54
+ instance_type: instance_type,
55
+ class_type: class_type,
56
+ &block
57
+ )
54
58
  end
55
59
  env.const_types[name] = annotated_type
56
60
  end
@@ -101,13 +105,20 @@ module Steep
101
105
  # @type method assign: (const: TypeName, type: AST::Type) { (Subtyping::Result::Failure | nil) -> void } -> AST::Type
102
106
  # | (gvar: Symbol, type: AST::Type) { (Subtyping::Result::Failure | nil) -> void } -> AST::Type
103
107
  # | (ivar: Symbol, type: AST::Type) { (Subtyping::Result::Failure | nil) -> void } -> AST::Type
104
- def assign(const: nil, gvar: nil, ivar: nil, type:, self_type:, &block)
108
+ def assign(const: nil, gvar: nil, ivar: nil, type:, self_type:, instance_type:, class_type:, &block)
105
109
  case
106
110
  when const
107
111
  yield_self do
108
112
  const_type = const_types[const] || const_env.lookup(const)
109
113
  if const_type
110
- assert_assign(var_type: const_type, lhs_type: type, self_type: self_type, &block)
114
+ assert_assign(
115
+ var_type: const_type,
116
+ lhs_type: type,
117
+ self_type: self_type,
118
+ instance_type: instance_type,
119
+ class_type: class_type,
120
+ &block
121
+ )
111
122
  else
112
123
  yield nil
113
124
  AST::Types::Any.new
@@ -116,7 +127,14 @@ module Steep
116
127
  else
117
128
  lookup_dictionary(ivar: ivar, gvar: gvar) do |var_name, dictionary|
118
129
  if dictionary.key?(var_name)
119
- assert_assign(var_type: dictionary[var_name], lhs_type: type, self_type: self_type, &block)
130
+ assert_assign(
131
+ var_type: dictionary[var_name],
132
+ lhs_type: type,
133
+ self_type: self_type,
134
+ instance_type: instance_type,
135
+ class_type: class_type,
136
+ &block
137
+ )
120
138
  else
121
139
  yield nil
122
140
  AST::Types::Any.new
@@ -134,7 +152,7 @@ module Steep
134
152
  end
135
153
  end
136
154
 
137
- def assert_assign(var_type:, lhs_type:, self_type:)
155
+ def assert_assign(var_type:, lhs_type:, self_type:, instance_type:, class_type:)
138
156
  return var_type if var_type == lhs_type
139
157
 
140
158
  var_type = subtyping.expand_alias(var_type)
@@ -143,20 +161,28 @@ module Steep
143
161
  relation = Subtyping::Relation.new(sub_type: lhs_type, super_type: var_type)
144
162
  constraints = Subtyping::Constraints.new(unknowns: Set.new)
145
163
 
146
- subtyping.check(relation, self_type: self_type, constraints: constraints).else do |result|
164
+ subtyping.check(relation, self_type: self_type, constraints: constraints, instance_type: instance_type, class_type: class_type).else do |result|
147
165
  yield result
148
166
  end
149
167
 
150
168
  var_type
151
169
  end
152
170
 
153
- def merge!(original_env:, override_env:, self_type:, &block)
171
+ def merge!(original_env:, override_env:, self_type:, instance_type:, class_type:, &block)
154
172
  original_env.merge!(override_env) do |name, original_type, override_type|
155
- assert_annotation name, annotated_type: override_type, original_type: original_type, self_type: self_type, &block
173
+ assert_annotation(
174
+ name,
175
+ annotated_type: override_type,
176
+ original_type: original_type,
177
+ self_type: self_type,
178
+ instance_type: instance_type,
179
+ class_type: class_type,
180
+ &block
181
+ )
156
182
  end
157
183
  end
158
184
 
159
- def assert_annotation(name, annotated_type:, original_type:, self_type:)
185
+ def assert_annotation(name, annotated_type:, original_type:, self_type:, instance_type:, class_type:)
160
186
  return annotated_type if annotated_type == original_type
161
187
 
162
188
  annotated_type = subtyping.expand_alias(annotated_type)
@@ -165,7 +191,7 @@ module Steep
165
191
  relation = Subtyping::Relation.new(sub_type: annotated_type, super_type: original_type)
166
192
  constraints = Subtyping::Constraints.new(unknowns: Set.new)
167
193
 
168
- subtyping.check(relation, constraints: constraints, self_type: self_type).else do |result|
194
+ subtyping.check(relation, constraints: constraints, self_type: self_type, instance_type: instance_type, class_type: class_type).else do |result|
169
195
  yield name, relation, result
170
196
  end
171
197
 
data/lib/steep/typing.rb CHANGED
@@ -21,8 +21,9 @@ module Steep
21
21
  attr_reader :contexts
22
22
  attr_reader :root_context
23
23
  attr_reader :method_calls
24
+ attr_reader :source_index
24
25
 
25
- def initialize(source:, root_context:, parent: nil, parent_last_update: parent&.last_update, contexts: nil)
26
+ def initialize(source:, root_context:, parent: nil, parent_last_update: parent&.last_update, contexts: nil, source_index: nil)
26
27
  @source = source
27
28
 
28
29
  @parent = parent
@@ -35,6 +36,8 @@ module Steep
35
36
  @root_context = root_context
36
37
  @contexts = contexts || TypeInference::ContextArray.from_source(source: source)
37
38
  @method_calls = {}.compare_by_identity
39
+
40
+ @source_index = source_index || Index::SourceIndex.new(source: source)
38
41
  end
39
42
 
40
43
  def add_error(error)
@@ -195,7 +198,8 @@ module Steep
195
198
  child = self.class.new(source: source,
196
199
  parent: self,
197
200
  root_context: root_context,
198
- contexts: TypeInference::ContextArray.new(buffer: contexts.buffer, range: range, context: nil))
201
+ contexts: TypeInference::ContextArray.new(buffer: contexts.buffer, range: range, context: nil),
202
+ source_index: source_index.new_child)
199
203
  @should_update = true
200
204
 
201
205
  if block_given?
@@ -224,6 +228,8 @@ module Steep
224
228
  errors.each do |error|
225
229
  parent.add_error error
226
230
  end
231
+
232
+ parent.source_index.merge!(source_index)
227
233
  end
228
234
  end
229
235
  end
data/lib/steep/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Steep
2
- VERSION = "0.37.0"
2
+ VERSION = "0.42.0"
3
3
  end