Skip to content

Commit 76e2934

Browse files
committed
[cxx-interop] Re-land part of 6ba7a1e. Fix an issue with extending nested namespaces across modules.
No one should be using C++ interop yet, so this part of the change cannot break anyone.
1 parent 2928459 commit 76e2934

File tree

8 files changed

+78
-5
lines changed

8 files changed

+78
-5
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,12 @@ namespace {
24732473
for (auto redecl : decl->redecls())
24742474
Impl.ImportedDecls[{redecl, getVersion()}] = enumDecl;
24752475

2476+
// Because a namespaces's decl context is the bridging header, make sure
2477+
// we add them to the bridging header lookup table.
2478+
addEntryToLookupTable(*Impl.BridgingHeaderLookupTable,
2479+
const_cast<clang::NamespaceDecl *>(decl),
2480+
Impl.getNameImporter());
2481+
24762482
return enumDecl;
24772483
}
24782484

lib/ClangImporter/ImporterImpl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
428428
private:
429429
DiagnosticWalker Walker;
430430

431-
/// The Swift lookup table for the bridging header.
432-
std::unique_ptr<SwiftLookupTable> BridgingHeaderLookupTable;
433-
434431
/// The Swift lookup tables, per module.
435432
///
436433
/// Annoyingly, we list this table early so that it gets torn down after
@@ -494,6 +491,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
494491
llvm::SmallDenseMap<ModuleDecl *, SourceFile *> ClangSwiftAttrSourceFiles;
495492

496493
public:
494+
/// The Swift lookup table for the bridging header.
495+
std::unique_ptr<SwiftLookupTable> BridgingHeaderLookupTable;
496+
497497
/// Mapping of already-imported declarations.
498498
llvm::DenseMap<std::pair<const clang::Decl *, Version>, Decl *> ImportedDecls;
499499

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_INTEROP_CXX_MODULES_INPUTS_BRIDGE_HEADER_H
2+
#define TEST_INTEROP_CXX_MODULES_INPUTS_BRIDGE_HEADER_H
3+
4+
namespace Namespace {
5+
struct InNamespace {};
6+
}
7+
8+
#endif // TEST_INTEROP_CXX_MODULES_INPUTS_BRIDGE_HEADER_H
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Namespace {
2+
header "namespace.h"
3+
requires cplusplus
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Namespace
2+
3+
extension Namespace.Parent {
4+
public static func test() -> Int { 42 }
5+
}
6+
7+
extension Namespace.Parent.Child {
8+
public static func test() -> Int { 52 }
9+
}
10+
11+
extension Namespace.NestedNamespace.NestedStruct {
12+
public func test() -> Int { 62 }
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef TEST_INTEROP_CXX_MODULES_INPUTS_NAMESPACE_H
2+
#define TEST_INTEROP_CXX_MODULES_INPUTS_NAMESPACE_H
3+
4+
namespace Namespace {
5+
6+
struct Parent {
7+
struct Child {};
8+
};
9+
10+
namespace NestedNamespace {
11+
12+
struct NestedStruct {};
13+
14+
} // namespace NestedNamespace
15+
16+
} // namespace Namespace
17+
18+
#endif // TEST_INTEROP_CXX_MODULES_INPUTS_NAMESPACE_H
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: cd %t
3+
// RUN: %target-build-swift -I %S/Inputs -Xfrontend -enable-cxx-interop %S/Inputs/namespace-extension-lib.swift -emit-module -emit-library -static -module-name NamespaceExtensionLib
4+
// RUN: %target-build-swift %s -I %S/Inputs -Xfrontend -enable-cxx-interop -o %t/run -I %t/ -L %t/ -lNamespaceExtensionLib
5+
// RUN: %target-codesign %t/run
6+
// RUN: %target-run %t/run
7+
//
8+
// REQUIRES: executable_test
9+
// XFAIL: OS=windows-msvc
10+
11+
import StdlibUnittest
12+
import Namespace
13+
import NamespaceExtensionLib
14+
15+
var NamespacesTestSuite = TestSuite("Extension in library on namespace")
16+
17+
NamespacesTestSuite.test("Call functions from extension") {
18+
expectEqual(Namespace.Parent.test(), 42)
19+
expectEqual(Namespace.Parent.Child.test(), 52)
20+
expectEqual(Namespace.NestedNamespace.NestedStruct().test(), 62)
21+
}
22+
23+
runAllTests()
24+

test/Interop/Cxx/namespace/templates-module-interface.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
// CHECK-NEXT: }
4848
// CHECK-NEXT: typealias ForwardDeclaredClassTemplateOutOfLineChar = TemplatesNS1.TemplatesNS2.__CxxTemplateInstN12TemplatesNS112TemplatesNS237ForwardDeclaredClassTemplateOutOfLineIcEE
4949
// CHECK-NEXT: enum TemplatesNS4 {
50-
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIcEE {
50+
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIiEE {
5151
// CHECK-NEXT: init()
5252
// CHECK-NEXT: }
53-
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIiEE {
53+
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIcEE {
5454
// CHECK-NEXT: init()
5555
// CHECK-NEXT: }
5656
// CHECK-NEXT: struct HasSpecialization<> {

0 commit comments

Comments
 (0)