Skip to content

[cxx-interop] Allow import-as-member for functions declared within a namespace #82579

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

Merged
merged 1 commit into from
Jul 2, 2025
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
8 changes: 5 additions & 3 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10230,9 +10230,11 @@ ClangImporter::Implementation::importDeclContextOf(
if (!importedDC) return nullptr;

// If the declaration was not global to start with, we're done.
bool isGlobal =
decl->getDeclContext()->getRedeclContext()->isTranslationUnit();
if (!isGlobal) return importedDC;
bool isRenamedGlobal =
decl->getDeclContext()->getRedeclContext()->isTranslationUnit() ||
(context.getKind() == EffectiveClangContext::UnresolvedContext &&
decl->getDeclContext()->getRedeclContext()->isNamespace());
if (!isRenamedGlobal) return importedDC;

// If the resulting declaration context is not a nominal type,
// we're done.
Expand Down
5 changes: 5 additions & 0 deletions test/Interop/Cxx/namespace/Inputs/import-as-member.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ typedef MyNS::MyDeepNS::DeepNestedStruct DeepNestedStructTypedef;

int deepNestedStructTypedef_method(DeepNestedStructTypedef p) SWIFT_NAME("DeepNestedStructTypedef.methodTypedef(self:)") { return p.value + 3; }
int deepNestedStructTypedef_methodQualName(MyNS::MyDeepNS::DeepNestedStruct p) SWIFT_NAME("DeepNestedStructTypedef.methodTypedefQualName(self:)") { return p.value + 4; }

namespace MyNS {
int nestedStruct_nestedMethod(MyNS::NestedStruct p) SWIFT_NAME("MyNS.NestedStruct.nestedMethod(self:)") { return p.value + 3; }
inline int nestedStruct_nestedMethodInline(MyNS::NestedStruct p) SWIFT_NAME("MyNS.NestedStruct.nestedMethodInline(self:)") { return p.value + 4; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// CHECK-NEXT: func method() -> Int32
// CHECK-NEXT: func methodConstRef() -> Int32
// CHECK-NEXT: func methodInline() -> Int32
// CHECK-NEXT: func nestedMethod() -> Int32
// CHECK-NEXT: func nestedMethodInline() -> Int32
// CHECK-NEXT: }

// CHECK: extension MyNS.MyDeepNS.DeepNestedStruct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ImportAsMember
func takesNestedStruct(_ s: MyNS.NestedStruct) {
_ = s.method()
_ = s.methodConstRef()
_ = s.nestedMethod()
_ = s.nestedMethodInline()

_ = nestedStruct_method(s) // expected-error {{'nestedStruct_method' has been replaced by instance method 'MyNS.NestedStruct.method()'}}
}
Expand All @@ -17,3 +19,6 @@ func takesDeepNestedStruct(_ s: MyNS.MyDeepNS.DeepNestedStruct) {

_ = deepNestedStruct_method(s) // expected-error {{'deepNestedStruct_method' has been replaced by instance method 'MyNS.MyDeepNS.DeepNestedStruct.method()'}}
}

MyNS.method() // expected-error {{type 'MyNS' has no member 'method'}}
MyNS.nestedMethod() // expected-error {{type 'MyNS' has no member 'nestedMethod'}}
6 changes: 6 additions & 0 deletions test/Interop/Cxx/namespace/import-as-member.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ NamespacesTestSuite.test("Struct in a deep namespace") {
expectEqual(460, s.methodTypedefQualName())
}

NamespacesTestSuite.test("Struct and function in a namespace") {
let s = MyNS.NestedStruct()
expectEqual(126, s.nestedMethod())
expectEqual(127, s.nestedMethodInline())
}

runAllTests()