Skip to content

Commit

Permalink
Toplevel constant must have the lowlevel precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Apr 5, 2022
1 parent dc942d8 commit 4a7071d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/rbs/resolver/constant_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ def load_context_constants(context)
# @type var consts: Hash[Symbol, Constant]
consts = {}

# Toplevel constants has the lowest precedence
consts.merge!(table.toplevel)
if context
if last = context[1]
# Ancestor constants has the next precedence
constants_from_ancestors(last, constants: consts)
end
end
# Context (module nesting) constants has the next precedence
constants_from_context(context, constants: consts) or return
# The constant itself comes the last
constants_itself(context, constants: consts)

context_constants_cache[context] = consts
Expand Down Expand Up @@ -152,8 +157,6 @@ def constants_from_context(context, constants:)
consts = table.children(last) or return false
constants.merge!(consts)
end
else
constants.merge!(table.toplevel)
end

true
Expand Down
23 changes: 23 additions & 0 deletions test/rbs/resolver/constant_resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,27 @@ class Stuff
end
end
end

def test_reference_constant_toplevel
SignatureManager.new do |manager|
manager.files[Pathname("foo.rbs")] = <<EOF
CONST: Integer
class Foo
CONST: String
end
class Bar < Foo
end
EOF
manager.build do |env|
builder = DefinitionBuilder.new(env: env)
resolver = Resolver::ConstantResolver.new(builder: builder)

resolver.resolve(:CONST, context: [nil, TypeName("::Bar")]).tap do |constant|
assert_equal "::Foo::CONST", constant.name.to_s
end
end
end
end
end

0 comments on commit 4a7071d

Please sign in to comment.