diff --git a/lib/rbs/resolver/type_name_resolver.rb b/lib/rbs/resolver/type_name_resolver.rb index 7d252e35e4..871a880e18 100644 --- a/lib/rbs/resolver/type_name_resolver.rb +++ b/lib/rbs/resolver/type_name_resolver.rb @@ -29,22 +29,47 @@ def resolve(type_name, context:) end try_cache([type_name, context]) do - resolve_in(type_name, context) + head, tail = partition(type_name) + + head = resolve_in(head, context) + + if head + if tail + has_name?(tail.with_prefix(head.to_namespace)) + else + head + end + end + end + end + + def partition(type_name) + if type_name.namespace.empty? + head = type_name.name + tail = nil + else + head, *tail = type_name.namespace.path.fetch(0) + tail = TypeName.new( + name: type_name.name, + namespace: Namespace.new(absolute: false, path: tail) + ) end + + [head, tail] end - def resolve_in(type_name, context) + def resolve_in(head, context) if context parent, child = context case child when false - resolve_in(type_name, parent) + resolve_in(head, parent) when TypeName - name = type_name.with_prefix(child.to_namespace) - has_name?(name) || resolve_in(type_name, parent) + name = TypeName.new(name: head, namespace: child.to_namespace) + has_name?(name) || resolve_in(head, parent) end else - has_name?(type_name.absolute!) + has_name?(TypeName.new(name: head, namespace: Namespace.root)) end end diff --git a/sig/resolver/type_name_resolver.rbs b/sig/resolver/type_name_resolver.rbs index bca1c2b106..650c1914d9 100644 --- a/sig/resolver/type_name_resolver.rbs +++ b/sig/resolver/type_name_resolver.rbs @@ -25,7 +25,9 @@ module RBS def try_cache: (query) { () -> TypeName? } -> TypeName? - def resolve_in: (TypeName, context) -> TypeName? + def resolve_in: (Symbol, context) -> TypeName? + + def partition: (TypeName) -> [Symbol, TypeName?] end end end diff --git a/test/rbs/resolver/type_name_resolver_test.rb b/test/rbs/resolver/type_name_resolver_test.rb index 081f2d2c8c..40d47aea65 100644 --- a/test/rbs/resolver/type_name_resolver_test.rb +++ b/test/rbs/resolver/type_name_resolver_test.rb @@ -42,6 +42,26 @@ class Bar end end + def test_resolve_failure + SignatureManager.new do |manager| + manager.files[Pathname("foo.rbs")] = <