Class: Steep::Signature::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/signature/validator.rb

Constant Summary collapse

Location =
RBS::Location
Declarations =
RBS::AST::Declarations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(checker:) ⇒ Validator

Returns a new instance of Validator.



10
11
12
13
14
# File 'lib/steep/signature/validator.rb', line 10

def initialize(checker:)
  @checker = checker
  @errors = []
  @context = []
end

Instance Attribute Details

#checkerObject (readonly)

Returns the value of attribute checker.



7
8
9
# File 'lib/steep/signature/validator.rb', line 7

def checker
  @checker
end

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/steep/signature/validator.rb', line 8

def context
  @context
end

Instance Method Details

#ancestor_to_type(ancestor) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/steep/signature/validator.rb', line 160

def ancestor_to_type(ancestor)
  case ancestor
  when RBS::Definition::Ancestor::Instance
    args = ancestor.args.map {|type| checker.factory.type(type) }

    case
    when ancestor.name.interface?
      AST::Types::Name::Interface.new(name: ancestor.name, args: args)
    when ancestor.name.class?
      AST::Types::Name::Instance.new(name: ancestor.name, args: args)
    else
      raise "#{ancestor.name}"
    end
  else
    raise "Unexpected ancestor: #{ancestor.inspect}"
  end
end

#builderObject



47
48
49
# File 'lib/steep/signature/validator.rb', line 47

def builder
  checker.factory.definition_builder
end

#each_error(&block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/steep/signature/validator.rb', line 35

def each_error(&block)
  if block
    @errors.each(&block)
  else
    enum_for :each_error
  end
end

#each_method_type(definition) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/steep/signature/validator.rb', line 210

def each_method_type(definition)
  type_name = definition.type_name

  definition.methods.each_value do |method|
    if method.defined_in == type_name
      method.method_types.each do |method_type|
        yield method_type
      end
    end
  end
end

#each_variable_type(definition) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/steep/signature/validator.rb', line 222

def each_variable_type(definition)
  type_name = definition.type_name

  definition.instance_variables.each_value do |var|
    if var.declared_in == type_name
      yield var.type
    end
  end

  definition.class_variables.each_value do |var|
    if var.declared_in == type_name
      yield var.type
    end
  end
end

#envObject



43
44
45
# File 'lib/steep/signature/validator.rb', line 43

def env
  checker.factory.env
end

#factoryObject



59
60
61
# File 'lib/steep/signature/validator.rb', line 59

def factory
  checker.factory
end

#has_error?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/steep/signature/validator.rb', line 27

def has_error?
  !no_error?
end

#latest_contextObject



23
24
25
# File 'lib/steep/signature/validator.rb', line 23

def latest_context
  context.last || [nil, nil, nil]
end

#mixin_constraints(definition, mixin_ancestors, immediate_self_types:) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/steep/signature/validator.rb', line 178

def mixin_constraints(definition, mixin_ancestors, immediate_self_types:)
  # @type var relations: Array[[Subtyping::Relation[AST::Types::t], RBS::Definition::Ancestor::Instance]]
  relations = []

  self_type = checker.factory.type(definition.self_type)
  if immediate_self_types && !immediate_self_types.empty?
    # @type var sts: Array[AST::Types::t]
    sts = immediate_self_types.map {|st| ancestor_to_type(st) }
    self_type = AST::Types::Intersection.build(types: sts.push(self_type))
  end

  mixin_ancestors.each do |ancestor|
    args = ancestor.args.map {|type| checker.factory.type(type) }
    ancestor_ancestors = builder.ancestor_builder.one_instance_ancestors(ancestor.name)
    ancestor_ancestors.self_types or raise
    ancestor_ancestors.params or raise
    self_constraints = ancestor_ancestors.self_types.map do |self_ancestor|
      s = Interface::Substitution.build(ancestor_ancestors.params, args)
      ancestor_to_type(self_ancestor).subst(s)
    end

    self_constraints.each do |constraint|
      relations << [
        Subtyping::Relation.new(sub_type: self_type, super_type: constraint),
        ancestor
      ]
    end
  end

  relations
end

#no_error?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/steep/signature/validator.rb', line 31

def no_error?
  @errors.empty?
end

#push_context(self_type: , class_type: , instance_type: ) ⇒ Object



16
17
18
19
20
21
# File 'lib/steep/signature/validator.rb', line 16

