Skip to content

Commit 41c41a4

Browse files
CodaFiRobert Widmann
authored andcommitted
Restrict Weak-Re-Exports To @_exported Modules
This code got refactored and it accidentally widened the applicable structures for this check. The idea is that you have the following structure // Module A @_weakLinked import B // Module B @_exported import C And the compiler conspires to make it so the modules B AND C wind up weak-linked from module A. The broadened check accidentally allowed the following: // Module A @_weakLinked import B // Module B import C // Oops! Which caused quite a few more modules than were intended to be weak-linked. Restore the `Exported` filter to cut back on the amount of weak re-exports the compiler processes. Resolves rdar://142706779
1 parent 9cbe177 commit 41c41a4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/AST/ImportCache.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,17 @@ ImportCache::getWeakImports(const ModuleDecl *mod) {
428428
ModuleDecl *importedModule = import.module.importedModule;
429429
result.insert(importedModule);
430430

431-
auto reexportedModules = getImportSet(importedModule).getAllImports();
431+
// Only explicit re-exports of a weak-linked module are themselves
432+
// weak-linked.
433+
//
434+
// // Module A
435+
// @_weakLinked import B
436+
//
437+
// // Module B
438+
// @_exported import C
439+
SmallVector<ImportedModule, 4> reexportedModules;
440+
importedModule->getImportedModules(
441+
reexportedModules, ModuleDecl::ImportFilterKind::Exported);
432442
for (auto reexportedModule : reexportedModules) {
433443
result.insert(reexportedModule.importedModule);
434444
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// REQUIRES: objc_interop, OS=macosx
2+
// RUN: %empty-directory(%t)
3+
//
4+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/weaklinked_import_helper.swiftmodule -parse-as-library %S/Inputs/weaklinked_import_helper.swift -enable-library-evolution
5+
//
6+
// RUN: echo 'import Foundation' > %t/intermediate_foundation.swift
7+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -emit-module-path %t/intermediate_foundation.swiftmodule -parse-as-library %t/intermediate_foundation.swift -I %t -enable-library-evolution
8+
//
9+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -primary-file %s -I %t -emit-ir -Xcc -fmodule-map-file=%S/Inputs/weaklinked_import_helper_clang.modulemap | %FileCheck %s
10+
11+
@_weakLinked import intermediate_foundation
12+
import Foundation
13+
14+
// ThisModule -weak imports-> intermediate_foundation -imports-> Foundation
15+
// Because Foundation is _not_ re-exported, make sure any symbols from it are strongly referenced.
16+
// CAUTION: Suppose you _want_ Foundation to be weak-linked. It's not enough to just `@_exported import Foundation`
17+
// in the intermediate_foundation module. That only gets you the Swift half of the Foundation overlay.
18+
19+
// CHECK-DAG: @"OBJC_CLASS_$_NSNotification" = external global %objc_class
20+
_ = NSNotification()

0 commit comments

Comments
 (0)