Skip to content

Commit 9a1a32c

Browse files
authored
Merge pull request #63639 from xymus/serial-precise-tag
[Serialization] Don't fail the precise tag check if only the last digit doesn't match
2 parents 075150c + a5ccbf3 commit 9a1a32c

File tree

8 files changed

+62
-22
lines changed

8 files changed

+62
-22
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,13 @@ ERROR(serialization_module_too_old,Fatal,
804804
"rebuild %0 and try again: %1",
805805
(Identifier, StringRef))
806806
ERROR(serialization_module_incompatible_revision,Fatal,
807-
"compiled module was created by a different version of the compiler; "
808-
"rebuild %0 and try again: %1",
809-
(Identifier, StringRef))
807+
"compiled module was created by a different version of the compiler '%0'; "
808+
"rebuild %1 and try again: %2",
809+
(StringRef, Identifier, StringRef))
810+
REMARK(serialization_module_problematic_revision, none,
811+
"compiled module was created by a different version of the compiler '%0': "
812+
"%1",
813+
(StringRef, StringRef))
810814
ERROR(serialization_missing_single_dependency,Fatal,
811815
"missing required module '%0'", (StringRef))
812816
ERROR(serialization_missing_dependencies,Fatal,

include/swift/Basic/Version.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,14 @@ StringRef getSwiftRevision();
167167
/// and \c getCurrentCompilerTag returns the version tuple in string format.
168168
bool isCurrentCompilerTagged();
169169

170-
/// Retrieves the distribtion tag of the running compiler, if any.
170+
/// Retrieves the distribution tag of the running compiler, if any.
171171
StringRef getCurrentCompilerTag();
172172

173+
/// Retrieves the distribution tag of the running compiler for serialization,
174+
/// if any. This can hold more information than \c getCurrentCompilerTag
175+
/// depending on the vendor.
176+
StringRef getCurrentCompilerSerializationTag();
177+
173178
} // end namespace version
174179
} // end namespace swift
175180

