Class: RBS::TypeAliasRegularity
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Inherits: | Object |
Defined in: | lib/rbs/type_alias_regularity.rb |
Class Method Summary
- .new(env:) ⇒ TypeAliasRegularity constructor
- .validate(env:)
Instance Attribute Summary
- #builder readonly
- #diagnostics readonly
- #env readonly
Instance Method Summary
Constructor Details
.new(env:) ⇒ TypeAliasRegularity
# File 'lib/rbs/type_alias_regularity.rb', line 16
def initialize(env:) @env = env @builder = DefinitionBuilder.new(env: env) @diagnostics = {} end
Class Method Details
.validate(env:)
[ GitHub ]Instance Attribute Details
#builder (readonly)
[ GitHub ]# File 'lib/rbs/type_alias_regularity.rb', line 14
attr_reader :env, :builder, :diagnostics
#diagnostics (readonly)
[ GitHub ]#env (readonly)
[ GitHub ]# File 'lib/rbs/type_alias_regularity.rb', line 14
attr_reader :env, :builder, :diagnostics
Instance Method Details
#build_alias_type(name)
[ GitHub ]# File 'lib/rbs/type_alias_regularity.rb', line 61
def build_alias_type(name) entry = env.type_alias_decls[name] or return unless entry.decl.type_params.empty? as = entry.decl.type_params.each.map {|param| Types::Variable.new(name: param.name, location: nil) } Types::Alias.new(name: name, args: as, location: nil) end end
#compatible_args?(args1, args2) ⇒ Boolean
#each_alias_type(type, &block)
[ GitHub ]#each_mutual_alias_defs(&block)
[ GitHub ]# File 'lib/rbs/type_alias_regularity.rb', line 83
def each_mutual_alias_defs(&block) # @type var each_node: ^() { (TypeName) -> void } -> void each_node = -> (&block) do env.type_alias_decls.each_value do |decl| if normalized = env.normalize_type_name?(decl.name) block[normalized] end end end # @type var each_child: ^(TypeName) { (TypeName) -> void } -> void each_child = -> (name, &block) do if env.type_alias_decls.key?(name) type = builder. (name) each_alias_type(type) do |ty| if normalized = env.normalize_type_name?(ty.name) block[normalized] end end end end TSort.each_strongly_connected_component(each_node, each_child) do |names| yield Set.new(names) end end
#nonregular?(type_name) ⇒ Boolean
# File 'lib/rbs/type_alias_regularity.rb', line 79
def nonregular?(type_name) diagnostics[env.normalize_type_name!(type_name)] end
#validate
[ GitHub ]# File 'lib/rbs/type_alias_regularity.rb', line 22
def validate diagnostics.clear each_mutual_alias_defs do |names| # Find the first generic type alias in strongly connected component. # This is to skip the regularity check when the alias is not generic. names.each do |name| # @type break: nil if type = build_alias_type(name) # Running validation only once from the first generic type is enough, because they are mutual recursive definition. validate_alias_type(type, names, {}) break end end end end
#validate_alias_type(alias_type, names, types)
[ GitHub ]# File 'lib/rbs/type_alias_regularity.rb', line 39
def validate_alias_type(alias_type, names, types) alias_name = env.normalize_type_name?(alias_type.name) or return if names.include?(alias_name) if ex_type = types[alias_name] unless compatible_args?(ex_type.args, alias_type.args) diagnostics[alias_name] ||= Diagnostic.new(type_name: alias_type.name, nonregular_type: alias_type) end return else types[alias_type.name] = alias_type end = builder. (alias_name, alias_type.args) each_alias_type( ) do |at| validate_alias_type(at, names, types) end end end