Skip to content

Commit 4327c73

Browse files
committed
[Serialization] Attempt to load transitive impl-only dependencies on testable imports
Implementation-only dependencies may be referenced from internal decls. When that module is imported as @testable, clients see the internal decls and may fail accessing them if the transitive implementation-only dependencies are not loaded. Let's consider such transtive implementation-only dependencies as optional for @testable imports. As such, the compiler will attempt to load them for test targets, and won't fail if the dependency is missing. We can make these dependencies required for non-public imports, but it could be project breaking to do so for implementation-only dependencies. Considering them as optional is a decent compromise. rdar://79459263
1 parent b4e2969 commit 4327c73

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ ModuleFileSharedCore::getTransitiveLoadingBehavior(
17021702
if (dependency.isImplementationOnly()) {
17031703
// Implementation-only dependencies are not usually loaded from
17041704
// transitive imports.
1705-
if (debuggerMode) {
1705+
if (debuggerMode || forTestable) {
17061706
// In the debugger, try to load the module if possible.
17071707
// Same in the case of a testable import, try to load the dependency
17081708
// but don't fail if it's missing as this could be source breaking.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/// Implementation-only dependencies of modules imported as testable are
2+
/// optional, we attempt to load them but don't error if they are missing.
3+
4+
// RUN: %empty-directory(%t)
5+
// RUN: split-file %s %t
6+
7+
/// Build the hidden implementation-only dependency and the testable module.
8+
// RUN: %target-swift-frontend -emit-module %t/HiddenDep.swift -o %t \
9+
// RUN: -swift-version 5 -enable-library-evolution
10+
// RUN: %target-swift-frontend -emit-module %t/TestedLib.swift -o %t \
11+
// RUN: -swift-version 5 -enable-library-evolution -I %t \
12+
// RUN: -enable-testing
13+
14+
/// A client should ignore the transitive implementation-only dependency for a
15+
/// normal import.
16+
// RUN: %target-swift-frontend -typecheck %t/ClientNormalImport.swift -o %t \
17+
// RUN: -swift-version 5 -I %t -Rmodule-loading 2>&1 | \
18+
// RUN: %FileCheck --check-prefix=CHECK-NOT-LOADED %s
19+
20+
/// A client should load the transitive implementation-only dependency of
21+
/// a module imported @testable.
22+
// RUN: %target-swift-frontend -typecheck %t/ClientTestableImport.swift -o %t \
23+
// RUN: -swift-version 5 -I %t -Rmodule-loading 2>&1 | %FileCheck %s
24+
25+
/// If the dependency is missing we don't fail at loading, but we may still
26+
/// fail later accessing decls missing because of references to the not-loaded module.
27+
// RUN: rm %t/HiddenDep.swiftmodule
28+
29+
/// The transitive dependency is not loaded but a client can still build fine.
30+
// RUN: %target-swift-frontend -typecheck %t/ClientNormalImport.swift -o %t \
31+
// RUN: -swift-version 5 -I %t -Rmodule-loading 2>&1 | \
32+
// RUN: %FileCheck %s --check-prefix=CHECK-NOT-LOADED
33+
34+
/// Clients referencing a decl that depends on the hidden module don't see the
35+
/// decl, it is dropped by deserialization recovery.
36+
// RUN: %target-swift-frontend -typecheck %t/ClientTestableImport.swift -o %t \
37+
// RUN: -swift-version 5 -I %t -verify
38+
39+
//--- HiddenDep.swift
40+
41+
public struct HiddenType {}
42+
43+
//--- TestedLib.swift
44+
45+
@_implementationOnly import HiddenDep
46+
47+
internal func dependsOnHiddenType() -> HiddenType { fatalError() }
48+
49+
//--- ClientNormalImport.swift
50+
51+
/// Note that the import doesn't have to be testable, only the imported module
52+
/// needs to enable testing. We may want to improve upon this in the future.
53+
import TestedLib
54+
// CHECK-NOT-LOADED-NOT: remark: loaded module 'HiddenDep'
55+
56+
//--- ClientTestableImport.swift
57+
58+
@testable import TestedLib
59+
// CHECK: remark: loaded module 'HiddenDep'
60+
61+
let _ = dependsOnHiddenType() // expected-error {{cannot find 'dependsOnHiddenType' in scope}}

0 commit comments

Comments
 (0)