steep-relaxed 1.9.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.gitmodules +0 -0
  4. data/CHANGELOG.md +1032 -0
  5. data/LICENSE +21 -0
  6. data/README.md +260 -0
  7. data/Rakefile +227 -0
  8. data/STDGEM_DEPENDENCIES.txt +59 -0
  9. data/Steepfile +68 -0
  10. data/bin/console +14 -0
  11. data/bin/generate-diagnostics-docs.rb +112 -0
  12. data/bin/mem_graph.rb +67 -0
  13. data/bin/mem_prof.rb +102 -0
  14. data/bin/output_rebaseline.rb +34 -0
  15. data/bin/output_test.rb +60 -0
  16. data/bin/rbs +20 -0
  17. data/bin/rbs-inline +19 -0
  18. data/bin/setup +9 -0
  19. data/bin/stackprof_test.rb +19 -0
  20. data/bin/steep +19 -0
  21. data/bin/steep-check.rb +251 -0
  22. data/bin/steep-prof +16 -0
  23. data/doc/narrowing.md +195 -0
  24. data/doc/shape.md +194 -0
  25. data/exe/steep +18 -0
  26. data/guides/README.md +5 -0
  27. data/guides/src/gem-rbs-collection/gem-rbs-collection.md +126 -0
  28. data/guides/src/getting-started/getting-started.md +163 -0
  29. data/guides/src/nil-optional/nil-optional.md +195 -0
  30. data/lib/steep/annotation_parser.rb +199 -0
  31. data/lib/steep/ast/annotation/collection.rb +172 -0
  32. data/lib/steep/ast/annotation.rb +137 -0
  33. data/lib/steep/ast/builtin.rb +104 -0
  34. data/lib/steep/ast/ignore.rb +148 -0
  35. data/lib/steep/ast/node/type_application.rb +88 -0
  36. data/lib/steep/ast/node/type_assertion.rb +81 -0
  37. data/lib/steep/ast/types/any.rb +35 -0
  38. data/lib/steep/ast/types/boolean.rb +45 -0
  39. data/lib/steep/ast/types/bot.rb +35 -0
  40. data/lib/steep/ast/types/class.rb +43 -0
  41. data/lib/steep/ast/types/factory.rb +557 -0
  42. data/lib/steep/ast/types/helper.rb +40 -0
  43. data/lib/steep/ast/types/instance.rb +42 -0
  44. data/lib/steep/ast/types/intersection.rb +93 -0
  45. data/lib/steep/ast/types/literal.rb +59 -0
  46. data/lib/steep/ast/types/logic.rb +84 -0
  47. data/lib/steep/ast/types/name.rb +128 -0
  48. data/lib/steep/ast/types/nil.rb +41 -0
  49. data/lib/steep/ast/types/proc.rb +117 -0
  50. data/lib/steep/ast/types/record.rb +79 -0
  51. data/lib/steep/ast/types/self.rb +43 -0
  52. data/lib/steep/ast/types/shared_instance.rb +11 -0
  53. data/lib/steep/ast/types/top.rb +35 -0
  54. data/lib/steep/ast/types/tuple.rb +60 -0
  55. data/lib/steep/ast/types/union.rb +97 -0
  56. data/lib/steep/ast/types/var.rb +65 -0
  57. data/lib/steep/ast/types/void.rb +35 -0
  58. data/lib/steep/cli.rb +401 -0
  59. data/lib/steep/diagnostic/deprecated/else_on_exhaustive_case.rb +20 -0
  60. data/lib/steep/diagnostic/deprecated/unknown_constant_assigned.rb +28 -0
  61. data/lib/steep/diagnostic/helper.rb +18 -0
  62. data/lib/steep/diagnostic/lsp_formatter.rb +78 -0
  63. data/lib/steep/diagnostic/result_printer2.rb +48 -0
  64. data/lib/steep/diagnostic/ruby.rb +1221 -0
  65. data/lib/steep/diagnostic/signature.rb +570 -0
  66. data/lib/steep/drivers/annotations.rb +52 -0
  67. data/lib/steep/drivers/check.rb +339 -0
  68. data/lib/steep/drivers/checkfile.rb +210 -0
  69. data/lib/steep/drivers/diagnostic_printer.rb +105 -0
  70. data/lib/steep/drivers/init.rb +66 -0
  71. data/lib/steep/drivers/langserver.rb +56 -0
  72. data/lib/steep/drivers/print_project.rb +113 -0
  73. data/lib/steep/drivers/stats.rb +203 -0
  74. data/lib/steep/drivers/utils/driver_helper.rb +143 -0
  75. data/lib/steep/drivers/utils/jobs_option.rb +26 -0
  76. data/lib/steep/drivers/vendor.rb +27 -0
  77. data/lib/steep/drivers/watch.rb +194 -0
  78. data/lib/steep/drivers/worker.rb +58 -0
  79. data/lib/steep/equatable.rb +23 -0
  80. data/lib/steep/expectations.rb +228 -0
  81. data/lib/steep/index/rbs_index.rb +350 -0
  82. data/lib/steep/index/signature_symbol_provider.rb +185 -0
  83. data/lib/steep/index/source_index.rb +167 -0
  84. data/lib/steep/interface/block.rb +103 -0
  85. data/lib/steep/interface/builder.rb +843 -0
  86. data/lib/steep/interface/function.rb +1090 -0
  87. data/lib/steep/interface/method_type.rb +330 -0
  88. data/lib/steep/interface/shape.rb +239 -0
  89. data/lib/steep/interface/substitution.rb +159 -0
  90. data/lib/steep/interface/type_param.rb +115 -0
  91. data/lib/steep/located_value.rb +20 -0
  92. data/lib/steep/method_name.rb +42 -0
  93. data/lib/steep/module_helper.rb +24 -0
  94. data/lib/steep/node_helper.rb +273 -0
  95. data/lib/steep/path_helper.rb +30 -0
  96. data/lib/steep/project/dsl.rb +268 -0
  97. data/lib/steep/project/group.rb +31 -0
  98. data/lib/steep/project/options.rb +63 -0
  99. data/lib/steep/project/pattern.rb +59 -0
  100. data/lib/steep/project/target.rb +92 -0
  101. data/lib/steep/project.rb +78 -0
  102. data/lib/steep/rake_task.rb +132 -0
  103. data/lib/steep/range_extension.rb +29 -0
  104. data/lib/steep/server/base_worker.rb +97 -0
  105. data/lib/steep/server/change_buffer.rb +73 -0
  106. data/lib/steep/server/custom_methods.rb +77 -0
  107. data/lib/steep/server/delay_queue.rb +45 -0
  108. data/lib/steep/server/interaction_worker.rb +492 -0
  109. data/lib/steep/server/lsp_formatter.rb +455 -0
  110. data/lib/steep/server/master.rb +922 -0
  111. data/lib/steep/server/target_group_files.rb +205 -0
  112. data/lib/steep/server/type_check_controller.rb +366 -0
  113. data/lib/steep/server/type_check_worker.rb +303 -0
  114. data/lib/steep/server/work_done_progress.rb +64 -0
  115. data/lib/steep/server/worker_process.rb +176 -0
  116. data/lib/steep/services/completion_provider.rb +802 -0
  117. data/lib/steep/services/content_change.rb +61 -0
  118. data/lib/steep/services/file_loader.rb +74 -0
  119. data/lib/steep/services/goto_service.rb +441 -0
  120. data/lib/steep/services/hover_provider/rbs.rb +88 -0
  121. data/lib/steep/services/hover_provider/ruby.rb +221 -0
  122. data/lib/steep/services/hover_provider/singleton_methods.rb +20 -0
  123. data/lib/steep/services/path_assignment.rb +46 -0
  124. data/lib/steep/services/signature_help_provider.rb +202 -0
  125. data/lib/steep/services/signature_service.rb +428 -0
  126. data/lib/steep/services/stats_calculator.rb +68 -0
  127. data/lib/steep/services/type_check_service.rb +394 -0
  128. data/lib/steep/services/type_name_completion.rb +236 -0
  129. data/lib/steep/signature/validator.rb +651 -0
  130. data/lib/steep/source/ignore_ranges.rb +69 -0
  131. data/lib/steep/source.rb +691 -0
  132. data/lib/steep/subtyping/cache.rb +30 -0
  133. data/lib/steep/subtyping/check.rb +1113 -0
  134. data/lib/steep/subtyping/constraints.rb +341 -0
  135. data/lib/steep/subtyping/relation.rb +101 -0
  136. data/lib/steep/subtyping/result.rb +324 -0
  137. data/lib/steep/subtyping/variable_variance.rb +89 -0
  138. data/lib/steep/test.rb +9 -0
  139. data/lib/steep/thread_waiter.rb +43 -0
  140. data/lib/steep/type_construction.rb +5183 -0
  141. data/lib/steep/type_inference/block_params.rb +416 -0
  142. data/lib/steep/type_inference/case_when.rb +303 -0
  143. data/lib/steep/type_inference/constant_env.rb +56 -0
  144. data/lib/steep/type_inference/context.rb +195 -0
  145. data/lib/steep/type_inference/logic_type_interpreter.rb +613 -0
  146. data/lib/steep/type_inference/method_call.rb +193 -0
  147. data/lib/steep/type_inference/method_params.rb +531 -0
  148. data/lib/steep/type_inference/multiple_assignment.rb +194 -0
  149. data/lib/steep/type_inference/send_args.rb +712 -0
  150. data/lib/steep/type_inference/type_env.rb +341 -0
  151. data/lib/steep/type_inference/type_env_builder.rb +138 -0
  152. data/lib/steep/typing.rb +321 -0
  153. data/lib/steep/version.rb +3 -0
  154. data/lib/steep.rb +369 -0
  155. data/manual/annotations.md +181 -0
  156. data/manual/ignore.md +20 -0
  157. data/manual/ruby-diagnostics.md +1879 -0
  158. data/sample/Steepfile +22 -0
  159. data/sample/lib/conference.rb +49 -0
  160. data/sample/lib/length.rb +35 -0
  161. data/sample/sig/conference.rbs +42 -0
  162. data/sample/sig/generics.rbs +15 -0
  163. data/sample/sig/length.rbs +34 -0
  164. data/steep-relaxed.gemspec +56 -0
  165. metadata +340 -0