def push_context(self_type: latest_context[0], class_type: latest_context[1], instance_type: latest_context[2])
  @context.push([self_type, class_type, instance_type])
  yield
ensure
  @context.pop
end

#rescue_validation_errors(type_name = nil) ⇒ Object



644
645
646
647
648
# File 'lib/steep/signature/validator.rb', line 644

def rescue_validation_errors(type_name = nil)
  yield
rescue RBS::BaseError => exn
  @errors << Diagnostic::Signature.from_rbs_error(exn, factory: factory)
end

#type_name_resolverObject



51
52
53
# File 'lib/steep/signature/validator.rb', line 51

def type_name_resolver
  @type_name_resolver ||= RBS::Resolver::TypeNameResolver.new(env)
end

#validateObject



63
64
65
66
67
68
69
70
# File 'lib/steep/signature/validator.rb', line 63

def validate
  @errors = []

  validate_decl
  validate_const
  validate_global
  validate_alias
end

#validate_aliasObject



638
639
640
641
642
# File 'lib/steep/signature/validator.rb', line 638

def validate_alias
  env.type_alias_decls.each do |name, entry|
    validate_one_alias(name, entry)
  end
end

#validate_ancestor_application(name, ancestor) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/steep/signature/validator.rb', line 473

def validate_ancestor_application(name, ancestor)
  unless ancestor.args.empty?
    definition =
      case
      when ancestor.name.class?
        builder.build_instance(ancestor.name)
      when ancestor.name.interface?
        builder.build_interface(ancestor.name)
      else
        raise
      end

    location =
      case ancestor.source
      when :super
        primary_decl = env.class_decls.fetch(name).primary.decl
        primary_decl.is_a?(RBS::AST::Declarations::Class) or raise
        if super_class = primary_decl.super_class
          super_class.location
        else
          # Implicit super class (Object): this can be skipped in fact...
          primary_decl.location&.aref(:name)
        end
      else
        ancestor.source&.location
      end

    validate_type_application_constraints(
      ancestor.name,
      definition.type_params_decl,
      ancestor.args,
      location: location
    )

    ancestor.args.each do |arg|
      validate_type(arg)
    end
  end
end

#validate_constObject



570
571
572
573
574
# File 'lib/steep/signature/validator.rb', line 570

def validate_const
  env.constant_decls.each do |name, entry|
    validate_one_constant(name, entry)
  end
end

#validate_declObject



556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/steep/signature/validator.rb', line 556

def validate_decl
  env.class_decls.each_key do |name|
    validate_one_class(name)
  end

  env.class_alias_decls.each do |name, entry|
    validate_one_class_alias(name, entry)
  end

  env.interface_decls.each_key do |name|
    validate_one_interface(name)
  end
end

#validate_definition_type(definition) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/steep/signature/validator.rb', line 238

def validate_definition_type(definition)
  each_method_type(definition) do |method_type|
    upper_bounds = method_type.type_params.each.with_object({}) do |param, hash| #$ Hash[Symbol, AST::Types::t?]
      hash[param.name] = factory.type_opt(param.upper_bound_type)
    end

    checker.push_variable_bounds(upper_bounds) do
      method_type.each_type do |type|
        validate_type(type)
      end
    end
  end

  each_variable_type(definition) do |type|
    validate_type(type)
  end
end

#validate_globalObject



584
585
586
587
588
# File 'lib/steep/signature/validator.rb', line 584

def validate_global
  env.global_decls.each do |name, entry|
    validate_one_global(name, entry)
  end
end

#validate_one_alias(name, entry = env.type_alias_decls.fetch(name)) ⇒ Object



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/steep/signature/validator.rb', line 597

def validate_one_alias(name, entry = env.type_alias_decls.fetch(name))
  *, inner_most_outer_module = entry.outer
  if inner_most_outer_module
    class_type = AST::Types::Name::Singleton.new(name: inner_most_outer_module.name)
    instance_type = AST::Types::Name::Instance.new(
      name: inner_most_outer_module.name,
      args: inner_most_outer_module.type_params.map { AST::Types::Any.instance() },
    )
  end

  push_context(class_type: class_type, instance_type: instance_type, self_type: nil) do
    rescue_validation_errors(name) do
      Steep.logger.debug "Validating alias `#{name}`..."

      unless name.namespace.empty?
        outer = name.namespace.to_type_name
        builder.validate_type_name(outer, entry.decl.location&.aref(:name))
      end

      validate_type_params(name, entry.decl.type_params)

      upper_bounds = entry.decl.type_params.each.with_object({}) do |param, bounds| #$ Hash[Symbol, AST::Types::t?]
        bounds[param.name] = factory.type_opt(param.upper_bound_type)
      end

      validator.validate_type_alias(entry: entry) do |type|
        checker.push_variable_bounds(upper_bounds) do
          validate_type(entry.decl.type)
        end
      end
    end
  end
