Skip to content

[Clang Importer] Do not rely on being able to always import Foundation on-demand #34331

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 2 commits into from
Nov 2, 2020
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
3 changes: 3 additions & 0 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ class ModuleDecl : public DeclContext, public TypeDecl {
/// \returns true if this module is the "SwiftOnoneSupport" module;
bool isOnoneSupportModule() const;

/// \returns true if this module is the "Foundation" module;
bool isFoundationModule() const;

/// \returns true if traversal was aborted, false otherwise.
bool walk(ASTWalker &Walker);

Expand Down
4 changes: 4 additions & 0 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,10 @@ bool ModuleDecl::isOnoneSupportModule() const {
return !getParent() && getName().str() == SWIFT_ONONE_SUPPORT;
}

bool ModuleDecl::isFoundationModule() const {
return !getParent() && getName() == getASTContext().Id_Foundation;
}

bool ModuleDecl::isBuiltinModule() const {
return this == getASTContext().TheBuiltinModule;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5688,8 +5688,7 @@ namespace {
}

static bool conformsToProtocolInOriginalModule(NominalTypeDecl *nominal,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Now that we're not passing in Foundation, this should probably be called something like conformsToProtocolInFoundationModule().

const ProtocolDecl *proto,
ModuleDecl *foundationModule) {
const ProtocolDecl *proto) {
auto &ctx = nominal->getASTContext();

if (inheritanceListContainsProtocol(nominal, proto))
Expand All @@ -5712,7 +5711,7 @@ static bool conformsToProtocolInOriginalModule(NominalTypeDecl *nominal,
for (ExtensionDecl *extension : nominal->getExtensions()) {
ModuleDecl *extensionModule = extension->getParentModule();
if (extensionModule != originalModule && extensionModule != overlayModule &&
extensionModule != foundationModule) {
!extensionModule->isFoundationModule()) {
continue;
}
if (inheritanceListContainsProtocol(extension, proto))
Expand Down Expand Up @@ -5807,8 +5806,7 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,

// Break circularity by only looking for declared conformances in the
// original module, or possibly its overlay.
if (conformsToProtocolInOriginalModule(computedNominal, proto,
Impl.tryLoadFoundationModule())) {
if (conformsToProtocolInOriginalModule(computedNominal, proto)) {
synthesizedProtocols.push_back(kind);
return true;
}
Expand Down
14 changes: 8 additions & 6 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1265,13 +1265,11 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl,
impl.SwiftContext.getSwiftName(KnownFoundationEntity::NSError))
return Type();

ModuleDecl *foundationModule = impl.tryLoadFoundationModule();
if (!foundationModule ||
foundationModule->getName()
!= elementClass->getModuleContext()->getName())
if (!impl.canImportFoundationModule() ||
!elementClass->getModuleContext()->isFoundationModule())
return Type();


ModuleDecl *foundationModule = impl.tryLoadFoundationModule();
if (resugarNSErrorPointer)
return impl.getNamedSwiftType(
foundationModule,
Expand Down Expand Up @@ -1377,7 +1375,7 @@ static ImportedType adjustTypeForConcreteImport(
// id and Any can be bridged without Foundation. There would be
// bootstrapping issues with the ObjectiveC module otherwise.
if (hint.BridgedType->isAny()
|| impl.tryLoadFoundationModule()
|| impl.canImportFoundationModule()
|| impl.ImportForwardDeclarations) {

// Set the bridged type if it wasn't done already.
Expand Down Expand Up @@ -2502,6 +2500,10 @@ ModuleDecl *ClangImporter::Implementation::tryLoadFoundationModule() {
ImportForwardDeclarations, checkedModules);
}

bool ClangImporter::Implementation::canImportFoundationModule() {
return SwiftContext.canImportModule({SwiftContext.Id_Foundation, SourceLoc()});
}

Type ClangImporter::Implementation::getNamedSwiftType(ModuleDecl *module,
StringRef name) {
if (!module)
Expand Down
3 changes: 3 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
/// into the ASTContext.
ModuleDecl *tryLoadFoundationModule();

/// Returns whether or not the "Foundation" module can be imported, without loading it.
bool canImportFoundationModule();

/// Retrieves the Swift wrapper for the given Clang module, creating
/// it if necessary.
ClangModuleUnit *getWrapperForModule(const clang::Module *underlying,
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/Cxx/class/constructors-objc-irgen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// REQUIRES: CPU=x86_64
// REQUIRES: objc_interop

import Foundation
import ConstructorsObjC

public func createConstructorWithNSArrayParam() -> ConstructorWithNSArrayParam {
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/Cxx/class/constructors-objc-silgen.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/Inputs -enable-cxx-interop -emit-sil %s | %FileCheck %s

// REQUIRES: objc_interop

import Foundation
import ConstructorsObjC

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