Skip to content

Commit

Permalink
Merge pull request #1281 from ksss/error-2
Browse files Browse the repository at this point in the history
Support detailed_message for DuplicatedMethodDefinitionError, DuplicatedInterfaceMethodDefinitionError, UnknownMethodAliasError and InvalidOverloadMethodError
  • Loading branch information
soutaro committed Mar 20, 2023
2 parents b984cec + a1024be commit 96fb810
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/rbs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ def self.check!(type_name, env:, member:)
end

class DuplicatedMethodDefinitionError < DefinitionError
include DetailedMessageable

attr_reader :type
attr_reader :method_name
attr_reader :members
Expand Down Expand Up @@ -273,6 +275,8 @@ def other_locations
end

class DuplicatedInterfaceMethodDefinitionError < DefinitionError
include DetailedMessageable

attr_reader :type
attr_reader :method_name
attr_reader :member
Expand All @@ -285,6 +289,10 @@ def initialize(type:, method_name:, member:)
super "#{member.location}: Duplicated method definition: #{qualified_method_name}"
end

def location
member.location
end

def qualified_method_name
case type
when Types::ClassSingleton
Expand All @@ -300,6 +308,8 @@ def type_name
end

class UnknownMethodAliasError < DefinitionError
include DetailedMessageable

attr_reader :type_name
attr_reader :original_name
attr_reader :aliased_name
Expand Down Expand Up @@ -327,6 +337,8 @@ def initialize(name:, entry:)
end

class InvalidOverloadMethodError < DefinitionError
include DetailedMessageable

attr_reader :type_name
attr_reader :method_name
attr_reader :kind
Expand All @@ -347,6 +359,10 @@ def initialize(type_name:, method_name:, kind:, members:)

super "#{Location.to_string members[0].location}: Invalid method overloading: #{type_name}#{delimiter}#{method_name}"
end

def location
members[0].location
end
end

class GenericParameterMismatchError < LoadingError
Expand Down
28 changes: 28 additions & 0 deletions test/rbs/definition_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,13 @@ class Error

assert_raises UnknownMethodAliasError do
builder.build_singleton(type_name("::Error"))
end.tap do |error|
assert_equal error.detailed_message, <<~DETAILED_MESSAGE if Exception.method_defined?(:detailed_message)
#{error.message} (RBS::UnknownMethodAliasError)
alias self.xxx self.yyy
^^^^^^^^^^^^^^^^^^^^^^^
DETAILED_MESSAGE
end
end
end
Expand Down Expand Up @@ -1535,6 +1542,13 @@ def foo: (Integer) -> String | ...

assert_raises RBS::InvalidOverloadMethodError do
builder.build_instance(type_name("::Hello"))
end.tap do |error|
assert_equal error.detailed_message, <<~DETAILED_MESSAGE if Exception.method_defined?(:detailed_message)
#{error.message} (RBS::InvalidOverloadMethodError)
def foo: (Integer) -> String | ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DETAILED_MESSAGE
end
end
end
Expand Down Expand Up @@ -1666,6 +1680,13 @@ class Hello

assert_raises RBS::DuplicatedInterfaceMethodDefinitionError do
builder.build_instance(type_name("::Hello"))
end.tap do |error|
assert_equal error.detailed_message, <<~DETAILED_MESSAGE if Exception.method_defined?(:detailed_message)
#{error.message} (RBS::DuplicatedInterfaceMethodDefinitionError)
include _I2
^^^^^^^^^^^
DETAILED_MESSAGE
end
end
end
Expand All @@ -1689,6 +1710,13 @@ def foo: () -> String

assert_raises RBS::DuplicatedMethodDefinitionError do
builder.build_instance(type_name("::Hello"))
end.tap do |error|
assert_equal error.detailed_message, <<~DETAILED_MESSAGE if Exception.method_defined?(:detailed_message)
#{error.message} (RBS::DuplicatedMethodDefinitionError)
def foo: () -> String
^^^^^^^^^^^^^^^^^^^^^
DETAILED_MESSAGE
end
end
end
Expand Down

0 comments on commit 96fb810

Please sign in to comment.