end

#validate_one_class(name) ⇒ Object



462
463
464
465
466
467
468
469
470
471
# File 'lib/steep/signature/validator.rb', line 462

def validate_one_class(name)
  entry = env.constant_entry(name)

  case entry
  when RBS::Environment::ClassEntry, RBS::Environment::ModuleEntry
    validate_one_class_decl(name, entry)
  when RBS::Environment::ClassAliasEntry, RBS::Environment::ModuleAliasEntry
    validate_one_class_alias(name, entry)
  end
end

#validate_one_class_alias(name, entry) ⇒ Object



631
632
633
634
635
636
# File 'lib/steep/signature/validator.rb', line 631

def validate_one_class_alias(name, entry)
  rescue_validation_errors(name) do
    Steep.logger.debug "Validating class/module alias `#{name}`..."
    validator.validate_class_alias(entry: entry)
  end
end

#validate_one_class_decl(name, entry) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/steep/signature/validator.rb', line 289

def validate_one_class_decl(name, entry)
  rescue_validation_errors(name) do
    Steep.logger.debug { "Validating class definition `#{name}`..." }

    class_type = AST::Types::Name::Singleton.new(name: name)
    instance_type = AST::Types::Name::Instance.new(
      name: name,
      args: entry.type_params.map { AST::Types::Any.instance() }
    )

    Steep.logger.tagged "#{name}" do
      builder.build_instance(name).tap do |definition|
        upper_bounds = definition.type_params_decl.each.with_object({}) do |param, bounds| #$ Hash[Symbol, AST::Types::t?]
          bounds[param.name] = factory.type_opt(param.upper_bound_type)
        end

        self_type = AST::Types::Name::Instance.new(
          name: name,
          args: entry.type_params.map { AST::Types::Var.new(name: _1.name) }
        )

        push_context(self_type: self_type, class_type: class_type, instance_type: instance_type) do
          checker.push_variable_bounds(upper_bounds) do
            definition.instance_variables.each do |name, var|
              if parent = var.parent_variable
                var_type = checker.factory.type(var.type)
                parent_type = checker.factory.type(parent.type)

                relation = Subtyping::Relation.new(sub_type: var_type, super_type: parent_type)
                result1 = checker.check(relation, self_type: nil, instance_type: nil, class_type: nil, constraints: Subtyping::Constraints.empty)
                result2 = checker.check(relation.flip, self_type: nil, instance_type: nil, class_type: nil, constraints: Subtyping::Constraints.empty)

                unless result1.success? and result2.success?
                  @errors << Diagnostic::Signature::InstanceVariableTypeError.new(
                    name: name,
                    location: var.type.location,
                    var_type: var_type,
                    parent_type: parent_type
                  )
                end
              end
            end

            ancestors = builder.ancestor_builder.one_instance_ancestors(name)
            mixin_constraints(definition, ancestors.included_modules || raise, immediate_self_types: ancestors.self_types).each do |relation, ancestor|
              checker.check(
                relation,
                self_type: AST::Types::Self.instance,
                instance_type: AST::Types::Instance.instance,
                class_type: AST::Types::Class.instance,
                constraints: Subtyping::Constraints.empty
              ).else do
                raise if ancestor.source.is_a?(Symbol)

                @errors << Diagnostic::Signature::ModuleSelfTypeError.new(
                  name: name,
                  location: ancestor.source&.location || raise,
                  ancestor: ancestor,
                  result: _1
                )
              end
            end

            ancestors.each_ancestor do |ancestor|
              case ancestor
              when RBS::Definition::Ancestor::Instance
                validate_ancestor_application(name, ancestor)
              end
            end

            validate_definition_type(definition)
          end
        end
      end

      builder.build_singleton(name).tap do |definition|
        entry =
          case definition.entry
          when RBS::Environment::ClassEntry, RBS::Environment::ModuleEntry
            definition.entry
          else
            raise
          end

        push_context(self_type: class_type, class_type: class_type, instance_type: instance_type) do
          definition.instance_variables.each do |name, var|
            if parent = var.parent_variable
              var_type = checker.factory.type(var.type)
              parent_type = checker.factory.type(parent.type)

              relation = Subtyping::Relation.new(sub_type: var_type, super_type: parent_type)
              result1 = checker.check(
                relation,
                self_type: AST::Types::Self.instance,
                instance_type: AST::Types::Instance.instance,
                class_type: AST::Types::Class.instance,
                constraints: Subtyping::Constraints.empty
              )
              result2 = checker.check(
                relation.flip,
                self_type: AST::Types::Self.instance,
                instance_type: AST::Types::Instance.instance,
                class_type: AST::Types::Class.instance,
                constraints: Subtyping::Constraints.empty
              )

              unless result1.success? and result2.success?
                @errors << Diagnostic::Signature::InstanceVariableTypeError.new(
                  name: name,
                  location: var.type.location,
                  var_type: var_type,
                  parent_type: parent_type
                )
              end
            end
          end

          definition.class_variables.each do |name, var|
            if var.declared_in == definition.type_name
              if (parent = var.parent_variable) && var.declared_in != parent.declared_in
                class_var = entry.decls.flat_map {|decl| decl.decl.members }.find do |member|
                  member.is_a?(RBS::AST::Members::ClassVariable) && member.name == name
                end

                if class_var
                  loc = class_var.location #: RBS::Location[untyped, untyped]?
                  @errors << Diagnostic::Signature::ClassVariableDuplicationError.new(
                    class_name: definition.type_name,
                    other_class_name: parent.declared_in,
                    variable_name: name,
                    location: loc&.[](:name) || raise
                  )
                end
              end
            end
          end

          ancestors = builder.ancestor_builder.one_singleton_ancestors(name)
          ancestors.extended_modules or raise
          mixin_constraints(definition, ancestors.extended_modules, immediate_self_types: ancestors.self_types).each do |relation, ancestor|
            checker.check(
              relation,
              self_type: AST::Types::Self.instance ,
              instance_type: AST::Types::Instance.instance,
              class_type: AST::Types::Class.instance,
              constraints: Subtyping::Constraints.empty
            ).else do
              raise if ancestor.source.is_a?(Symbol)

              @errors << Diagnostic::Signature::ModuleSelfTypeError.new(
                name: name,
                location: ancestor.source&.location || raise,
                ancestor: ancestor,
                result: _1
              )
            end
          end
          ancestors.each_ancestor do |ancestor|
            case ancestor
            when RBS::Definition::Ancestor::Instance
              validate_ancestor_application(name, ancestor)
            end
          end

          validate_definition_type(definition)
        end
      end

      validate_type_params(name, entry.type_params)
    end
  end
