Skip to content

Commit 83995f2

Browse files
committed
[Frontend] Don't set "current" compiler version for modules built from swiftinterface
If swiftinterface doesn't have `-swift-compiler-version` flag it means that it was generated with an older version of the Swift compiler. In such cases it would be incorrect to use "current" compiler version because the field is intended to indicate the compiler the swiftinterface was built with.
1 parent 2d8751b commit 83995f2

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,12 @@ bool ArgsToFrontendOptionsConverter::convert(
304304
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
305305
A->getAsString(Args), A->getValue());
306306
}
307-
} else {
307+
// If swiftinterface doesn't have a flag, let's not set the version
308+
// to the current compiler version here. This helps us to identify
309+
// swiftmodules built from swiftinterface generated before introduction
310+
// of `-swift-compiler-version` flag.
311+
} else if (Opts.InputMode !=
312+
FrontendOptions::ParseInputMode::SwiftModuleInterface) {
308313
Opts.SwiftCompilerVersion.tryParse(version::getCompilerVersion());
309314
}
310315

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,8 +1489,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
14891489
if (Invocation.getSILOptions().EnableSerializePackage)
14901490
MainModule->setSerializePackageEnabled();
14911491

1492-
MainModule->setSwiftCompilerVersion(
1493-
Invocation.getFrontendOptions().SwiftCompilerVersion);
1492+
if (auto compilerVersion =
1493+
Invocation.getFrontendOptions().SwiftCompilerVersion) {
1494+
MainModule->setSwiftCompilerVersion(compilerVersion);
1495+
}
14941496

14951497
// Register the main module with the AST context.
14961498
Context->addLoadedModule(MainModule);

lib/Serialization/Serialization.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,10 @@ void Serializer::writeHeader() {
11411141
}
11421142

11431143
llvm::VersionTuple compilerVersion = M->getSwiftCompilerVersion();
1144-
options_block::SwiftCompilerVersionLayout SwiftCompilerVersion(Out);
1145-
SwiftCompilerVersion.emit(ScratchRecord, compilerVersion.getAsString());
1144+
if (compilerVersion) {
1145+
options_block::SwiftCompilerVersionLayout SwiftCompilerVersion(Out);
1146+
SwiftCompilerVersion.emit(ScratchRecord, compilerVersion.getAsString());
1147+
}
11461148

11471149
if (M->isConcurrencyChecked()) {
11481150
options_block::IsConcurrencyCheckedLayout IsConcurrencyChecked(Out);

test/ModuleInterface/swift-compiler-version-option.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/// Build the libraries.
44
// RUN: %target-swift-frontend %s \
5+
// RUN: -module-name Lib \
56
// RUN: -emit-module-path %t/Lib.swiftmodule \
67
// RUN: -emit-module-interface-path %t/Lib.swiftinterface \
78
// RUN: -enable-library-evolution \
@@ -20,6 +21,16 @@
2021
// CHECK-MODULE-OPTION: <SWIFT_COMPILER_VERSION abbrevid={{.*}}/> blob data = '{{.*}}'
2122
// CHECK-MODULE-OPTION: </OPTIONS_BLOCK>
2223

24+
// Drop and rebuilt swiftmodule to make sure that the version is inferred from the interface file.
25+
// RUN: rm %t/Lib.swiftmodule
26+
// RUN: %target-swift-frontend -compile-module-from-interface %t/Lib.swiftinterface -o %t/Lib.swiftmodule -module-name Lib
27+
28+
/// Check option in swiftmodule
29+
// RUN: llvm-bcanalyzer --dump %t/Lib.swiftmodule | %FileCheck --check-prefix=CHECK-REBUILT-MODULE-OPTION %s
30+
// CHECK-REBUILT-MODULE-OPTION: <OPTIONS_BLOCK
31+
// CHECK-REBUILT-MODULE-OPTION: <SWIFT_COMPILER_VERSION abbrevid={{.*}}/> blob data = '{{.*}}'
32+
// CHECK-REBUILT-MODULE-OPTION: </OPTIONS_BLOCK>
33+
2334
public struct S {
2435
public var test: Int = 42
2536
}

0 commit comments

Comments
 (0)