Skip to content

Commit 13140df

Browse files
author
Robert Widmann
committed
Weak-Link the Immediate Re-Exports of a Weak-Linked Module
When a module is imported @_weakLinked, its re-exported peer modules have their definitions imported with strong linkage which can often defeat the point of weak linking the parent in the first place. Allow the weak-linkage to float to the immediate re-exports of the @_weakLinked module. Resolves rdar://117166194
1 parent e19954f commit 13140df

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/AST/Module.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,6 +3441,17 @@ bool SourceFile::importsModuleAsWeakLinked(const ModuleDecl *module) const {
34413441
importedModule->getUnderlyingModuleIfOverlay();
34423442
if (module == clangModule)
34433443
return true;
3444+
3445+
// Traverse the exported modules of this weakly-linked module to ensure
3446+
// that we weak-link declarations from its exported peers.
3447+
SmallVector<ImportedModule, 8> reexportedModules;
3448+
importedModule->getImportedModules(reexportedModules,
3449+
ModuleDecl::ImportFilterKind::Exported);
3450+
for (const ImportedModule &reexportedModule : reexportedModules) {
3451+
if (!module->isNonSwiftModule() &&
3452+
module == reexportedModule.importedModule)
3453+
return true;
3454+
}
34443455
}
34453456
return false;
34463457
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %empty-directory(%t)
2+
//
3+
// 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
4+
//
5+
// RUN: echo '@_exported import weaklinked_import_helper' > %t/intermediate.swift
6+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/intermediate.swiftmodule -parse-as-library %t/intermediate.swift -I %t -enable-library-evolution
7+
//
8+
// RUN: %target-swift-frontend -primary-file %s -I %t -emit-ir | %FileCheck %s
9+
10+
// UNSUPPORTED: OS=windows-msvc
11+
12+
@_weakLinked import intermediate
13+
14+
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s24weaklinked_import_helper2fnyyF"()
15+
fn()

0 commit comments

Comments
 (0)