Skip to content

[Serialization] Allow rebuilding modules from the resource dir on SDK mismatch #64181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,14 +790,18 @@ class ModuleInterfaceLoaderImpl {
UsableModulePath = adjacentMod;
return std::make_error_code(std::errc::not_supported);
} else if (isInResourceDir(adjacentMod) &&
loadMode == ModuleLoadingMode::PreferSerialized) {
loadMode == ModuleLoadingMode::PreferSerialized &&
rebuildInfo.getOrInsertCandidateModule(adjacentMod).serializationStatus !=
serialization::Status::SDKMismatch) {
// Special-case here: If we're loading a .swiftmodule from the resource
// dir adjacent to the compiler, defer to the serialized loader instead
// of falling back. This is mainly to support development of Swift,
// where one might change the module format version but forget to
// recompile the standard library. If that happens, don't fall back
// and silently recompile the standard library -- instead, error like
// we used to.
// Still accept modules built with a different SDK, allowing the use
// of one toolchain against a different SDK.
LLVM_DEBUG(llvm::dbgs() << "Found out-of-date module in the "
"resource-dir at "
<< adjacentMod
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFileSharedCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ ValidationInfo serialization::validateSerializedAST(
requiresOSSAModules, requiresRevisionMatch,
requiredSDK,
extendedInfo, localObfuscator);
if (result.status == Status::Malformed)
if (result.status != Status::Valid)
return result;
} else if ((dependencies || searchPaths) &&
result.status == Status::Valid &&
Expand Down
14 changes: 14 additions & 0 deletions test/Serialization/restrict-swiftmodule-to-sdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
// RUN: -Rmodule-interface-rebuild 2>&1 | %FileCheck %s -check-prefix=CHECK-AvsB-REBUILD
// CHECK-AvsB-REBUILD: remark: rebuilding module 'Lib' from interface

/// Modules loaded from the resource dir are not usually rebuilt from
/// the swiftinterface as it would indicate a configuration problem.
/// Lift that behavior for SDK mismatch and still rebuild them.
// RUN: %empty-directory(%t/cache)
// RUN: %target-swift-frontend -emit-module %t/Lib.swift \
// RUN: -swift-version 5 -enable-library-evolution \
// RUN: -target-sdk-name A -parse-stdlib -module-cache-path %t/cache \
// RUN: -o %t/build -emit-module-interface-path %t/build/Lib.swiftinterface
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_PER_SDK=true \
// RUN: %target-swift-frontend -typecheck %t/Client.swift \
// RUN: -target-sdk-name B -resource-dir %t/build -I %t/build \
// RUN: -parse-stdlib -module-cache-path %t/cache \
// RUN: -Rmodule-interface-rebuild 2>&1 | %FileCheck %s -check-prefix=CHECK-AvsB-REBUILD

// BEGIN Lib.swift
public func foo() {}

Expand Down