include/swift/Serialization/Validation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct ValidationInfo {
9191
version::Version compatibilityVersion = {};
9292
llvm::VersionTuple userModuleVersion;
9393
StringRef sdkName = {};
94+
StringRef problematicRevision = {};
9495
size_t bytes = 0;
9596
Status status = Status::Malformed;
9697
std::vector<StringRef> allowableClients;

lib/Basic/Version.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,5 +297,13 @@ StringRef getCurrentCompilerTag() {
297297
#endif
298298
}
299299

300+
StringRef getCurrentCompilerSerializationTag() {
301+
#ifdef SWIFT_COMPILER_VERSION
302+
return SWIFT_COMPILER_VERSION;
303+
#else
304+
return StringRef();
305+
#endif
306+
}
307+
300308
} // end namespace version
301309
} // end namespace swift

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ static ValidationInfo validateControlBlock(
356356
// Disable this restriction for compiler testing by setting this
357357
// env var to any value.
358358
static const char* ignoreRevision =
359-
::getenv("SWIFT_DEBUG_IGNORE_SWIFTMODULE_REVISION");
359+
::getenv("SWIFT_IGNORE_SWIFTMODULE_REVISION");
360360
if (ignoreRevision)
361361
break;
362362

@@ -370,12 +370,20 @@ static ValidationInfo validateControlBlock(
370370
if (forcedDebugRevision ||
371371
(requiresRevisionMatch && version::isCurrentCompilerTagged())) {
372372
StringRef compilerRevision = forcedDebugRevision ?
373-
forcedDebugRevision : version::getCurrentCompilerTag();
373+
forcedDebugRevision : version::getCurrentCompilerSerializationTag();
374374
if (moduleRevision != compilerRevision) {
375-
result.status = Status::RevisionIncompatible;
375+
// The module versions are mismatching, record it and diagnose later.
376+
result.problematicRevision = moduleRevision;
376377

377-
// We can't trust this module format at this point.
378-
return result;
378+
// Reject the module only it still mismatches without the last digit.
379+
StringRef compilerRevisionHead = compilerRevision.rsplit('.').first;
380+
StringRef moduleRevisionHead = moduleRevision.rsplit('.').first;
381+
if (moduleRevisionHead != compilerRevisionHead) {
382+
result.status = Status::RevisionIncompatible;
383+
384+
// We can't trust the module format at this point.
385+
return result;
386+
}
379387
}
380388
}
381389
break;

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ void Serializer::writeHeader(const SerializationOptions &options) {
10111011
static const char* forcedDebugRevision =
10121012
::getenv("SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION");
10131013
auto revision = forcedDebugRevision ?
1014-
forcedDebugRevision : version::getCurrentCompilerTag();
1014+
forcedDebugRevision : version::getCurrentCompilerSerializationTag();
10151015
Revision.emit(ScratchRecord, revision);
10161016

10171017
IsOSSA.emit(ScratchRecord, options.IsOSSA);

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,13 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
845845
if (loadedModuleFile &&
846846
loadedModuleFile->mayHaveDiagnosticsPointingAtBuffer())
847847
OrphanedModuleFiles.push_back(std::move(loadedModuleFile));
848+
} else {
849+
// Report non-fatal compiler tag mismatch.
850+
if (!loadInfo.problematicRevision.empty()) {
851+
Ctx.Diags.diagnose(*diagLoc,
852+
diag::serialization_module_problematic_revision,
853+
loadInfo.problematicRevision, moduleBufferID);
854+
}
848855
}
849856

850857
// The -experimental-hermetic-seal-at-link flag turns on dead-stripping
@@ -905,7 +912,7 @@ void swift::serialization::diagnoseSerializedASTLoadFailure(
905912
break;
906913
case serialization::Status::RevisionIncompatible:
907914
Ctx.Diags.diagnose(diagLoc, diag::serialization_module_incompatible_revision,
908-
ModuleName, moduleBufferID);
915+
loadInfo.problematicRevision, ModuleName, moduleBufferID);
909916
break;
910917
case serialization::Status::Malformed:
911918
Ctx.Diags.diagnose(diagLoc, diag::serialization_malformed_module,

test/Serialization/restrict-swiftmodule-to-revision.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public func foo() {}
99
/// Build Lib as a resilient and non-resilient swiftmodule
1010
// RUN: %target-swift-frontend -emit-module %t/Lib.swift -swift-version 5 -o %t/build -parse-stdlib -module-cache-path %t/cache -module-name ResilientLib -enable-library-evolution
1111
// RUN: %target-swift-frontend -emit-module %t/Lib.swift -swift-version 5 -o %t/build -parse-stdlib -module-cache-path %t/cache -module-name NonResilientLib
12-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=my-revision \
12+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 \
1313
// RUN: %target-swift-frontend -emit-module %t/Lib.swift -swift-version 5 -o %t/build -parse-stdlib -module-cache-path %t/cache -module-name TaggedLib -enable-library-evolution
1414

1515

@@ -19,9 +19,9 @@ import NonResilientLib
1919
foo()
2020

2121
/// Building a NonResilientLib client should reject the import for a tagged compiler
22-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=my-revision \
22+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 \
2323
// RUN: not %target-swift-frontend -typecheck %t/NonResilientClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache 2>&1 | %FileCheck -check-prefix=CHECK-NON-RESILIENT %s
24-
// CHECK-NON-RESILIENT: compiled module was created by a different version of the compiler; rebuild 'NonResilientLib' and try again: {{.*}}NonResilientLib.swiftmodule
24+
// CHECK-NON-RESILIENT: compiled module was created by a different version of the compiler ''; rebuild 'NonResilientLib' and try again: {{.*}}NonResilientLib.swiftmodule
2525

2626

2727
/// 3. Test importing the resilient untagged library
@@ -33,12 +33,12 @@ foo()
3333
// RUN: %target-swift-frontend -typecheck %t/ResilientClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache
3434

3535
/// Building a ResilientLib client should reject the import for a tagged compiler
36-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=not-a-revision \
36+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 \
3737
// RUN: not %target-swift-frontend -typecheck %t/ResilientClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache 2>&1 | %FileCheck %s
38-
// CHECK: compiled module was created by a different version of the compiler; rebuild 'ResilientLib' and try again: {{.*}}ResilientLib.swiftmodule
38+
// CHECK: compiled module was created by a different version of the compiler ''; rebuild 'ResilientLib' and try again: {{.*}}ResilientLib.swiftmodule
3939

40-
/// Building a ResilientLib client should succeed for a tagged compiler with SWIFT_DEBUG_IGNORE_SWIFTMODULE_REVISION
41-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=not-a-revision SWIFT_DEBUG_IGNORE_SWIFTMODULE_REVISION=true \
40+
/// Building a ResilientLib client should succeed for a tagged compiler with SWIFT_IGNORE_SWIFTMODULE_REVISION
41+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 SWIFT_IGNORE_SWIFTMODULE_REVISION=true \
4242
// RUN: %target-swift-frontend -typecheck %t/ResilientClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache
4343

4444

@@ -47,12 +47,19 @@ foo()
4747
import TaggedLib
4848
foo()
4949

50-
/// Importing TaggedLib should succeed with the same tag or a dev compiler
51-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=my-revision \
50+
/// Importing TaggedLib should succeed with the same tag.
51+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.1 \
5252
// RUN: %target-swift-frontend -typecheck %t/TaggedClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache
53+
54+
/// Importing TaggedLib should succeed with a dev compiler
5355
// RUN: %target-swift-frontend -typecheck %t/TaggedClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache
5456

57+
/// Importing TaggedLib should succeed but remark on a last digit difference.
58+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.0.2 \
59+
// RUN: %target-swift-frontend -typecheck %t/TaggedClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache 2>&1 | %FileCheck %s --check-prefix=CHECK-LAST-DIGIT
60+
// CHECK-LAST-DIGIT: remark: compiled module was created by a different version of the compiler '1.0.0.0.1': {{.*}}TaggedLib.swiftmodule
61+
5562
/// Building a TaggedLib client should reject the import for a different tagged compiler
56-
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=not-a-revision \
63+
// RUN: env SWIFT_DEBUG_FORCE_SWIFTMODULE_REVISION=1.0.0.1.1 \
5764
// RUN: not %target-swift-frontend -typecheck %t/TaggedClient.swift -swift-version 5 -I %t/build -parse-stdlib -module-cache-path %t/cache 2>&1 | %FileCheck %s --check-prefix=CHECK-TAGGED
58-
// CHECK-TAGGED: compiled module was created by a different version of the compiler; rebuild 'TaggedLib' and try again: {{.*}}TaggedLib.swiftmodule
65+
// CHECK-TAGGED: error: compiled module was created by a different version of the compiler '1.0.0.0.1'; rebuild 'TaggedLib' and try again: {{.*}}TaggedLib.swiftmodule

0 commit comments

Comments
 (0)