Skip to content

Commit 3b54549

Browse files
authored
Merge pull request #34331 from artemcm/NoFoundationOnExplicitImportSoil
[Clang Importer] Do not rely on being able to always import Foundation on-demand
2 parents 5983960 + e98dbc8 commit 3b54549

File tree

7 files changed

+23
-13
lines changed

7 files changed

+23
-13
lines changed

include/swift/AST/Module.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ class ModuleDecl : public DeclContext, public TypeDecl {
694694
/// \returns true if this module is the "SwiftOnoneSupport" module;
695695
bool isOnoneSupportModule() const;
696696

697+
/// \returns true if this module is the "Foundation" module;
698+
bool isFoundationModule() const;
699+
697700
/// \returns true if traversal was aborted, false otherwise.
698701
bool walk(ASTWalker &Walker);
699702

lib/AST/Module.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,10 @@ bool ModuleDecl::isOnoneSupportModule() const {
13361336
return !getParent() && getName().str() == SWIFT_ONONE_SUPPORT;
13371337
}
13381338

1339+
bool ModuleDecl::isFoundationModule() const {
1340+
return !getParent() && getName() == getASTContext().Id_Foundation;
1341+
}
1342+
13391343
bool ModuleDecl::isBuiltinModule() const {
13401344
return this == getASTContext().TheBuiltinModule;
13411345
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5690,8 +5690,7 @@ namespace {
56905690
}
56915691

56925692
static bool conformsToProtocolInOriginalModule(NominalTypeDecl *nominal,
5693-
const ProtocolDecl *proto,
5694-
ModuleDecl *foundationModule) {
5693+
const ProtocolDecl *proto) {
56955694
auto &ctx = nominal->getASTContext();
56965695

56975696
if (inheritanceListContainsProtocol(nominal, proto))
@@ -5714,7 +5713,7 @@ static bool conformsToProtocolInOriginalModule(NominalTypeDecl *nominal,
57145713
for (ExtensionDecl *extension : nominal->getExtensions()) {
57155714
ModuleDecl *extensionModule = extension->getParentModule();
57165715
if (extensionModule != originalModule && extensionModule != overlayModule &&
5717-
extensionModule != foundationModule) {
5716+
!extensionModule->isFoundationModule()) {
57185717
continue;
57195718
}
57205719
if (inheritanceListContainsProtocol(extension, proto))
@@ -5809,8 +5808,7 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
58095808

58105809
// Break circularity by only looking for declared conformances in the
58115810
// original module, or possibly its overlay.
5812-
if (conformsToProtocolInOriginalModule(computedNominal, proto,
5813-
Impl.tryLoadFoundationModule())) {
5811+
if (conformsToProtocolInOriginalModule(computedNominal, proto)) {
58145812
synthesizedProtocols.push_back(kind);
58155813
return true;
58165814
}

lib/ClangImporter/ImportType.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,13 +1274,11 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl,
12741274
impl.SwiftContext.getSwiftName(KnownFoundationEntity::NSError))
12751275
return Type();
12761276

1277-
ModuleDecl *foundationModule = impl.tryLoadFoundationModule();
1278-
if (!foundationModule ||
1279-
foundationModule->getName()
1280-
!= elementClass->getModuleContext()->getName())
1277+
if (!impl.canImportFoundationModule() ||
1278+
!elementClass->getModuleContext()->isFoundationModule())
12811279
return Type();
12821280

1283-
1281+
ModuleDecl *foundationModule = impl.tryLoadFoundationModule();
12841282
if (resugarNSErrorPointer)
12851283
return impl.getNamedSwiftType(
12861284
foundationModule,
@@ -1386,7 +1384,7 @@ static ImportedType adjustTypeForConcreteImport(
13861384
// id and Any can be bridged without Foundation. There would be
13871385
// bootstrapping issues with the ObjectiveC module otherwise.
13881386
if (hint.BridgedType->isAny()
1389-
|| impl.tryLoadFoundationModule()
1387+
|| impl.canImportFoundationModule()
13901388
|| impl.ImportForwardDeclarations) {
13911389

13921390
// Set the bridged type if it wasn't done already.
@@ -2517,6 +2515,10 @@ ModuleDecl *ClangImporter::Implementation::tryLoadFoundationModule() {
25172515
ImportForwardDeclarations, checkedModules);
25182516
}
25192517

2518+
bool ClangImporter::Implementation::canImportFoundationModule() {
2519+
return SwiftContext.canImportModule({SwiftContext.Id_Foundation, SourceLoc()});
2520+
}
2521+
25202522
Type ClangImporter::Implementation::getNamedSwiftType(ModuleDecl *module,
25212523
StringRef name) {
25222524
if (!module)

lib/ClangImporter/ImporterImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
921921
/// into the ASTContext.
922922
ModuleDecl *tryLoadFoundationModule();
923923

924+
/// Returns whether or not the "Foundation" module can be imported, without loading it.
925+
bool canImportFoundationModule();
926+
924927
/// Retrieves the Swift wrapper for the given Clang module, creating
925928
/// it if necessary.
926929
ClangModuleUnit *getWrapperForModule(const clang::Module *underlying,

test/Interop/Cxx/class/constructors-objc-irgen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// REQUIRES: CPU=x86_64
44
// REQUIRES: objc_interop
5-
5+
import Foundation
66
import ConstructorsObjC
77

88
public func createConstructorWithNSArrayParam() -> ConstructorWithNSArrayParam {

test/Interop/Cxx/class/constructors-objc-silgen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/Inputs -enable-cxx-interop -emit-sil %s | %FileCheck %s
22

33
// REQUIRES: objc_interop
4-
4+
import Foundation
55
import ConstructorsObjC
66

77
// CHECK: [[VAR:%[0-9]+]] = alloc_stack $ConstructorWithNSArrayParam

0 commit comments

Comments
 (0)