Skip to content

Commit 6bc52fd

Browse files
authored
Merge pull request #63108 from apple/egorzhdan/cxx-disallow-std
[cxx-interop] Disallow `import std`, require `import CxxStdlib`
2 parents fcfd089 + ee6b9b2 commit 6bc52fd

File tree

8 files changed

+23
-22
lines changed

8 files changed

+23
-22
lines changed

lib/AST/Module.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,8 @@ StringRef ModuleDecl::ReverseFullNameIterator::operator*() const {
19081908

19091909
auto *clangModule =
19101910
static_cast<const clang::Module *>(current.get<const void *>());
1911+
if (!clangModule->isSubModule() && clangModule->Name == "std")
1912+
return "CxxStdlib";
19111913
return clangModule->Name;
19121914
}
19131915

lib/ClangImporter/ClangImporter.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,9 @@ ModuleDecl *ClangImporter::Implementation::loadModule(
20282028
ModuleDecl *MD = nullptr;
20292029
ASTContext &ctx = getNameImporter().getContext();
20302030

2031+
// `CxxStdlib` is the only accepted spelling of the C++ stdlib module name.
2032+
if (path.front().Item.is("std"))
2033+
return nullptr;
20312034
if (path.front().Item == ctx.Id_CxxStdlib) {
20322035
ImportPath::Builder adjustedPath(ctx.getIdentifier("std"), importLoc);
20332036
adjustedPath.append(path.getSubmodulePath());
@@ -2376,7 +2379,9 @@ ClangModuleUnit *ClangImporter::Implementation::getWrapperForModule(
23762379
return cached;
23772380

23782381
// FIXME: Handle hierarchical names better.
2379-
Identifier name = SwiftContext.getIdentifier(underlying->Name);
2382+
Identifier name = underlying->Name == "std"
2383+
? SwiftContext.Id_CxxStdlib
2384+
: SwiftContext.getIdentifier(underlying->Name);
23802385
auto wrapper = ModuleDecl::create(name, SwiftContext);
23812386
wrapper->setIsSystemModule(underlying->IsSystem);
23822387
wrapper->setIsNonSwiftModule();
@@ -3253,7 +3258,13 @@ ImportDecl *swift::createImportDecl(ASTContext &Ctx,
32533258
ImportPath::Builder importPath;
32543259
auto *TmpMod = ImportedMod;
32553260
while (TmpMod) {
3256-
importPath.push_back(Ctx.getIdentifier(TmpMod->Name));
3261+
// If this is a C++ stdlib module, print its name as `CxxStdlib` instead of
3262+
// `std`. `CxxStdlib` is the only accepted spelling of the C++ stdlib module
3263+
// name in Swift.
3264+
Identifier moduleName = !TmpMod->isSubModule() && TmpMod->Name == "std"
3265+
? Ctx.Id_CxxStdlib
3266+
: Ctx.getIdentifier(TmpMod->Name);
3267+
importPath.push_back(moduleName);
32573268
TmpMod = TmpMod->Parent;
32583269
}
32593270
std::reverse(importPath.begin(), importPath.end());

lib/IRGen/GenDecl.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,9 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
496496
!getSwiftModule()->getName().is("Cxx") &&
497497
!getSwiftModule()->getName().is("CxxStdlib") &&
498498
!getSwiftModule()->getName().is("std")) {
499-
// TODO: link with swiftCxxStdlib unconditionally once the overlay module
500-
// is renamed in CMake
499+
this->addLinkLibrary(LinkLibrary("swiftCxxStdlib", LibraryKind::Library));
501500
if (target.isOSDarwin())
502-
this->addLinkLibrary(
503-
LinkLibrary("swiftCxxStdlib", LibraryKind::Library));
504-
this->addLinkLibrary(LinkLibrary("swiftstd", LibraryKind::Library));
501+
this->addLinkLibrary(LinkLibrary("swiftstd", LibraryKind::Library));
505502
}
506503
}
507504

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 738; // macro role attribute
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 739; // CxxStdlib
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ add_dependencies(sdk-overlay libstdcxx-modulemap)
127127
#
128128
# C++ Standard Library Overlay.
129129
#
130-
add_swift_target_library(swiftstd STATIC NO_LINK_NAME IS_STDLIB
130+
add_swift_target_library(swiftCxxStdlib STATIC NO_LINK_NAME IS_STDLIB
131131
std.swift
132132
String.swift
133133

test/Interop/Cxx/stdlib/libcxx-module-interface.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// This test is specific to libc++ and therefore only runs on Darwin platforms.
66
// REQUIRES: OS=macosx || OS=ios
77

8-
// CHECK-STD: import std.iosfwd
9-
// CHECK-STD: import std.string
8+
// CHECK-STD: import CxxStdlib.iosfwd
9+
// CHECK-STD: import CxxStdlib.string
1010

1111
// CHECK-IOSFWD: enum std {
1212
// CHECK-IOSFWD: enum __1 {

test/Interop/Cxx/stdlib/msvcprt-module-interface.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// This test is specific to msvcprt and therefore only runs on Windows.
66
// REQUIRES: OS=windows-msvc
77

8-
// CHECK-STD: import std.iosfwd
9-
// CHECK-STD: import std.string
8+
// CHECK-STD: import CxxStdlib.iosfwd
9+
// CHECK-STD: import CxxStdlib.string
1010

1111
// CHECK-STRING: enum std {
1212
// CHECK-STRING: typealias size_t = size_t

test/Interop/Cxx/stdlib/use-std-string.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
2-
// RUN: %target-run-simple-swift(-D USE_CXXSTDLIB_SPELLING -I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
32
//
43
// REQUIRES: executable_test
54

65
import StdlibUnittest
76
import StdString
87
#if os(Linux)
9-
#if USE_CXXSTDLIB_SPELLING
108
import CxxStdlib
11-
#else
12-
import std
13-
#endif
149
// FIXME: import CxxStdlib.string once libstdc++ is split into submodules.
1510
#else
16-
#if USE_CXXSTDLIB_SPELLING
1711
import CxxStdlib.string
18-
#else
19-
import std.string
20-
#endif
2112
#endif
2213

2314
var StdStringTestSuite = TestSuite("StdString")

0 commit comments

Comments
 (0)