Skip to content
Open
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
4 changes: 3 additions & 1 deletion lib/rbs/definition_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ def validate_variable(var)
when AST::Ruby::Members::AttrReaderMember, AST::Ruby::Members::AttrWriterMember, AST::Ruby::Members::AttrAccessorMember
# nop
else
variables << tmp_var
# A module that is included more than once, directly or through a superclass or another module,
# inserts the same declaration again, which is not a duplicate.
variables << tmp_var unless variables.any? {|v| v.source.equal?(tmp_var.source) }
end

tmp_var = tmp_var.parent_variable
Expand Down
68 changes: 68 additions & 0 deletions test/rbs/definition_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3026,6 +3026,74 @@ class ClassInstanceVariable
end
end

def test_variable_of_module_included_more_than_once
SignatureManager.new do |manager|
manager.add_file("instance.rbs", <<-EOF)
module M
@m: Integer
end

module N
include M
end

class IncludedTwice
include M
include M
end

class Parent
include M
end

class IncludedInParent < Parent
include M
end

class IncludedThroughModule
include M
include N
end

module Duplicated
@m: Integer
@m: Integer
end

class DuplicatedIncludedTwice
include Duplicated
include Duplicated
end

class DuplicatedInClass
include M
include M

@c: Integer
@c: Integer
end
EOF

manager.build do |env|
builder = DefinitionBuilder.new(env: env)

["::IncludedTwice", "::IncludedInParent", "::IncludedThroughModule"].each do |name|
definition = builder.build_instance(type_name(name))
var = definition.instance_variables[:@m]
assert_equal parse_type("::Integer"), var.type
assert_equal type_name("::M"), var.declared_in
end

assert_raises(RBS::InstanceVariableDuplicationError) do
builder.build_instance(type_name("::DuplicatedIncludedTwice"))
end
assert_raises(RBS::InstanceVariableDuplicationError) do
builder.build_instance(type_name("::DuplicatedInClass"))
end
end
end
end

def test_annotations__method_def
SignatureManager.new do |manager|
manager.add_file("inherited.rbs", <<-EOF)
Expand Down
Loading