Class: RBS::Validator
- Inherits:
-
Object
- Object
- RBS::Validator
- Defined in:
- lib/rbs/validator.rb
Instance Attribute Summary collapse
-
#definition_builder ⇒ Object
readonly
Returns the value of attribute definition_builder.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
Instance Method Summary collapse
- #absolute_type(type, context:) ⇒ Object
-
#initialize(env:, resolver:) ⇒ Validator
constructor
A new instance of Validator.
- #type_alias_dependency ⇒ Object
- #type_alias_regularity ⇒ Object
- #validate_class_alias(entry:) ⇒ Object
- #validate_method_definition(method_def, type_name:) ⇒ Object
-
#validate_type(type, context:) ⇒ Object
Validates presence of the relative type, and application arity match.
- #validate_type_alias(entry:) ⇒ Object
- #validate_type_params(params, type_name:, method_name: nil, location:) ⇒ Object
Constructor Details
#initialize(env:, resolver:) ⇒ Validator
Returns a new instance of Validator.
9 10 11 12 13 |
# File 'lib/rbs/validator.rb', line 9 def initialize(env:, resolver:) @env = env @resolver = resolver @definition_builder = DefinitionBuilder.new(env: env) end |
Instance Attribute Details
#definition_builder ⇒ Object (readonly)
Returns the value of attribute definition_builder.
7 8 9 |
# File 'lib/rbs/validator.rb', line 7 def definition_builder @definition_builder end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
5 6 7 |
# File 'lib/rbs/validator.rb', line 5 def env @env end |
#resolver ⇒ Object (readonly)
Returns the value of attribute resolver.
6 7 8 |
# File 'lib/rbs/validator.rb', line 6 def resolver @resolver end |
Instance Method Details
#absolute_type(type, context:) ⇒ Object
15 16 17 18 19 |
# File 'lib/rbs/validator.rb', line 15 def absolute_type(type, context:) type.map_type_name do |type_name, _, type| resolver.resolve(type_name, context: context) || yield(type) end end |
#type_alias_dependency ⇒ Object
171 172 173 |
# File 'lib/rbs/validator.rb', line 171 def type_alias_dependency @type_alias_dependency ||= TypeAliasDependency.new(env: env) end |
#type_alias_regularity ⇒ Object
175 176 177 |
# File 'lib/rbs/validator.rb', line 175 def type_alias_regularity @type_alias_regularity ||= TypeAliasRegularity.validate(env: env) end |
#validate_class_alias(entry:) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rbs/validator.rb', line 151 def validate_class_alias(entry:) case env.normalize_module_name?(entry.decl.new_name) when nil raise NoTypeFoundError.new(type_name: entry.decl.old_name, location: entry.decl.location&.[](:old_name)) when false raise CyclicClassAliasDefinitionError.new(entry) end case entry when Environment::ClassAliasEntry unless env.class_entry(entry.decl.old_name) raise InconsistentClassModuleAliasError.new(entry) end when Environment::ModuleAliasEntry unless env.module_entry(entry.decl.old_name) raise InconsistentClassModuleAliasError.new(entry) end end end |
#validate_method_definition(method_def, type_name:) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rbs/validator.rb', line 101 def validate_method_definition(method_def, type_name:) method_def.overloads.each do |overload| method_type = overload.method_type unless method_type.type_params.empty? loc = method_type.location&.aref(:type_params) validate_type_params( method_type.type_params, type_name: type_name, method_name: method_def.name, location: loc ) end end end |
#validate_type(type, context:) ⇒ Object
Validates presence of the relative type, and application arity match.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rbs/validator.rb', line 22 def validate_type(type, context:) case type when Types::ClassInstance, Types::Interface, Types::Alias # @type var type: Types::ClassInstance | Types::Interface | Types::Alias if type.name.namespace.relative? type = _ = absolute_type(type, context: context) do |_| NoTypeFoundError.check!(type.name.absolute!, env: env, location: type.location) end end definition_builder.validate_type_name(type.name, type.location) type_params = case type when Types::ClassInstance entry = env.normalized_module_class_entry(type.name) or raise entry.type_params when Types::Interface env.interface_decls[type.name].decl.type_params when Types::Alias env.type_alias_decls[type.name].decl.type_params end InvalidTypeApplicationError.check!( type_name: type.name, args: type.args, params: type_params.each.map(&:name), location: type.location ) when Types::ClassSingleton definition_builder.validate_type_presence(type) end type.each_type do |type| validate_type(type, context: context) end end |
#validate_type_alias(entry:) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 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 |
# File 'lib/rbs/validator.rb', line 60 def validate_type_alias(entry:) type_name = entry.decl.name if type_alias_dependency.circular_definition?(type_name) location = entry.decl.location or raise raise RecursiveTypeAliasError.new(alias_names: [type_name], location: location) end if diagnostic = type_alias_regularity.nonregular?(type_name) location = entry.decl.location or raise raise NonregularTypeAliasError.new(diagnostic: diagnostic, location: location) end unless entry.decl.type_params.empty? calculator = VarianceCalculator.new(builder: definition_builder) result = calculator.in_type_alias(name: type_name) if set = result.incompatible?(entry.decl.type_params) set.each do |param_name| param = entry.decl.type_params.find {|param| param.name == param_name } or raise next if param.unchecked? raise InvalidVarianceAnnotationError.new( type_name: type_name, param: param, location: entry.decl.type.location ) end end validate_type_params( entry.decl.type_params, type_name: type_name, location: entry.decl.location&.aref(:type_params) ) end if block_given? yield entry.decl.type end end |
#validate_type_params(params, type_name:, method_name: nil, location:) ⇒ Object
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 |
# File 'lib/rbs/validator.rb', line 117 def validate_type_params(params, type_name: , method_name: nil, location:) # @type var each_node: TSort::_EachNode[Symbol] each_node = __skip__ = -> (&block) do params.each do |param| block[param.name] end end # @type var each_child: TSort::_EachChild[Symbol] each_child = __skip__ = -> (name, &block) do if param = params.find {|p| p.name == name } if b = param.upper_bound b.free_variables.each do |tv| block[tv] end end end end TSort.each_strongly_connected_component(each_node, each_child) do |names| if names.size > 1 params = names.map do |name| params.find {|param| param.name == name} or raise end raise CyclicTypeParameterBound.new( type_name: type_name, method_name: method_name, params: params, location: location ) end end end |