Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support detailed_message for DuplicatedMethodDefinitionError, DuplicatedInterfaceMethodDefinitionError, UnknownMethodAliasError and InvalidOverloadMethodError #1281

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/rbs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,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 @@ -256,6 +258,8 @@ def other_locations
end

class DuplicatedInterfaceMethodDefinitionError < DefinitionError
include DetailedMessageable

attr_reader :type
attr_reader :method_name
attr_reader :member
Expand All @@ -268,6 +272,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 @@ -283,6 +291,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 @@ -310,6 +320,8 @@ def initialize(name:, entry:)
end

class InvalidOverloadMethodError < DefinitionError
include DetailedMessageable

attr_reader :type_name
attr_reader :method_name
attr_reader :kind
Expand All @@ -330,6 +342,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