lib/rbs/errors.rb in rbs-0.20.1 vs lib/rbs/errors.rb in rbs-1.0.0.pre
- old
+ new
@@ -66,22 +66,24 @@
class RecursiveAncestorError < StandardError
attr_reader :ancestors
attr_reader :location
def initialize(ancestors:, location:)
- last = case last = ancestors.last
- when Definition::Ancestor::Singleton
- "singleton(#{last.name})"
- when Definition::Ancestor::Instance
- if last.args.empty?
- last.name.to_s
- else
- "#{last.name}[#{last.args.join(", ")}]"
- end
- end
+ names = ancestors.map do |ancestor|
+ case ancestor
+ when Definition::Ancestor::Singleton
+ "singleton(#{ancestor.name})"
+ when Definition::Ancestor::Instance
+ if ancestor.args.empty?
+ ancestor.name.to_s
+ else
+ "#{ancestor.name}[#{ancestor.args.join(", ")}]"
+ end
+ end
+ end
- super "#{Location.to_string location}: Detected recursive ancestors: #{last}"
+ super "#{Location.to_string location}: Detected recursive ancestors: #{names.join(" < ")}"
end
def self.check!(self_ancestor, ancestors:, location:)
case self_ancestor
when Definition::Ancestor::Instance
@@ -192,49 +194,59 @@
dic.key?(type_name) or raise new(type_name: type_name, member: member)
end
end
class DuplicatedMethodDefinitionError < StandardError
- attr_reader :decl
- attr_reader :location
+ attr_reader :type
+ attr_reader :method_name
+ attr_reader :members
- def initialize(decl:, name:, location:)
- decl_str = case decl
- when AST::Declarations::Interface, AST::Declarations::Class, AST::Declarations::Module
- decl.name.to_s
- when AST::Declarations::Extension
- "#{decl.name} (#{decl.extension_name})"
- end
+ def initialize(type:, method_name:, members:)
+ @type = type
+ @method_name = method_name
+ @members = members
- super "#{Location.to_string location}: #{decl_str} has duplicated method definition: #{name}"
+ super "#{Location.to_string location}: #{qualified_method_name} has duplicated definitions"
end
- def self.check!(decl:, methods:, name:, location:)
- if methods.key?(name)
- raise new(decl: decl, name: name, location: location)
+ def qualified_method_name
+ case type
+ when Types::ClassSingleton
+ "#{type.name}.#{method_name}"
+ else
+ "#{type.name}##{method_name}"
end
end
+
+ def location
+ members[0].location
+ end
end
- class MethodDefinitionConflictWithInterfaceMixinError < StandardError
+ class DuplicatedInterfaceMethodDefinitionError < StandardError
include MethodNameHelper
- attr_reader :type_name
+ attr_reader :type
attr_reader :method_name
- attr_reader :kind
- attr_reader :mixin_member
- attr_reader :entries
+ attr_reader :member
- def initialize(type_name:, method_name:, kind:, mixin_member:, entries:)
- @type_name = type_name
+ def initialize(type:, method_name:, member:)
+ @type = type
@method_name = method_name
- @kind = kind
- @mixin_member = mixin_member
- @entries = entries
+ @member = member
- super "#{entries[0].decl.location}: Duplicated method with interface mixin: #{method_name_string}"
+ super "#{member.location}: Duplicated method definition: #{qualified_method_name}"
end
+
+ def qualified_method_name
+ case type
+ when Types::ClassSingleton
+ "#{type.name}.#{method_name}"
+ else
+ "#{type.name}##{method_name}"
+ end
+ end
end
class UnknownMethodAliasError < StandardError
attr_reader :original_name
attr_reader :aliased_name
@@ -245,16 +257,10 @@
@aliased_name = aliased_name
@location = location
super "#{Location.to_string location}: Unknown method alias name: #{original_name} => #{aliased_name}"
end
-
- def self.check!(methods:, original_name:, aliased_name:, location:)
- unless methods.key?(original_name)
- raise new(original_name: original_name, aliased_name: aliased_name, location: location)
- end
- end
end
class SuperclassMismatchError < StandardError
attr_reader :name
attr_reader :entry
@@ -334,35 +340,34 @@
super "#{Location.to_string decls.last.location}: Duplicated declaration: #{name}"
end
end
class InvalidVarianceAnnotationError < StandardError
- MethodTypeError = Struct.new(:method_name, :method_type, :param, keyword_init: true)
- InheritanceError = Struct.new(:super_class, :param, keyword_init: true)
- MixinError = Struct.new(:include_member, :param, keyword_init: true)
+ attr_reader :type_name
+ attr_reader :param
+ attr_reader :location
- attr_reader :decl
- attr_reader :errors
+ def initialize(type_name:, param:, location:)
+ @type_name = type_name
+ @param = param
+ @location = location
- def initialize(decl:, errors:)
- @decl = decl
- @errors = errors
+ super "#{Location.to_string location}: Type parameter variance error: #{param.name} is #{param.variance} but used as incompatible variance"
+ end
+ end
- message = [
- "#{Location.to_string decl.location}: Invalid variance annotation: #{decl.name}"
- ]
+ class RecursiveAliasDefinitionError < StandardError
+ attr_reader :type
+ attr_reader :defs
- errors.each do |error|
- case error
- when MethodTypeError
- message << " MethodTypeError (#{error.param.name}): on `#{error.method_name}` #{error.method_type.to_s} (#{error.method_type.location&.start_line})"
- when InheritanceError
- message << " InheritanceError: #{error.super_class}"
- when MixinError
- message << " MixinError: #{error.include_member.name} (#{error.include_member.location&.start_line})"
- end
- end
+ def initialize(type:, defs:)
+ @type = type
+ @defs = defs
- super message.join("\n")
+ super "#{Location.to_string location}: Recursive aliases in #{type}: #{defs.map(&:name).join(", ")}"
+ end
+
+ def location
+ defs[0].original.location
end
end
end