end

#validate_one_constant(name, entry) ⇒ Object



576
577
578
579
580
581
582
# File 'lib/steep/signature/validator.rb', line 576

def validate_one_constant(name, entry)
  rescue_validation_errors do
    Steep.logger.debug "Validating constant `#{name}`..."
    builder.ensure_namespace!(name.namespace, location: entry.decl.location)
    validate_type entry.decl.type
  end
end

#validate_one_global(name, entry) ⇒ Object



590
591
592
593
594
595
# File 'lib/steep/signature/validator.rb', line 590

def validate_one_global(name, entry)
  rescue_validation_errors do
    Steep.logger.debug "Validating global `#{name}`..."
    validate_type entry.decl.type
  end
end

#validate_one_interface(name) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/steep/signature/validator.rb', line 513

def validate_one_interface(name)
  rescue_validation_errors(name) do
    Steep.logger.debug "Validating interface `#{name}`..."
    Steep.logger.tagged "#{name}" do
      definition = builder.build_interface(name)

      validate_type_params(name, definition.type_params_decl)

      upper_bounds = definition.type_params_decl.each.with_object({}) do |param, bounds| #$ Hash[Symbol, AST::Types::t?]
        bounds[param.name] = factory.type_opt(param.upper_bound_type)
      end

      self_type = AST::Types::Name::Interface.new(
        name: name,
        args: definition.type_params.map { AST::Types::Var.new(name: _1) }
      )

      push_context(self_type: self_type, class_type: nil, instance_type: nil) do
        checker.push_variable_bounds(upper_bounds) do
          validate_definition_type(definition)

          ancestors = builder.ancestor_builder.one_interface_ancestors(name)
          ancestors.each_ancestor do |ancestor|
            case ancestor
            when RBS::Definition::Ancestor::Instance
              # Interface ancestor cannot be other than Interface
              ancestor.source.is_a?(Symbol) and raise

              defn = builder.build_interface(ancestor.name)
              validate_type_application_constraints(
                ancestor.name,
                defn.type_params_decl,
                ancestor.args,
                location: ancestor.source&.location || raise
              )
            end
          end
        end
      end
    end
  end
