Skip to content

Commit 8696d97

Browse files
authored
Revert "Ignore transitive ObjC imports when cross-importing"
1 parent 724f8c2 commit 8696d97

File tree

12 files changed

+23
-72
lines changed

12 files changed

+23
-72
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ class ClangModuleLoader : public ModuleLoader {
128128

129129
/// Retrieves the Swift wrapper for the given Clang module, creating
130130
/// it if necessary.
131-
virtual ModuleDecl *
132-
getWrapperForModule(const clang::Module *mod,
133-
bool returnOverlayIfPossible = false) const = 0;
131+
virtual ModuleDecl *getWrapperForModule(const clang::Module *mod) const = 0;
134132

135133
/// Adds a new search path to the Clang CompilerInstance, as if specified with
136134
/// -I or -F.

include/swift/AST/Module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
465465
/// Retrieve the top-level module. If this module is already top-level, this
466466
/// returns itself. If this is a submodule such as \c Foo.Bar.Baz, this
467467
/// returns the module \c Foo.
468-
ModuleDecl *getTopLevelModule(bool overlay = false);
468+
ModuleDecl *getTopLevelModule();
469469

470470
bool isResilient() const {
471471
return getResilienceStrategy() != ResilienceStrategy::Default;

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,7 @@ class ClangImporter final : public ClangModuleLoader {
330330

331331
/// Retrieves the Swift wrapper for the given Clang module, creating
332332
/// it if necessary.
333-
ModuleDecl *
334-
getWrapperForModule(const clang::Module *mod,
335-
bool returnOverlayIfPossible = false) const override;
333+
ModuleDecl *getWrapperForModule(const clang::Module *mod) const override;
336334

337335
std::string getBridgingHeaderContents(StringRef headerPath, off_t &fileSize,
338336
time_t &fileModTime);

lib/AST/Module.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,16 +514,15 @@ SourceLookupCache &ModuleDecl::getSourceLookupCache() const {
514514
return *Cache;
515515
}
516516

517-
ModuleDecl *ModuleDecl::getTopLevelModule(bool overlay) {
517+
ModuleDecl *ModuleDecl::getTopLevelModule() {
518518
// If this is a Clang module, ask the Clang importer for the top-level module.
519519
// We need to check isNonSwiftModule() to ensure we don't look through
520520
// overlays.
521521
if (isNonSwiftModule()) {
522522
if (auto *underlying = findUnderlyingClangModule()) {
523523
auto &ctx = getASTContext();
524524
auto *clangLoader = ctx.getClangModuleLoader();
525-
return clangLoader->getWrapperForModule(underlying->getTopLevelModule(),
526-
overlay);
525+
return clangLoader->getWrapperForModule(underlying->getTopLevelModule());
527526
}
528527
}
529528
// Swift modules don't currently support submodules.

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,13 +1860,8 @@ ModuleDecl *ClangImporter::getImportedHeaderModule() const {
18601860
return Impl.ImportedHeaderUnit->getParentModule();
18611861
}
18621862

1863-
ModuleDecl *
1864-
ClangImporter::getWrapperForModule(const clang::Module *mod,
1865-
bool returnOverlayIfPossible) const {
1866-
auto clangUnit = Impl.getWrapperForModule(mod);
1867-
if (returnOverlayIfPossible && clangUnit->getOverlayModule())
1868-
return clangUnit->getOverlayModule();
1869-
return clangUnit->getParentModule();
1863+
ModuleDecl *ClangImporter::getWrapperForModule(const clang::Module *mod) const {
1864+
return Impl.getWrapperForModule(mod)->getParentModule();
18701865
}
18711866

18721867
PlatformAvailability::PlatformAvailability(LangOptions &langOpts)

lib/Sema/ImportResolution.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,15 @@ class ImportResolver final : public DeclVisitor<ImportResolver> {
172172
/// The list of fully bound imports.
173173
SmallVector<ImportedModuleDesc, 16> boundImports;
174174

175-
/// All imported modules which should be considered when cross-importing.
176-
/// This is basically the transitive import graph, but with only top-level
177-
/// modules and without reexports from Objective-C modules.
175+
/// All imported modules, including by re-exports, and including submodules.
176+
llvm::DenseSet<ImportedModuleDesc> visibleModules;
177+
178+
/// \c visibleModules but without the submodules.
178179
///
179180
/// We use a \c SmallSetVector here because this doubles as the worklist for
180181
/// cross-importing, so we want to keep it in order; this is feasible
181-
/// because this set is usually fairly small.
182+
/// because this set is usually fairly small, while \c visibleModules is
183+
/// often enormous.
182184
SmallSetVector<ImportedModuleDesc, 64> crossImportableModules;
183185

184186
/// The subset of \c crossImportableModules which may declare cross-imports.
@@ -221,7 +223,7 @@ class ImportResolver final : public DeclVisitor<ImportResolver> {
221223

222224
/// Adds \p desc and everything it re-exports to \c visibleModules using
223225
/// the settings from \c desc.
224-
void addCrossImportableModules(ImportedModuleDesc desc);
226+
void addVisibleModules(ImportedModuleDesc desc);
225227

226228
/// * If \p I is a cross-import overlay, registers \p M as overlaying
227229
/// \p I.underlyingModule in \c SF.
@@ -339,7 +341,7 @@ void ImportResolver::bindImport(UnboundImport &&I) {
339341

340342
void ImportResolver::addImport(const UnboundImport &I, ModuleDecl *M) {
341343
auto importDesc = I.makeDesc(M);
342-
addCrossImportableModules(importDesc);
344+
addVisibleModules(importDesc);
343345
boundImports.push_back(importDesc);
344346
}
345347

@@ -934,7 +936,7 @@ static bool isSubmodule(ModuleDecl* M) {
934936
return clangMod && clangMod->Parent;
935937
}
936938

937-
void ImportResolver::addCrossImportableModules(ImportedModuleDesc importDesc) {
939+
void ImportResolver::addVisibleModules(ImportedModuleDesc importDesc) {
938940
// FIXME: namelookup::getAllImports() doesn't quite do what we need (mainly
939941
// w.r.t. scoped imports), but it seems like we could extend it to do so, and
940942
// then eliminate most of this.
@@ -951,34 +953,19 @@ void ImportResolver::addCrossImportableModules(ImportedModuleDesc importDesc) {
951953
nextImport.first))
952954
continue;
953955

954-
// If we are importing a submodule, treat it as though we imported its
955-
// top-level module (or rather, the top-level module's clang overlay if it
956-
// has one).
957-
if (isSubmodule(nextImport.second)) {
958-
nextImport.second =
959-
nextImport.second->getTopLevelModule(/*overlay=*/true);
960-
961-
// If the rewritten import is now for our own parent module, this was an
962-
// import of our own clang submodule in a mixed-language module. We don't
963-
// want to process our own cross-imports.
964-
if (nextImport.second == SF.getParentModule())
965-
continue;
966-
}
967-
968956
// Drop this module into the ImportDesc so we treat it as imported with the
969957
// same options and scope as `I`.
970958
importDesc.module.second = nextImport.second;
971959

972-
// Add it to the list of cross-importable modules. If it's already there,
973-
// we've already done the rest of the work of this loop iteration and can
974-
// skip it.
975-
if (!crossImportableModules.insert(importDesc))
960+
// If we've already imported it, we've also already imported its
961+
// imports.
962+
if (!visibleModules.insert(importDesc).second)
976963
continue;
977964

978-
// We don't consider the re-exports of ObjC modules because ObjC re-exports
979-
// everything, so there isn't enough signal there to work from.
980-
if (nextImport.second->isNonSwiftModule())
981-
continue;
965+
// We don't cross-import submodules, so we shouldn't add them to the
966+
// visibility set. (However, we do consider their reexports.)
967+
if(!isSubmodule(importDesc.module.second))
968+
crossImportableModules.insert(importDesc);
982969

983970
// Add the module's re-exports to worklist.
984971
nextImport.second->getImportedModules(importsWorklist,

test/CrossImport/Inputs/lib-templates/include/core_mi6.h

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/CrossImport/Inputs/lib-templates/include/core_mi6.swiftcrossimport/ThinLibrary.swiftoverlay

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/CrossImport/Inputs/lib-templates/include/module.modulemap

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,3 @@ module ClangLibrary {
44
header "clang_library_submodule.h"
55
}
66
}
7-
module core_mi6 {
8-
header "core_mi6.h"
9-
export *
10-
}
11-
module UniversalExports {
12-
header "universal_exports.h"
13-
export *
14-
}

test/CrossImport/Inputs/lib-templates/include/universal_exports.h

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/CrossImport/Inputs/lib-templates/lib/swift/UniversalExports.swiftinterface

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name UniversalExports
33

44
import Swift
5-
6-
// This is also a clang overlay:
7-
@_exported import UniversalExports
8-
95
@_exported import AlwaysImported
106
import NeverImported
117
import MI6 // @_exported imports NeverImported

test/CrossImport/transitive.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ import UniversalExports
1414
import ThinLibrary
1515
#endif
1616

17-
// We should have loaded the underlying clang module.
18-
fromUniversalExportsClang() // no-error
19-
20-
// We should have loaded core_mi6 too.
21-
fromCoreMI6() // no-error
22-
2317
// We should have loaded _AlwaysImportedOverlay.
2418
fromAlwaysImportedOverlay() // no-error
2519

0 commit comments

Comments
 (0)