Skip to content

[cxx-interop] Re-land part of 6ba7a1e. Fix an issue with extending nested namespaces across modules. #40905

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
Jan 20, 2022
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
6 changes: 6 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,12 @@ namespace {
for (auto redecl : decl->redecls())
Impl.ImportedDecls[{redecl, getVersion()}] = enumDecl;

// Because a namespaces's decl context is the bridging header, make sure
// we add them to the bridging header lookup table.
addEntryToLookupTable(*Impl.BridgingHeaderLookupTable,
const_cast<clang::NamespaceDecl *>(decl),
Impl.getNameImporter());

return enumDecl;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
private:
DiagnosticWalker Walker;

/// The Swift lookup table for the bridging header.
std::unique_ptr<SwiftLookupTable> BridgingHeaderLookupTable;

/// The Swift lookup tables, per module.
///
/// Annoyingly, we list this table early so that it gets torn down after
Expand Down Expand Up @@ -494,6 +491,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
llvm::SmallDenseMap<ModuleDecl *, SourceFile *> ClangSwiftAttrSourceFiles;

public:
/// The Swift lookup table for the bridging header.
std::unique_ptr<SwiftLookupTable> BridgingHeaderLookupTable;

/// Mapping of already-imported declarations.
llvm::DenseMap<std::pair<const clang::Decl *, Version>, Decl *> ImportedDecls;

Expand Down
8 changes: 8 additions & 0 deletions test/Interop/Cxx/modules/Inputs/bridge-header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef TEST_INTEROP_CXX_MODULES_INPUTS_BRIDGE_HEADER_H
#define TEST_INTEROP_CXX_MODULES_INPUTS_BRIDGE_HEADER_H

namespace Namespace {
struct InNamespace {};
}

#endif // TEST_INTEROP_CXX_MODULES_INPUTS_BRIDGE_HEADER_H
4 changes: 4 additions & 0 deletions test/Interop/Cxx/modules/Inputs/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Namespace {
header "namespace.h"
requires cplusplus
}
13 changes: 13 additions & 0 deletions test/Interop/Cxx/modules/Inputs/namespace-extension-lib.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Namespace

extension Namespace.Parent {
public static func test() -> Int { 42 }
}

extension Namespace.Parent.Child {
public static func test() -> Int { 52 }
}

extension Namespace.NestedNamespace.NestedStruct {
public func test() -> Int { 62 }
}
18 changes: 18 additions & 0 deletions test/Interop/Cxx/modules/Inputs/namespace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef TEST_INTEROP_CXX_MODULES_INPUTS_NAMESPACE_H
#define TEST_INTEROP_CXX_MODULES_INPUTS_NAMESPACE_H

namespace Namespace {

struct Parent {
struct Child {};
};

namespace NestedNamespace {

struct NestedStruct {};

} // namespace NestedNamespace

} // namespace Namespace

#endif // TEST_INTEROP_CXX_MODULES_INPUTS_NAMESPACE_H
24 changes: 24 additions & 0 deletions test/Interop/Cxx/modules/use-namespace-extension-lib.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %empty-directory(%t)
// RUN: cd %t
// 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
// RUN: %target-build-swift %s -I %S/Inputs -Xfrontend -enable-cxx-interop -o %t/run -I %t/ -L %t/ -lNamespaceExtensionLib
// RUN: %target-codesign %t/run
// RUN: %target-run %t/run
//
// REQUIRES: executable_test
// XFAIL: OS=windows-msvc

import StdlibUnittest
import Namespace
import NamespaceExtensionLib

var NamespacesTestSuite = TestSuite("Extension in library on namespace")

NamespacesTestSuite.test("Call functions from extension") {
expectEqual(Namespace.Parent.test(), 42)
expectEqual(Namespace.Parent.Child.test(), 52)
expectEqual(Namespace.NestedNamespace.NestedStruct().test(), 62)
}

runAllTests()

4 changes: 2 additions & 2 deletions test/Interop/Cxx/namespace/templates-module-interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
// CHECK-NEXT: }
// CHECK-NEXT: typealias ForwardDeclaredClassTemplateOutOfLineChar = TemplatesNS1.TemplatesNS2.__CxxTemplateInstN12TemplatesNS112TemplatesNS237ForwardDeclaredClassTemplateOutOfLineIcEE
// CHECK-NEXT: enum TemplatesNS4 {
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIcEE {
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIiEE {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIiEE {
// CHECK-NEXT: struct __CxxTemplateInstN12TemplatesNS417HasSpecializationIcEE {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: struct HasSpecialization<> {
Expand Down