end

#validate_type(type) ⇒ Object



153
154
155
156
157
158
# File 'lib/steep/signature/validator.rb', line 153

def validate_type(type)
  Steep.logger.debug { "#{Location.to_string type.location}: Validating #{type}..." }

  validator.validate_type(type, context: nil)
  validate_type_application(type)
end

#validate_type_application(type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/steep/signature/validator.rb', line 116

def validate_type_application(type)
  name, type_params, type_args =
    case type
    when RBS::Types::ClassInstance
      [
        type.name,
        builder.build_instance(type.name).type_params_decl,
        type.args
      ]
    when RBS::Types::Interface
      [
        type.name,
        builder.build_interface(type.name).type_params_decl,
        type.args
      ]
    when RBS::Types::Alias
      type_name = env.normalize_type_name?(type.name) or return
      entry = env.type_alias_decls.fetch(type_name)

      [
        type_name,
        entry.decl.type_params,
        type.args
      ]
    end

  if name && type_params && type_args
    if !type_params.empty? && !type_args.empty?
      validate_type_application_constraints(name, type_params, type_args, location: type.location)
    end
  end

  type.each_type do |child|
    validate_type_application(child)
  end
end

#validate_type_application_constraints(type_name, type_params, type_args, location:) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/steep/signature/validator.rb', line 72

def validate_type_application_constraints(type_name, type_params, type_args, location:)
  if type_params.size == type_args.size
    subst = Interface::Substitution.build(
      type_params.map(&:name),
      type_args.map {|type| factory.type(type) }
    )

    type_params.zip(type_args).each do |param, arg|
      arg or raise

      if param.upper_bound_type
        upper_bound_type = factory.type(param.upper_bound_type).subst(subst)
        arg_type = factory.type(arg)

        constraints = Subtyping::Constraints.empty

        self_type, class_type, instance_type = latest_context

        checker.check(
          Subtyping::Relation.new(sub_type: arg_type, super_type: upper_bound_type),
          self_type: self_type,
          class_type: class_type,
          instance_type: instance_type,
          constraints: constraints
        ).else do |result|
          @errors << Diagnostic::Signature::UnsatisfiableTypeApplication.new(
            type_name: type_name,
            type_arg: arg_type,
            type_param: Interface::TypeParam.new(
              name: param.name,
              upper_bound: upper_bound_type,
              variance: param.variance,
              unchecked: param.unchecked?,
              default_type: factory.type_opt(param.default_type)
            ),
            result: result,
            location: location
          )
        end
      end
    end
  end
end

#validate_type_params(type_name, type_params) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/steep/signature/validator.rb', line 256

def validate_type_params(type_name, type_params)
  if error_type_params = RBS::AST::TypeParam.validate(type_params)
    error_type_params.each do |type_param|
      default_type = type_param.default_type or raise
      @errors << Diagnostic::Signature::TypeParamDefaultReferenceError.new(type_param, location: default_type.location)
    end
  end

  upper_bounds = type_params.each.with_object({}) do |param, bounds| #$ Hash[Symbol, AST::Types::t?]
    bounds[param.name] = factory.type_opt(param.upper_bound_type)
  end

  checker.push_variable_bounds(upper_bounds) do
    type_params.each do |type_param|
      param = checker.factory.type_param(type_param)

      default_type = param.default_type or next
      upper_bound = param.upper_bound or next

      relation = Subtyping::Relation.new(sub_type: default_type, super_type: upper_bound)
      result = checker.check(relation, self_type: nil, instance_type: nil, class_type: nil, constraints: Subtyping::Constraints.empty)

      if result.failure?
        @errors << Diagnostic::Signature::UnsatisfiableGenericsDefaultType.new(
          type_param.name,
          result,
          location: (type_param.default_type || raise).location
        )
      end
    end
  end
end

#validatorObject



55
56
57
# File 'lib/steep/signature/validator.rb', line 55

def validator
  @validator ||= RBS::Validator.new(env: env, resolver: type_name_resolver)
end