Skip to content

Commit ee10f30

Browse files
committed
[cxx-interop] Implicitly import Cxx module
This lifts the requirement for the user to explicitly add `import Cxx` in Swift code that relies on protocols from the `Cxx` module, such as `CxxSequence`.
1 parent 45b8c1e commit ee10f30

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,19 +3269,6 @@ static void getImportDecls(ClangModuleUnit *ClangUnit, const clang::Module *M,
32693269
auto *ID = createImportDecl(Ctx, ClangUnit, ImportedMod, Exported);
32703270
Results.push_back(ID);
32713271
}
3272-
3273-
if (Ctx.LangOpts.EnableCXXInterop && requiresCPlusPlus(M)) {
3274-
// Try to load the Cxx module. We don't use it directly here, but we need to
3275-
// make sure that ASTContext has loaded this module.
3276-
auto *cxxModule = Ctx.getModuleByIdentifier(Ctx.Id_Cxx);
3277-
if (cxxModule) {
3278-
ImportPath::Builder builder(Ctx.Id_Cxx);
3279-
auto *importCxx =
3280-
ImportDecl::create(Ctx, ClangUnit, SourceLoc(), ImportKind::Module,
3281-
SourceLoc(), builder.get());
3282-
Results.push_back(importCxx);
3283-
}
3284-
}
32853272
}
32863273

32873274
void ClangModuleUnit::getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive) const {
@@ -3918,6 +3905,17 @@ void ClangModuleUnit::getImportedModulesForLookup(
39183905
} else {
39193906
clangModule->getExportedModules(imported);
39203907
topLevel = clangModule->getTopLevelModule();
3908+
3909+
// If this is a C++ module, implicitly import the Cxx module, which contains
3910+
// definitions of Swift protocols that C++ types might conform to, such as
3911+
// CxxSequence.
3912+
if (owner.SwiftContext.LangOpts.EnableCXXInterop &&
3913+
requiresCPlusPlus(clangModule) && clangModule->Name != "CxxShim") {
3914+
auto *cxxModule =
3915+
owner.SwiftContext.getModuleByIdentifier(owner.SwiftContext.Id_Cxx);
3916+
if (cxxModule)
3917+
imports.push_back({ImportPath::Access(), cxxModule});
3918+
}
39213919
}
39223920

39233921
if (imported.empty()) {

test/Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop
22

33
import CustomSequence
4-
import Cxx
54

65
func checkIntSequence<S>(_ seq: S) where S: Sequence, S.Element == Int32 {
76
let contains = seq.contains(where: { $0 == 3 })

test/Interop/Cxx/stdlib/overlay/custom-sequence.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import StdlibUnittest
77
import CustomSequence
8-
import Cxx
98

109
var CxxSequenceTestSuite = TestSuite("CxxSequence")
1110

0 commit comments

Comments
 (0)