Skip to content

Commit c73869e

Browse files
committed
Do not fail the build on only finding incompatible-architecture modules on 'canImport'
This change refactors the module loaders to explicitly take a parameter indicating whether or not the loader is handling a 'canImport' query, in order to avoid emitting an error when finding a dependency Swift binary module with only imcompatible architecture variants present. Resolves rdar://161175498
1 parent 8e96210 commit c73869e

File tree

9 files changed

+56
-51
lines changed

9 files changed

+56
-51
lines changed

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class ExplicitSwiftModuleLoader: public SerializedModuleLoaderBase {
152152
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
153153
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
154154
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
155-
bool skipBuildingInterface, bool isTestableDependencyLookup,
155+
bool isCanImportLookup, bool isTestableDependencyLookup,
156156
bool &isFramework, bool &isSystemModule) override;
157157

158158
std::error_code findModuleFilesInDirectory(
@@ -162,7 +162,7 @@ class ExplicitSwiftModuleLoader: public SerializedModuleLoaderBase {
162162
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
163163
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
164164
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
165-
bool SkipBuildingInterface, bool IsFramework,
165+
bool isCanImportLookup, bool IsFramework,
166166
bool IsTestableDependencyLookup = false) override;
167167

168168
bool canImportModule(ImportPath::Module named, SourceLoc loc,
@@ -201,7 +201,7 @@ class ExplicitCASModuleLoader : public SerializedModuleLoaderBase {
201201
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
202202
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
203203
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
204-
bool skipBuildingInterface, bool isTestableDependencyLookup,
204+
bool isCanImportLookup, bool isTestableDependencyLookup,
205205
bool &isFramework, bool &isSystemModule) override;
206206

207207
std::error_code findModuleFilesInDirectory(
@@ -211,7 +211,7 @@ class ExplicitCASModuleLoader : public SerializedModuleLoaderBase {
211211
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
212212
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
213213
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
214-
bool SkipBuildingInterface, bool IsFramework,
214+
bool IsCanImportLookup, bool IsFramework,
215215
bool IsTestableDependencyLookup = false) override;
216216

217217
bool canImportModule(ImportPath::Module named, SourceLoc loc,
@@ -561,7 +561,7 @@ class ModuleInterfaceLoader : public SerializedModuleLoaderBase {
561561
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
562562
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
563563
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
564-
bool SkipBuildingInterface, bool IsFramework,
564+
bool IsCanImportLookup, bool IsFramework,
565565
bool IsTestableDependencyLookup = false) override;
566566

567567
bool isCached(StringRef DepPath) override;

include/swift/Serialization/ScanningLoaders.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class SwiftModuleScanner : public SerializedModuleLoaderBase {
6363
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
6464
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
6565
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
66-
bool SkipBuildingInterface, bool IsFramework,
66+
bool IsCanImportLookup, bool IsFramework,
6767
bool IsTestableDependencyLookup) override;
6868

6969
bool canImportModule(ImportPath::Module named, SourceLoc loc,

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
103103
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
104104
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
105105
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
106-
bool skipBuildingInterface, bool isTestableDependencyLookup,
106+
bool isCanImportLookup, bool isTestableDependencyLookup,
107107
bool &isFramework, bool &isSystemModule);
108108

109109
/// Attempts to search the provided directory for a loadable serialized
@@ -125,8 +125,8 @@ class SerializedModuleLoaderBase : public ModuleLoader {
125125
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
126126
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
127127
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
128-
bool SkipBuildingInterface, bool IsFramework,
129-
bool isTestableDependencyLookup = false) = 0;
128+
bool IsCanImportLookup, bool IsFramework,
129+
bool IsTestableDependencyLookup = false) = 0;
130130

131131
std::error_code
132132
openModuleFile(
@@ -154,7 +154,8 @@ class SerializedModuleLoaderBase : public ModuleLoader {
154154
virtual bool maybeDiagnoseTargetMismatch(
155155
SourceLoc sourceLocation,
156156
StringRef moduleName,
157-
const SerializedModuleBaseName &BaseName) {
157+
const SerializedModuleBaseName &BaseName,
158+
bool isCanImportLookup) {
158159
return false;
159160
}
160161

@@ -278,13 +279,14 @@ class ImplicitSerializedModuleLoader : public SerializedModuleLoaderBase {
278279
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
279280
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
280281
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
281-
bool SkipBuildingInterface, bool IsFramework,
282-
bool isTestableDependencyLookup = false) override;
282+
bool IsCanImportLookup, bool IsFramework,
283+
bool IsTestableDependencyLookup = false) override;
283284

284285
bool maybeDiagnoseTargetMismatch(
285286
SourceLoc sourceLocation,
286287
StringRef moduleName,
287-
const SerializedModuleBaseName &BaseName) override;
288+
const SerializedModuleBaseName &BaseName,
289+
bool isCanImportLookup) override;
288290

289291
public:
290292
virtual ~ImplicitSerializedModuleLoader();
@@ -336,14 +338,9 @@ class MemoryBufferSerializedModuleLoader : public SerializedModuleLoaderBase {
336338
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
337339
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
338340
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
339-
bool SkipBuildingInterface, bool IsFramework,
341+
bool IsCanImportLookup, bool IsFramework,
340342
bool IsTestableDependencyLookup = false) override;
341343

342-
bool maybeDiagnoseTargetMismatch(
343-
SourceLoc sourceLocation,
344-
StringRef moduleName,
345-
const SerializedModuleBaseName &BaseName) override;
346-
347344
bool BypassResilience;
348345
public:
349346
virtual ~MemoryBufferSerializedModuleLoader();

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,8 +1336,8 @@ std::error_code ModuleInterfaceLoader::findModuleFilesInDirectory(
13361336
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
13371337
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
13381338
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
1339-
bool skipBuildingInterface, bool IsFramework,
1340-
bool isTestableImport) {
1339+
bool IsCanImportLookup, bool IsFramework,
1340+
bool IsTestableImport) {
13411341

13421342
// If running in OnlySerialized mode, ModuleInterfaceLoader
13431343
// should not have been constructed at all.
@@ -1361,11 +1361,9 @@ std::error_code ModuleInterfaceLoader::findModuleFilesInDirectory(
13611361
if (ModuleInterfaceSourcePath)
13621362
ModuleInterfaceSourcePath->assign(InPath->begin(), InPath->end());
13631363

1364-
// If we've been told to skip building interfaces, we are done here and do
1365-
// not need to have the module actually built. For example, if we are
1366-
// currently answering a `canImport` query, it is enough to have found
1367-
// the interface.
1368-
if (skipBuildingInterface) {
1364+
// If we are currently answering a `canImport` query, it is enough to have
1365+
// found the interface.
1366+
if (IsCanImportLookup) {
13691367
if (ModuleInterfacePath)
13701368
ModuleInterfacePath->assign(InPath->begin(), InPath->end());
13711369
return std::error_code();
@@ -2317,7 +2315,7 @@ bool ExplicitSwiftModuleLoader::findModule(
23172315
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
23182316
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
23192317
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
2320-
bool skipBuildingInterface, bool isTestableDependencyLookup,
2318+
bool IsCanImportLookup, bool isTestableDependencyLookup,
23212319
bool &IsFramework, bool &IsSystemModule) {
23222320
// Find a module with an actual, physical name on disk, in case
23232321
// -module-alias is used (otherwise same).
@@ -2396,7 +2394,7 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
23962394
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
23972395
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
23982396
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
2399-
bool skipBuildingInterface, bool IsFramework,
2397+
bool IsCanImportLookup, bool IsFramework,
24002398
bool IsTestableDependencyLookup) {
24012399
llvm_unreachable("Not supported in the Explicit Swift Module Loader.");
24022400
return std::make_error_code(std::errc::not_supported);
@@ -2674,7 +2672,7 @@ bool ExplicitCASModuleLoader::findModule(
26742672
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
26752673
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
26762674
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
2677-
bool skipBuildingInterface, bool isTestableDependencyLookup,
2675+
bool IsCanImportLookup, bool IsTestableDependencyLookup,
26782676
bool &IsFramework, bool &IsSystemModule) {
26792677
// Find a module with an actual, physical name on disk, in case
26802678
// -module-alias is used (otherwise same).
@@ -2763,7 +2761,7 @@ std::error_code ExplicitCASModuleLoader::findModuleFilesInDirectory(
27632761
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
27642762
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
27652763
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
2766-
bool skipBuildingInterface, bool IsFramework,
2764+
bool IsCanImportLookup, bool IsFramework,
27672765
bool IsTestableDependencyLookup) {
27682766
llvm_unreachable("Not supported in the Explicit Swift Module Loader.");
27692767
return std::make_error_code(std::errc::not_supported);

lib/Serialization/ScanningLoaders.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ std::error_code SwiftModuleScanner::findModuleFilesInDirectory(
4646
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
4747
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
4848
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
49-
bool skipBuildingInterface, bool IsFramework,
49+
bool IsCanImportLookup, bool IsFramework,
5050
bool isTestableDependencyLookup) {
5151
using namespace llvm::sys;
5252

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ std::error_code ImplicitSerializedModuleLoader::findModuleFilesInDirectory(
550550
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
551551
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
552552
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
553-
bool skipBuildingInterface, bool IsFramework, bool IsTestableDependencyLookup) {
553+
bool IsCanImportLookup, bool IsFramework, bool IsTestableDependencyLookup) {
554554
if (LoadMode == ModuleLoadingMode::OnlyInterface ||
555555
Ctx.IgnoreAdjacentModules)
556556
return std::make_error_code(std::errc::not_supported);
@@ -579,7 +579,8 @@ std::error_code ImplicitSerializedModuleLoader::findModuleFilesInDirectory(
579579

580580
bool ImplicitSerializedModuleLoader::maybeDiagnoseTargetMismatch(
581581
SourceLoc sourceLocation, StringRef moduleName,
582-
const SerializedModuleBaseName &absoluteBaseName) {
582+
const SerializedModuleBaseName &absoluteBaseName,
583+
bool isCanImportLookup) {
583584
llvm::vfs::FileSystem &fs = *Ctx.SourceMgr.getFileSystem();
584585

585586
// Get the last component of the base name, which is the target-specific one.
@@ -614,9 +615,11 @@ bool ImplicitSerializedModuleLoader::maybeDiagnoseTargetMismatch(
614615
return false;
615616
}
616617

617-
Ctx.Diags.diagnose(sourceLocation, diag::sema_no_import_target, moduleName,
618-
target, foundArchs, dir);
619-
return true;
618+
Ctx.Diags
619+
.diagnose(sourceLocation, diag::sema_no_import_target, moduleName, target,
620+
foundArchs, dir)
621+
.limitBehaviorIf(isCanImportLookup, DiagnosticBehavior::Warning);
622+
return !isCanImportLookup;
620623
}
621624

622625
SerializedModuleBaseName::SerializedModuleBaseName(
@@ -720,7 +723,7 @@ bool SerializedModuleLoaderBase::findModule(
720723
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
721724
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
722725
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
723-
bool skipBuildingInterface, bool isTestableDependencyLookup,
726+
bool isCanImportLookup, bool isTestableDependencyLookup,
724727
bool &isFramework, bool &isSystemModule) {
725728
// Find a module with an actual, physical name on disk, in case
726729
// -module-alias is used (otherwise same).
@@ -766,7 +769,7 @@ bool SerializedModuleLoaderBase::findModule(
766769
auto result = findModuleFilesInDirectory(
767770
moduleID, absoluteBaseName, moduleInterfacePath,
768771
moduleInterfaceSourcePath, moduleBuffer, moduleDocBuffer,
769-
moduleSourceInfoBuffer, skipBuildingInterface, IsFramework,
772+
moduleSourceInfoBuffer, isCanImportLookup, IsFramework,
770773
isTestableDependencyLookup);
771774
if (!result)
772775
return SearchResult::Found;
@@ -780,7 +783,7 @@ bool SerializedModuleLoaderBase::findModule(
780783
// 'std::errc::no_such_file_or_directory'.
781784
if (firstAbsoluteBaseName &&
782785
maybeDiagnoseTargetMismatch(moduleID.Loc, moduleName,
783-
*firstAbsoluteBaseName))
786+
*firstAbsoluteBaseName, isCanImportLookup))
784787
return SearchResult::Error;
785788

786789
return SearchResult::NotFound;
@@ -838,7 +841,7 @@ bool SerializedModuleLoaderBase::findModule(
838841
auto result = findModuleFilesInDirectory(
839842
moduleID, absoluteBaseName, moduleInterfacePath,
840843
moduleInterfaceSourcePath, moduleBuffer, moduleDocBuffer,
841-
moduleSourceInfoBuffer, skipBuildingInterface, isFramework,
844+
moduleSourceInfoBuffer, isCanImportLookup, isFramework,
842845
isTestableDependencyLookup);
843846
if (!result)
844847
return true;
@@ -1512,7 +1515,7 @@ bool SerializedModuleLoaderBase::canImportModule(
15121515
mID, /*moduleInterfacePath=*/nullptr, &moduleInterfaceSourcePath,
15131516
&moduleInputBuffer,
15141517
/*moduleDocBuffer=*/nullptr, /*moduleSourceInfoBuffer=*/nullptr,
1515-
/*skipBuildingInterface=*/true, isTestableDependencyLookup,
1518+
/*isCanImportLookup=*/true, isTestableDependencyLookup,
15161519
isFramework, isSystemModule);
15171520
// If we cannot find the module, don't continue.
15181521
if (!found)
@@ -1607,7 +1610,7 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
16071610
if (!findModule(moduleID, &moduleInterfacePath, &moduleInterfaceSourcePath,
16081611
&moduleInputBuffer, &moduleDocInputBuffer,
16091612
&moduleSourceInfoInputBuffer,
1610-
/*skipBuildingInterface=*/false,
1613+
/*isCanImportLookup=*/false,
16111614
/*isTestableDependencyLookup=*/false,
16121615
isFramework,
16131616
isSystemModule)) {
@@ -1747,7 +1750,7 @@ std::error_code MemoryBufferSerializedModuleLoader::findModuleFilesInDirectory(
17471750
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
17481751
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
17491752
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
1750-
bool skipBuildingInterface, bool IsFramework,
1753+
bool isCanImportLookup, bool IsFramework,
17511754
bool isTestableDependencyLookup) {
17521755
// This is a soft error instead of an llvm_unreachable because this API is
17531756
// primarily used by LLDB which makes it more likely that unwitting changes to
@@ -1756,12 +1759,6 @@ std::error_code MemoryBufferSerializedModuleLoader::findModuleFilesInDirectory(
17561759
return std::make_error_code(std::errc::not_supported);
17571760
}
17581761

1759-
bool MemoryBufferSerializedModuleLoader::maybeDiagnoseTargetMismatch(
1760-
SourceLoc sourceLocation, StringRef moduleName,
1761-
const SerializedModuleBaseName &absoluteBaseName) {
1762-
return false;
1763-
}
1764-
17651762
void SerializedModuleLoaderBase::verifyAllModules() {
17661763
#ifndef NDEBUG
17671764
for (const LoadedModulePair &loaded : LoadedModuleFiles)

test/Parse/ConditionalCompilation/can_import_in_inactive.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#endif
2222

2323
#if canImport(SomeModule)
24-
// CHECK: :[[@LINE-1]]:{{.*}}: error: could not find module 'SomeModule' for target '{{.*}}'; found: i386
24+
// CHECK: :[[@LINE-1]]:{{.*}}: warning: could not find module 'SomeModule' for target '{{.*}}'; found: i386
2525
#endif
2626

2727
import SomeModule
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir %t/Foo.swiftmodule
3+
// RUN: touch %t/Foo.swiftmodule/i387.swiftmodule
4+
// RUN: touch %t/Foo.swiftmodule/ppc65.swiftmodule
5+
// RUN: touch %t/Foo.swiftmodule/i387.swiftdoc
6+
// RUN: touch %t/Foo.swiftmodule/ppc65.swiftdoc
7+
// RUN: %target-swift-frontend %s -typecheck -I %t 2>&1 | %FileCheck %s -DTARGET_ARCHITECTURE=%module-target-triple
8+
9+
// CHECK: {{.*}} warning: could not find module 'Foo' for target '[[TARGET_ARCHITECTURE]]'; found: {{ppc65, i387|i387, ppc65}}, at: {{.*}}Foo.swiftmodule
10+
11+
#if canImport(Foo)
12+
import Foo
13+
#endif

unittests/FrontendTool/ModuleLoadingTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class ModuleInterfaceLoaderTest : public testing::Test {
131131
SerializedModuleBaseName(tempDir, SerializedModuleBaseName("Library")),
132132
/*ModuleInterfacePath=*/nullptr, /*ModuleInterfaceSourcePath=*/nullptr,
133133
&moduleBuffer, &moduleDocBuffer, &moduleSourceInfoBuffer,
134-
/*skipBuildingInterface*/ false, /*IsFramework*/false);
134+
/*isCanImportLookup*/ false, /*IsFramework*/false);
135135
ASSERT_FALSE(error);
136136
ASSERT_FALSE(diags.hadAnyError());
137137

0 commit comments

Comments
 (0)