Skip to content

Commit a18db8f

Browse files
authored
Merge pull request #82579 from swiftlang/egorzhdan/import-as-member-from-namespace
[cxx-interop] Allow import-as-member for functions declared within a namespace
2 parents dd28c53 + d3dc17a commit a18db8f

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10300,9 +10300,11 @@ ClangImporter::Implementation::importDeclContextOf(
1030010300
if (!importedDC) return nullptr;
1030110301

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

1030710309
// If the resulting declaration context is not a nominal type,
1030810310
// we're done.

test/Interop/Cxx/namespace/Inputs/import-as-member.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ typedef MyNS::MyDeepNS::DeepNestedStruct DeepNestedStructTypedef;
2525

2626
int deepNestedStructTypedef_method(DeepNestedStructTypedef p) SWIFT_NAME("DeepNestedStructTypedef.methodTypedef(self:)") { return p.value + 3; }
2727
int deepNestedStructTypedef_methodQualName(MyNS::MyDeepNS::DeepNestedStruct p) SWIFT_NAME("DeepNestedStructTypedef.methodTypedefQualName(self:)") { return p.value + 4; }
28+
29+
namespace MyNS {
30+
int nestedStruct_nestedMethod(MyNS::NestedStruct p) SWIFT_NAME("MyNS.NestedStruct.nestedMethod(self:)") { return p.value + 3; }
31+
inline int nestedStruct_nestedMethodInline(MyNS::NestedStruct p) SWIFT_NAME("MyNS.NestedStruct.nestedMethodInline(self:)") { return p.value + 4; }
32+
}

test/Interop/Cxx/namespace/import-as-member-module-interface.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// CHECK-NEXT: func method() -> Int32
55
// CHECK-NEXT: func methodConstRef() -> Int32
66
// CHECK-NEXT: func methodInline() -> Int32
7+
// CHECK-NEXT: func nestedMethod() -> Int32
8+
// CHECK-NEXT: func nestedMethodInline() -> Int32
79
// CHECK-NEXT: }
810

911
// CHECK: extension MyNS.MyDeepNS.DeepNestedStruct {

test/Interop/Cxx/namespace/import-as-member-typechecker.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import ImportAsMember
55
func takesNestedStruct(_ s: MyNS.NestedStruct) {
66
_ = s.method()
77
_ = s.methodConstRef()
8+
_ = s.nestedMethod()
9+
_ = s.nestedMethodInline()
810

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

1820
_ = deepNestedStruct_method(s) // expected-error {{'deepNestedStruct_method' has been replaced by instance method 'MyNS.MyDeepNS.DeepNestedStruct.method()'}}
1921
}
22+
23+
MyNS.method() // expected-error {{type 'MyNS' has no member 'method'}}
24+
MyNS.nestedMethod() // expected-error {{type 'MyNS' has no member 'nestedMethod'}}

test/Interop/Cxx/namespace/import-as-member.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ NamespacesTestSuite.test("Struct in a deep namespace") {
2222
expectEqual(460, s.methodTypedefQualName())
2323
}
2424

25+
NamespacesTestSuite.test("Struct and function in a namespace") {
26+
let s = MyNS.NestedStruct()
27+
expectEqual(126, s.nestedMethod())
28+
expectEqual(127, s.nestedMethodInline())
29+
}
30+
2531
runAllTests()

0 commit comments

Comments
 (0)