@@ -0,0 +1,651 @@
1
+ module Steep
2
+ module Signature
3
+ class Validator
4
+ Location = RBS::Location
5
+ Declarations = RBS::AST::Declarations
6
+
7
+ attr_reader :checker
8
+ attr_reader :context
9
+
10
+ def initialize(checker:)
11
+ @checker = checker
12
+ @errors = []
13
+ @context = []
14
+ end
15
+
16
+ def push_context(self_type: latest_context[0], class_type: latest_context[1], instance_type: latest_context[2])
17
+ @context.push([self_type, class_type, instance_type])
18
+ yield
19
+ ensure
20
+ @context.pop
21
+ end
22
+
23
+ def latest_context
24
+ context.last || [nil, nil, nil]
25
+ end
26
+
27
+ def has_error?
28
+ !no_error?
29
+ end
30
+
31
+ def no_error?
32
+ @errors.empty?
33
+ end
34
+
35
+ def each_error(&block)
36
+ if block
37
+ @errors.each(&block)
38
+ else
39
+ enum_for :each_error
40
+ end
41
+ end
42
+
43
+ def env
44
+ checker.factory.env
45
+ end
46
+
47
+ def builder
48
+ checker.factory.definition_builder
49
+ end
50
+
51
+ def type_name_resolver
52
+ @type_name_resolver ||= RBS::Resolver::TypeNameResolver.new(env)
53
+ end
54
+
55
+ def validator
56
+ @validator ||= RBS::Validator.new(env: env, resolver: type_name_resolver)
57
+ end
58
+
59
+ def factory
60
+ checker.factory
61
+ end
62
+
63
+ def validate
64
+ @errors = []
65
+
66
+ validate_decl
67
+ validate_const
68
+ validate_global
69
+ validate_alias
70
+ end
71
+
72
+ def validate_type_application_constraints(type_name, type_params, type_args, location:)
73
+ if type_params.size == type_args.size
74
+ subst = Interface::Substitution.build(
75
+ type_params.map(&:name),
76
+ type_args.map {|type| factory.type(type) }
77
+ )
78
+
79
+ type_params.zip(type_args).each do |param, arg|
80
+ arg or raise
81
+
82
+ if param.upper_bound_type
83
+ upper_bound_type = factory.type(param.upper_bound_type).subst(subst)
84
+ arg_type = factory.type(arg)
85
+
86
+ constraints = Subtyping::Constraints.empty
87
+
88
+ self_type, class_type, instance_type = latest_context
89
+
90
+ checker.check(
91
+ Subtyping::Relation.new(sub_type: arg_type, super_type: upper_bound_type),
92
+ self_type: self_type,
93
+ class_type: class_type,
94
+ instance_type: instance_type,
95
+ constraints: constraints
96
+ ).else do |result|
97
+ @errors << Diagnostic::Signature::UnsatisfiableTypeApplication.new(
98
+ type_name: type_name,
99
+ type_arg: arg_type,
100
+ type_param: Interface::TypeParam.new(
101
+ name: param.name,
102
+ upper_bound: upper_bound_type,
103
+ variance: param.variance,
104
+ unchecked: param.unchecked?,
105
+ default_type: factory.type_opt(param.default_type)
106
+ ),
107
+ result: result,
108
+ location: location
109
+ )
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ def validate_type_application(type)
117
+ name, type_params, type_args =
118
+ case type
119
+ when RBS::Types::ClassInstance
120
+ [
121
+ type.name,
122
+ builder.build_instance(type.name).type_params_decl,
123
+ type.args
124
+ ]
125
+ when RBS::Types::Interface
126
+ [
127
+ type.name,
128
+ builder.build_interface(type.name).type_params_decl,
129
+ type.args
130
+ ]
131
+ when RBS::Types::Alias
132
+ type_name = env.normalize_type_name?(type.name) or return
133
+ entry = env.type_alias_decls.fetch(type_name)
134
+
135
+ [
136
+ type_name,
137
+ entry.decl.type_params,
138
+ type.args
139
+ ]
140
+ end
141
+
142
+ if name && type_params && type_args
143
+ if !type_params.empty? && !type_args.empty?
144
+ validate_type_application_constraints(name, type_params, type_args, location: type.location)
145
+ end
146
+ end
147
+
148
+ type.each_type do |child|
149
+ validate_type_application(child)
150
+ end
151
+ end
152
+
153
+ def validate_type(type)
154
+ Steep.logger.debug { "#{Location.to_string type.location}: Validating #{type}..." }
155
+
156
+ validator.validate_type(type, context: nil)
157
+ validate_type_application(type)
158
+ end
159
+
160
+ def ancestor_to_type(ancestor)
161
+ case ancestor
162
+ when RBS::Definition::Ancestor::Instance
163
+ args = ancestor.args.map {|type| checker.factory.type(type) }
164
+
165
+ case
166
+ when ancestor.name.interface?
167
+ AST::Types::Name::Interface.new(name: ancestor.name, args: args)
168
+ when ancestor.name.class?
169
+ AST::Types::Name::Instance.new(name: ancestor.name, args: args)
170
+ else
171
+ raise "#{ancestor.name}"
172
+ end
173
+ else
174
+ raise "Unexpected ancestor: #{ancestor.inspect}"
175
+ end
176
+ end
177
+
178
+ def mixin_constraints(definition, mixin_ancestors, immediate_self_types:)
179
+ # @type var relations: Array[[Subtyping::Relation[AST::Types::t], RBS::Definition::Ancestor::Instance]]
180
+ relations = []
181
+
182
+ self_type = checker.factory.type(definition.self_type)
183
+ if immediate_self_types && !immediate_self_types.empty?
184
+ # @type var sts: Array[AST::Types::t]
185
+ sts = immediate_self_types.map {|st| ancestor_to_type(st) }
186
+ self_type = AST::Types::Intersection.build(types: sts.push(self_type))
187
+ end
188
+
189
+ mixin_ancestors.each do |ancestor|
190
+ args = ancestor.args.map {|type| checker.factory.type(type) }
191
+ ancestor_ancestors = builder.ancestor_builder.one_instance_ancestors(ancestor.name)
192
+ ancestor_ancestors.self_types or raise
193
+ ancestor_ancestors.params or raise
194
+ self_constraints = ancestor_ancestors.self_types.map do |self_ancestor|
195
+ s = Interface::Substitution.build(ancestor_ancestors.params, args)
196
+ ancestor_to_type(self_ancestor).subst(s)
197
+ end
198
+
199
+ self_constraints.each do |constraint|
200
+ relations << [
201
+ Subtyping::Relation.new(sub_type: self_type, super_type: constraint),
202
+ ancestor
203
+ ]
204
+ end
205
+ end
206
+
207
+ relations
208
+ end
209
+
210
+ def each_method_type(definition)
211
+ type_name = definition.type_name
212
+
213
+ definition.methods.each_value do |method|
214
+ if method.defined_in == type_name
215
+ method.method_types.each do |method_type|
216
+ yield method_type
217
+ end
218
+ end
219
+ end
220
+ end
221
+
222
+ def each_variable_type(definition)
223
+ type_name = definition.type_name
224
+
225
+ definition.instance_variables.each_value do |var|
226
+ if var.declared_in == type_name
227
+ yield var.type
228
+ end
229
+ end
230
+
231
+ definition.class_variables.each_value do |var|
232
+ if var.declared_in == type_name
233
+ yield var.type
234
+ end
235
+ end
236
+ end
237
+
238
+ def validate_definition_type(definition)
239
+ each_method_type(definition) do |method_type|
240
+ upper_bounds = method_type.type_params.each.with_object({}) do |param, hash| #$ Hash[Symbol, AST::Types::t?]
241
+ hash[param.name] = factory.type_opt(param.upper_bound_type)
242
+ end
243
+
244
+ checker.push_variable_bounds(upper_bounds) do
245
+ method_type.each_type do |type|
246
+ validate_type(type)
247
+ end
248
+ end
249
+ end
250
+
251
+ each_variable_type(definition) do |type|
252
+ validate_type(type)
253
+ end
254
+ end
255
+
256
+ def validate_type_params(type_name, type_params)
257
+ if error_type_params = RBS::AST::TypeParam.validate(type_params)
258
+ error_type_params.each do |type_param|
259
+ default_type = type_param.default_type or raise
260
+ @errors << Diagnostic::Signature::TypeParamDefaultReferenceError.new(type_param, location: default_type.location)
261
+ end
262
+ end
263
+
264
+ upper_bounds = type_params.each.with_object({}) do |param, bounds| #$ Hash[Symbol, AST::Types::t?]
265
+ bounds[param.name] = factory.type_opt(param.upper_bound_type)
266
+ end
267
+
268
+ checker.push_variable_bounds(upper_bounds) do
269
+ type_params.each do |type_param|
270
+ param = checker.factory.type_param(type_param)
271
+
272
+ default_type = param.default_type or next
273
+ upper_bound = param.upper_bound or next
274
+
275
+ relation = Subtyping::Relation.new(sub_type: default_type, super_type: upper_bound)
276
+ result = checker.check(relation, self_type: nil, instance_type: nil, class_type: nil, constraints: Subtyping::Constraints.empty)
277
+
278
+ if result.failure?
279
+ @errors << Diagnostic::Signature::UnsatisfiableGenericsDefaultType.new(
280
+ type_param.name,
281
+ result,
282
+ location: (type_param.default_type || raise).location
283
+ )
284
+ end
285
+ end
286
+ end
287
+ end
288
+
289
+ def validate_one_class_decl(name, entry)
290
+ rescue_validation_errors(name) do
291
+ Steep.logger.debug { "Validating class definition `#{name}`..." }
292
+
293
+ class_type = AST::Types::Name::Singleton.new(name: name)
294
+ instance_type = AST::Types::Name::Instance.new(
295
+ name: name,
296
+ args: entry.type_params.map { AST::Types::Any.instance() }
297
+ )
298
+
299
+ Steep.logger.tagged "#{name}" do
300
+ builder.build_instance(name).tap do |definition|
301
+ upper_bounds = definition.type_params_decl.each.with_object({}) do |param, bounds| #$ Hash[Symbol, AST::Types::t?]
302
+ bounds[param.name] = factory.type_opt(param.upper_bound_type)
303
+ end
304
+
305
+ self_type = AST::Types::Name::Instance.new(
306
+ name: name,
307
+ args: entry.type_params.map { AST::Types::Var.new(name: _1.name) }
308
+ )
309
+
310
+ push_context(self_type: self_type, class_type: class_type, instance_type: instance_type) do
311
+ checker.push_variable_bounds(upper_bounds) do
312
+ definition.instance_variables.each do |name, var|
313
+ if parent = var.parent_variable
314
+ var_type = checker.factory.type(var.type)
315
+ parent_type = checker.factory.type(parent.type)
316
+
317
+ relation = Subtyping::Relation.new(sub_type: var_type, super_type: parent_type)
318
+ result1 = checker.check(relation, self_type: nil, instance_type: nil, class_type: nil, constraints: Subtyping::Constraints.empty)
319
+ result2 = checker.check(relation.flip, self_type: nil, instance_type: nil, class_type: nil, constraints: Subtyping::Constraints.empty)
320
+
321
+ unless result1.success? and result2.success?
322
+ @errors << Diagnostic::Signature::InstanceVariableTypeError.new(
323
+ name: name,
324
+ location: var.type.location,
325
+ var_type: var_type,
326
+ parent_type: parent_type
327
+ )
328
+ end
329
+ end
330
+ end
331
+
332
+ ancestors = builder.ancestor_builder.one_instance_ancestors(name)
333
+ mixin_constraints(definition, ancestors.included_modules || raise, immediate_self_types: ancestors.self_types).each do |relation, ancestor|
334
+ checker.check(
335
+ relation,
336
+ self_type: AST::Types::Self.instance,
337
+ instance_type: AST::Types::Instance.instance,
338
+ class_type: AST::Types::Class.instance,
339
+ constraints: Subtyping::Constraints.empty
340
+ ).else do
341
+ raise if ancestor.source.is_a?(Symbol)
342
+
343
+ @errors << Diagnostic::Signature::ModuleSelfTypeError.new(
344
+ name: name,
345
+ location: ancestor.source&.location || raise,
346
+ ancestor: ancestor,
347
+ result: _1
348
+ )
349
+ end
350
+ end
351
+
352
+ ancestors.each_ancestor do |ancestor|
353
+ case ancestor
354
+ when RBS::Definition::Ancestor::Instance
355
+ validate_ancestor_application(name, ancestor)
356
+ end
357
+ end
358
+
359
+ validate_definition_type(definition)
360
+ end
361
+ end
362
+ end
363
+
364
+ builder.build_singleton(name).tap do |definition|
365
+ entry =
366
+ case definition.entry
367
+ when RBS::Environment::ClassEntry, RBS::Environment::ModuleEntry
368
+ definition.entry
369
+ else
370
+ raise
371
+ end
372
+
373
+ push_context(self_type: class_type, class_type: class_type, instance_type: instance_type) do
374
+ definition.instance_variables.each do |name, var|
375
+ if parent = var.parent_variable
376
+ var_type = checker.factory.type(var.type)
377
+ parent_type = checker.factory.type(parent.type)
378
+
379
+ relation = Subtyping::Relation.new(sub_type: var_type, super_type: parent_type)
380
+ result1 = checker.check(
381
+ relation,
382
+ self_type: AST::Types::Self.instance,
383
+ instance_type: AST::Types::Instance.instance,
384
+ class_type: AST::Types::Class.instance,
385
+ constraints: Subtyping::Constraints.empty
386
+ )
387
+ result2 = checker.check(
388
+ relation.flip,
389
+ self_type: AST::Types::Self.instance,
390
+ instance_type: AST::Types::Instance.instance,
391
+ class_type: AST::Types::Class.instance,
392
+ constraints: Subtyping::Constraints.empty
393
+ )
394
+
395
+ unless result1.success? and result2.success?
396
+ @errors << Diagnostic::Signature::InstanceVariableTypeError.new(
397
+ name: name,
398
+ location: var.type.location,
399
+ var_type: var_type,
400
+ parent_type: parent_type
401
+ )
402
+ end
403
+ end
404
+ end
405
+
406
+ definition.class_variables.each do |name, var|
407
+ if var.declared_in == definition.type_name
408
+ if (parent = var.parent_variable) && var.declared_in != parent.declared_in
409
+ class_var = entry.decls.flat_map {|decl| decl.decl.members }.find do |member|
410
+ member.is_a?(RBS::AST::Members::ClassVariable) && member.name == name
411
+ end
412
+
413
+ if class_var
414
+ loc = class_var.location #: RBS::Location[untyped, untyped]?
415
+ @errors << Diagnostic::Signature::ClassVariableDuplicationError.new(
416
+ class_name: definition.type_name,
417
+ other_class_name: parent.declared_in,
418
+ variable_name: name,
419
+ location: loc&.[](:name) || raise
420
+ )
421
+ end
422
+ end
423
+ end
424
+ end
425
+
426
+ ancestors = builder.ancestor_builder.one_singleton_ancestors(name)
427
+ ancestors.extended_modules or raise
428
+ mixin_constraints(definition, ancestors.extended_modules, immediate_self_types: ancestors.self_types).each do |relation, ancestor|
429
+ checker.check(
430
+ relation,
431
+ self_type: AST::Types::Self.instance ,
432
+ instance_type: AST::Types::Instance.instance,
433
+ class_type: AST::Types::Class.instance,
434
+ constraints: Subtyping::Constraints.empty
435
+ ).else do
436
+ raise if ancestor.source.is_a?(Symbol)
437
+
438
+ @errors << Diagnostic::Signature::ModuleSelfTypeError.new(
439
+ name: name,
440
+ location: ancestor.source&.location || raise,
441
+ ancestor: ancestor,
442
+ result: _1
443
+ )
444
+ end
445
+ end
446
+ ancestors.each_ancestor do |ancestor|
447
+ case ancestor
448
+ when RBS::Definition::Ancestor::Instance
449
+ validate_ancestor_application(name, ancestor)
450
+ end
451
+ end
452
+
453
+ validate_definition_type(definition)
454
+ end
455
+ end
456
+
457
+ validate_type_params(name, entry.type_params)
458
+ end
459
+ end
460
+ end
461
+
462
+ def validate_one_class(name)
463
+ entry = env.constant_entry(name)
464
+
465
+ case entry
466
+ when RBS::Environment::ClassEntry, RBS::Environment::ModuleEntry
467
+ validate_one_class_decl(name, entry)
468
+ when RBS::Environment::ClassAliasEntry, RBS::Environment::ModuleAliasEntry
469
+ validate_one_class_alias(name, entry)
470
+ end
471
+ end
472
+
473
+ def validate_ancestor_application(name, ancestor)
474
+ unless ancestor.args.empty?
475
+ definition =
476
+ case
477
+ when ancestor.name.class?
478
+ builder.build_instance(ancestor.name)
479
+ when ancestor.name.interface?
480
+ builder.build_interface(ancestor.name)
481
+ else
482
+ raise
483
+ end
484
+
485
+ location =
486
+ case ancestor.source
487
+ when :super
488
+ primary_decl = env.class_decls.fetch(name).primary.decl
489
+ primary_decl.is_a?(RBS::AST::Declarations::Class) or raise
490
+ if super_class = primary_decl.super_class
491
+ super_class.location
492
+ else
493
+ # Implicit super class (Object): this can be skipped in fact...
494
+ primary_decl.location&.aref(:name)
495
+ end
496
+ else
497
+ ancestor.source&.location
498
+ end
499
+
500
+ validate_type_application_constraints(
501
+ ancestor.name,
502
+ definition.type_params_decl,
503
+ ancestor.args,
504
+ location: location
505
+ )
506
+
507
+ ancestor.args.each do |arg|
508
+ validate_type(arg)
509
+ end
510
+ end
511
+ end
512
+
513
+ def validate_one_interface(name)
514
+ rescue_validation_errors(name) do
515
+ Steep.logger.debug "Validating interface `#{name}`..."
516
+ Steep.logger.tagged "#{name}" do
517
+ definition = builder.build_interface(name)
518
+
519
+ validate_type_params(name, definition.type_params_decl)
520
+
521
+ upper_bounds = definition.type_params_decl.each.with_object({}) do |param, bounds| #$ Hash[Symbol, AST::Types::t?]
522
+ bounds[param.name] = factory.type_opt(param.upper_bound_type)
523
+ end
524
+
525
+ self_type = AST::Types::Name::Interface.new(
526
+ name: name,
527
+ args: definition.type_params.map { AST::Types::Var.new(name: _1) }
528
+ )
529
+
530
+ push_context(self_type: self_type, class_type: nil, instance_type: nil) do
531
+ checker.push_variable_bounds(upper_bounds) do
532
+ validate_definition_type(definition)
533
+
534
+ ancestors = builder.ancestor_builder.one_interface_ancestors(name)
535
+ ancestors.each_ancestor do |ancestor|
536
+ case ancestor
537
+ when RBS::Definition::Ancestor::Instance
538
+ # Interface ancestor cannot be other than Interface
539
+ ancestor.source.is_a?(Symbol) and raise
540
+
541
+ defn = builder.build_interface(ancestor.name)
542
+ validate_type_application_constraints(
543
+ ancestor.name,
544
+ defn.type_params_decl,
545
+ ancestor.args,
546
+ location: ancestor.source&.location || raise
547
+ )
548
+ end
549
+ end
550
+ end
551
+ end
552
+ end
553
+ end
554
+ end
555
+
556
+ def validate_decl
557
+ env.class_decls.each_key do |name|
558
+ validate_one_class(name)
559
+ end
560
+
561
+ env.class_alias_decls.each do |name, entry|
562
+ validate_one_class_alias(name, entry)
563
+ end
564
+
565
+ env.interface_decls.each_key do |name|
566
+ validate_one_interface(name)
567
+ end
568
+ end
569
+
570
+ def validate_const
571
+ env.constant_decls.each do |name, entry|
572
+ validate_one_constant(name, entry)
573
+ end
574
+ end
575
+
576
+ def validate_one_constant(name, entry)
577
+ rescue_validation_errors do
578
+ Steep.logger.debug "Validating constant `#{name}`..."
579
+ builder.ensure_namespace!(name.namespace, location: entry.decl.location)
580
+ validate_type entry.decl.type
581
+ end
582
+ end
583
+
584
+ def validate_global
585
+ env.global_decls.each do |name, entry|
586
+ validate_one_global(name, entry)
587
+ end
588
+ end
589
+
590
+ def validate_one_global(name, entry)
591
+ rescue_validation_errors do
592
+ Steep.logger.debug "Validating global `#{name}`..."
593
+ validate_type entry.decl.type
594
+ end
595
+ end
596
+
597
+ def validate_one_alias(name, entry = env.type_alias_decls.fetch(name))
598
+ *, inner_most_outer_module = entry.outer
599
+ if inner_most_outer_module
600
+ class_type = AST::Types::Name::Singleton.new(name: inner_most_outer_module.name)
601
+ instance_type = AST::Types::Name::Instance.new(
602
+ name: inner_most_outer_module.name,
603
+ args: inner_most_outer_module.type_params.map { AST::Types::Any.instance() },
604
+ )
605
+ end
606
+
607
+ push_context(class_type: class_type, instance_type: instance_type, self_type: nil) do
608
+ rescue_validation_errors(name) do
609
+ Steep.logger.debug "Validating alias `#{name}`..."
610
+
611
+ unless name.namespace.empty?
612
+ outer = name.namespace.to_type_name
613
+ builder.validate_type_name(outer, entry.decl.location&.aref(:name))
614
+ end
615
+
616
+ validate_type_params(name, entry.decl.type_params)
617
+
618
+ upper_bounds = entry.decl.type_params.each.with_object({}) do |param, bounds| #$ Hash[Symbol, AST::Types::t?]
619
+ bounds[param.name] = factory.type_opt(param.upper_bound_type)
620
+ end
621
+
622
+ validator.validate_type_alias(entry: entry) do |type|
623
+ checker.push_variable_bounds(upper_bounds) do
624
+ validate_type(entry.decl.type)
625
+ end
626
+ end
627
+ end
628
+ end
629
+ end
630
+
631
+ def validate_one_class_alias(name, entry)
632
+ rescue_validation_errors(name) do
633
+ Steep.logger.debug "Validating class/module alias `#{name}`..."
634
+ validator.validate_class_alias(entry: entry)
635
+ end
636
+ end
637
+
638
+ def validate_alias
639
+ env.type_alias_decls.each do |name, entry|
640
+ validate_one_alias(name, entry)
641
+ end
642
+ end
643
+
644
+ def rescue_validation_errors(type_name = nil)
645
+ yield
646
+ rescue RBS::BaseError => exn
647
+ @errors << Diagnostic::Signature.from_rbs_error(exn, factory: factory)
648
+ end
649
+ end
650
+ end
651
+ end