lib/rbs/errors.rb in rbs-3.0.0.dev.1 vs lib/rbs/errors.rb in rbs-3.0.0.dev.2

- old
+ new

@@ -117,23 +117,11 @@ super "#{Location.to_string location}: Could not find #{type_name}" end def self.check!(type_name, env:, location:) - dic = case - when type_name.class? - env.class_decls - when type_name.alias? - env.alias_decls - when type_name.interface? - env.interface_decls - else - raise - end - - dic.key?(type_name) or raise new(type_name: type_name, location: location) - + env.type_name?(type_name) or raise new(type_name: type_name, location: location) type_name end end class NoSuperclassFoundError < DefinitionError @@ -146,11 +134,11 @@ super "#{Location.to_string location}: Could not find super class: #{type_name}" end def self.check!(type_name, env:, location:) - if decl = env.class_decls[type_name] + if env.module_name?(type_name) return end raise new(type_name: type_name, location: location) end @@ -164,11 +152,11 @@ super "#{Location.to_string(super_decl.location)}: Cannot inherit a module: #{super_decl.name}" end def self.check!(super_decl, env:) - return if env.class_decls[super_decl.name].is_a?(Environment::ClassEntry) + return if env.class_decl?(super_decl.name) || env.class_alias?(super_decl.name) raise new(super_decl) end end @@ -184,20 +172,11 @@ end def self.check!(self_type, env:) type_name = self_type.name - dic = case - when type_name.class? - env.class_decls - when type_name.interface? - env.interface_decls - else - raise - end - - dic.key?(type_name) or raise new(type_name: type_name, location: self_type.location) + (env.module_name?(type_name) || env.interface_name?(type_name)) or raise new(type_name: type_name, location: self_type.location) end end class NoMixinFoundError < DefinitionError attr_reader :type_name @@ -213,20 +192,11 @@ def location member.location end def self.check!(type_name, env:, member:) - dic = case - when type_name.class? - env.class_decls - when type_name.interface? - env.interface_decls - else - raise - end - - dic.key?(type_name) or raise new(type_name: type_name, member: member) + (env.module_name?(type_name) || env.interface_name?(type_name)) or raise new(type_name: type_name, member: member) end end class DuplicatedMethodDefinitionError < DefinitionError attr_reader :type @@ -414,12 +384,11 @@ def location member.location end def self.check!(type_name:, env:, member:) - case env.class_decls[member.name] - when Environment::ClassEntry + if env.class_decl?(member.name) raise new(type_name: type_name, member: member) end end private @@ -474,8 +443,36 @@ @method_name = method_name @params = params @location = location super "#{Location.to_string(location)}: Cyclic type parameter bound is prohibited" + end + end + + class InconsistentClassModuleAliasError < BaseError + attr_reader :alias_entry + + def initialize(entry) + @alias_entry = entry + + expected_kind, actual_kind = + case entry + when Environment::ModuleAliasEntry + ["module", "class"] + when Environment::ClassAliasEntry + ["class", "module"] + end + + super "#{Location.to_string(entry.decl.location&.[](:old_name))}: A #{expected_kind} `#{entry.decl.new_name}` cannot be an alias of a #{actual_kind} `#{entry.decl.old_name}`" + end + end + + class CyclicClassAliasDefinitionError < BaseError + attr_reader :alias_entry + + def initialize(entry) + @alias_entry = entry + + super "#{Location.to_string(entry.decl.location&.[](:old_name))}: A #{alias_entry.decl.new_name} is a cyclic definition" end end end