Class: RBS::TypeAliasRegularity
- Inherits:
-
Object
- Object
- RBS::TypeAliasRegularity
- Defined in:
- lib/rbs/type_alias_regularity.rb
Defined Under Namespace
Classes: Diagnostic
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#diagnostics ⇒ Object
readonly
Returns the value of attribute diagnostics.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Class Method Summary collapse
Instance Method Summary collapse
- #build_alias_type(name) ⇒ Object
- #compatible_args?(args1, args2) ⇒ Boolean
- #each_alias_type(type, &block) ⇒ Object
- #each_mutual_alias_defs(&block) ⇒ Object
-
#initialize(env:) ⇒ TypeAliasRegularity
constructor
A new instance of TypeAliasRegularity.
- #nonregular?(type_name) ⇒ Boolean
- #validate ⇒ Object
- #validate_alias_type(alias_type, names, types) ⇒ Object
Constructor Details
#initialize(env:) ⇒ TypeAliasRegularity
Returns a new instance of TypeAliasRegularity.
14 15 16 17 18 |
# File 'lib/rbs/type_alias_regularity.rb', line 14 def initialize(env:) @env = env @builder = DefinitionBuilder.new(env: env) @diagnostics = {} end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
12 13 14 |
# File 'lib/rbs/type_alias_regularity.rb', line 12 def builder @builder end |
#diagnostics ⇒ Object (readonly)
Returns the value of attribute diagnostics.
12 13 14 |
# File 'lib/rbs/type_alias_regularity.rb', line 12 def diagnostics @diagnostics end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
12 13 14 |
# File 'lib/rbs/type_alias_regularity.rb', line 12 def env @env end |
Class Method Details
.validate(env:) ⇒ Object
109 110 111 112 113 |
# File 'lib/rbs/type_alias_regularity.rb', line 109 def self.validate(env:) self.new(env: env).tap do |validator| validator.validate() end end |
Instance Method Details
#build_alias_type(name) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rbs/type_alias_regularity.rb', line 57 def build_alias_type(name) entry = env.alias_decls[name] or raise "Unknown alias name: #{name}" 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
65 66 67 68 69 70 71 72 73 |
# File 'lib/rbs/type_alias_regularity.rb', line 65 def compatible_args?(args1, args2) if args1.size == args2.size args1.zip(args2).all? do |t1, t2| t1.is_a?(Types::Bases::Any) || t2.is_a?(Types::Bases::Any) || t1 == t2 end end end |
#each_alias_type(type, &block) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/rbs/type_alias_regularity.rb', line 99 def each_alias_type(type, &block) if type.is_a?(RBS::Types::Alias) yield type end type.each_type do |ty| each_alias_type(ty, &block) end end |
#each_mutual_alias_defs(&block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rbs/type_alias_regularity.rb', line 79 def each_mutual_alias_defs(&block) # @type var each_node: TSort::_EachNode[TypeName] each_node = __skip__ = -> (&block) do env.alias_decls.each_value do |decl| block[decl.name] end end # @type var each_child: TSort::_EachChild[TypeName] each_child = __skip__ = -> (name, &block) do type = builder.(name) each_alias_type(type) do |ty| block[ty.name] end end TSort.each_strongly_connected_component(each_node, each_child) do |names| yield Set.new(names) end end |
#nonregular?(type_name) ⇒ Boolean
75 76 77 |
# File 'lib/rbs/type_alias_regularity.rb', line 75 def nonregular?(type_name) diagnostics[type_name] end |
#validate ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rbs/type_alias_regularity.rb', line 20 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) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rbs/type_alias_regularity.rb', line 37 def validate_alias_type(alias_type, names, types) if names.include?(alias_type.name) if ex_type = types[alias_type.name] unless compatible_args?(ex_type.args, alias_type.args) diagnostics[alias_type.name] ||= Diagnostic.new(type_name: alias_type.name, nonregular_type: alias_type) end return else types[alias_type.name] = alias_type end = builder.(alias_type.name, alias_type.args) each_alias_type() do |at| validate_alias_type(at, names, types) end end end |