Skip to content

Commit d699e2f

Browse files
committed
[Dependency Scanning] Warn, instead of fail, when a Swift dependency query only finds modules built for incompatible target
We have adopters who are relying on directly importing the underlying Clang module in the presence of incompatible Swift modules. Resolves rdar://162549210
1 parent cb4cb70 commit d699e2f

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

include/swift/Serialization/ScanningLoaders.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace swift {
2222
/// Result of looking up a Swift module on the current filesystem
2323
/// search paths.
2424
struct SwiftModuleScannerQueryResult {
25+
// Checked for by the scanner as a special case
26+
// for downgrading imcompatible-candidate-only lookup result
27+
// to a warning.
28+
static constexpr const char *BUILT_FOR_INCOMPATIBLE_TARGET =
29+
"built for incompatible target";
2530
struct IncompatibleCandidate {
2631
std::string path;
2732
std::string incompatibilityReason;

lib/DependencyScan/ModuleDependencyScanner.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,19 @@ void ModuleDependencyIssueReporter::diagnoseFailureOnOnlyIncompatibleCandidates(
20882088
if (candidates.empty())
20892089
return;
20902090

2091+
// FIXME: There are known cases where clients are relying on
2092+
// loading the underlying Clang module in the presence of a Swift
2093+
// module which is lacking the required target-specific variant,
2094+
// such as MacCatalyst. Eventually, we should pursue making this
2095+
// an error as well.
2096+
if (llvm::all_of(candidates, [](auto &incompatibleCandidate) {
2097+
return incompatibleCandidate.incompatibilityReason ==
2098+
SwiftModuleScannerQueryResult::BUILT_FOR_INCOMPATIBLE_TARGET;
2099+
})) {
2100+
warnOnIncompatibleCandidates(moduleImport.importIdentifier, candidates);
2101+
return;
2102+
}
2103+
20912104
diagnoseModuleNotFoundFailure(moduleImport, cache, dependencyOf,
20922105
/* resolvingSerializedSearchPath */ std::nullopt,
20932106
candidates);

lib/Serialization/ScanningLoaders.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ bool SwiftModuleScanner::handlePossibleTargetMismatch(
117117

118118
for (const auto &modulePath : foundIncompatibleArchModules)
119119
incompatibleCandidates.push_back({modulePath,
120-
"invalid architecture"});
120+
SwiftModuleScannerQueryResult::BUILT_FOR_INCOMPATIBLE_TARGET});
121121

122122
return false;
123123
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/deps)
3+
// RUN: %empty-directory(%t/module-cache)
4+
// RUN: %empty-directory(%t/inputs/Foo.swiftmodule)
5+
// RUN: touch %t/inputs/Foo.swiftmodule/i387.swiftmodule
6+
// RUN: touch %t/inputs/Foo.swiftmodule/ppc65.swiftmodule
7+
// RUN: split-file %s %t
8+
9+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/module-cache %t/client.swift -o %t/deps.json -I %t/inputs -I %t/deps -diagnostic-style llvm -scanner-module-validation 2>&1 | %FileCheck %s
10+
11+
// CHECK-DAG: warning: module file '{{.*}}Foo.swiftmodule{{/|\\}}ppc65.swiftmodule' is incompatible with this Swift compiler: built for incompatible target
12+
// CHECK-DAG: warning: module file '{{.*}}Foo.swiftmodule{{/|\\}}i387.swiftmodule' is incompatible with this Swift compiler: built for incompatible target
13+
// CHECK-NOT: error
14+
15+
//--- deps/foo.h
16+
void foo(void);
17+
18+
//--- deps/module.modulemap
19+
module Foo {
20+
header "foo.h"
21+
export *
22+
}
23+
24+
//--- client.swift
25+
import Foo

test/ScanDependencies/incompatible-arch.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/module-cache %s -o %t/deps.json -I %t/inputs -diagnostic-style llvm -scanner-module-validation 2>&1 | %FileCheck %s
88

9-
// CHECK: error: unable to resolve Swift module dependency to a compatible module: 'Foo'
10-
// CHECK-DAG: note: found incompatible module '{{.*}}Foo.swiftmodule{{/|\\}}ppc65.swiftmodule': invalid architecture
11-
// CHECK-DAG: note: found incompatible module '{{.*}}Foo.swiftmodule{{/|\\}}i387.swiftmodule': invalid architecture
9+
// CHECK-DAG: warning: module file '{{.*}}Foo.swiftmodule{{/|\\}}ppc65.swiftmodule' is incompatible with this Swift compiler: built for incompatible target
10+
// CHECK-DAG: warning: module file '{{.*}}Foo.swiftmodule{{/|\\}}i387.swiftmodule' is incompatible with this Swift compiler: built for incompatible target
11+
// CHECK: error: unable to resolve module dependency: 'Foo'
12+
1213
import Foo

0 commit comments

Comments
 (0)