Skip to content

Commit 13d61b0

Browse files
committed
Frontend: Require -language-mode option when emitting swiftinterfaces.
If a `.swiftinterface` file does not include an explicit `-language-mode` option (or its predecessor `swift-version`) and needs to be built as a dependency of a client compilation, then the invocation to build the module from interface would end up inheriting the language mode that the client code is built with. This can result in spurious type checking diagnostics or even mis-compilation. To ensure that a module interface is always built using the language mode that its source code was originally built with, require an explicit `-language-mode` option when emitting swiftinterface files. Resolves rdar://145168219.
1 parent 014ed51 commit 13d61b0

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ ERROR(error_load_resolved_plugin,none,
156156
NOTE(note_valid_swift_versions, none,
157157
"valid arguments to '-swift-version' are %0", (StringRef))
158158

159+
ERROR(error_module_interface_requires_language_mode,none,
160+
"emitting module interface files requires '-language-mode'", ())
161+
159162
ERROR(error_mode_cannot_emit_dependencies,none,
160163
"this mode does not support emitting dependency files", ())
161164
ERROR(error_mode_cannot_emit_reference_dependencies,none,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
10341034
}
10351035
if (!isValid)
10361036
diagnoseSwiftVersion(vers, A, Args, Diags);
1037+
} else if (FrontendOpts.InputsAndOutputs.hasModuleInterfaceOutputPath()) {
1038+
Diags.diagnose({}, diag::error_module_interface_requires_language_mode);
1039+
HadError = true;
10371040
}
10381041

10391042
if (auto A = Args.getLastArg(OPT_package_description_version)) {

test/Driver/createCompilerInvocation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
// CHECK-NOWARN-NOT: warning
1010

1111
// RUN: %swift-ide-test_plain -test-createCompilerInvocation \
12-
// RUN: -module-name foo -emit-module -emit-module-path %t/foo.swiftmodule -emit-objc-header -emit-objc-header-path %t/foo.h -enable-library-evolution -emit-module-interface -emit-module-interface-path %t/foo.swiftinterface -emit-library -emit-tbd -emit-tbd-path %t/foo.tbd -emit-dependencies -serialize-diagnostics %s \
12+
// RUN: -swift-version 5 -module-name foo -emit-module -emit-module-path %t/foo.swiftmodule -emit-objc-header -emit-objc-header-path %t/foo.h -enable-library-evolution -emit-module-interface -emit-module-interface-path %t/foo.swiftinterface -emit-library -emit-tbd -emit-tbd-path %t/foo.tbd -emit-dependencies -serialize-diagnostics %s \
1313
// RUN: 2>&1 | %FileCheck %s --check-prefix=NORMAL_ARGS --implicit-check-not="error: "
1414
// NORMAL_ARGS: Frontend Arguments BEGIN
1515
// NORMAL_ARGS-DAG: -o{{$}}
1616
// NORMAL_ARGS-DAG: foo-{{[a-z0-9]+}}.o
1717
// NORMAL_ARGS-DAG: -c{{$}}
18+
// NORMAL_ARGS-DAG: -swift-version
1819
// NORMAL_ARGS-DAG: -module-name
1920
// NORMAL_ARGS-DAG: -emit-module-path
2021
// NORMAL_ARGS-DAG: -emit-module-doc-path
@@ -26,7 +27,7 @@
2627
// NORMAL_ARGS: Frontend Arguments END
2728

2829
// RUN: %swift-ide-test_plain -test-createCompilerInvocation -force-no-outputs \
29-
// RUN: -module-name foo -emit-module -emit-module-path %t/foo.swiftmodule -emit-objc-header -emit-objc-header-path %t/foo.h -enable-library-evolution -emit-module-interface -emit-module-interface-path %t/foo.swiftinterface -emit-library -emit-tbd -emit-tbd-path %t/foo.tbd -emit-dependencies -serialize-diagnostics %s \
30+
// RUN: -swift-version 5 -module-name foo -emit-module -emit-module-path %t/foo.swiftmodule -emit-objc-header -emit-objc-header-path %t/foo.h -enable-library-evolution -emit-module-interface -emit-module-interface-path %t/foo.swiftinterface -emit-library -emit-tbd -emit-tbd-path %t/foo.tbd -emit-dependencies -serialize-diagnostics %s \
3031
// RUN: 2>&1 > %t.nooutput_args
3132
// RUN: %FileCheck %s --check-prefix=NOOUTPUT_ARGS --implicit-check-not="error: " < %t.nooutput_args
3233
// RUN: %FileCheck %s --check-prefix=NOOUTPUT_ARGS_NEG --implicit-check-not="error: " < %t.nooutput_args
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: not %swift_frontend_plain -target %target-swift-5.1-abi-triple %s \
4+
// RUN: -enable-library-evolution -module-name Test \
5+
// RUN: -emit-module-interface-path %t.swiftinterface \
6+
// RUN: -emit-module -o /dev/null 2>&1 | %FileCheck %s
7+
8+
// CHECK: <unknown>:0: error: emitting module interface files requires '-language-mode'
9+
10+
// RUN: %swift_frontend_plain -target %target-swift-5.1-abi-triple %s \
11+
// RUN: -enable-library-evolution -module-name Test \
12+
// RUN: -emit-module-interface-path %t.swiftinterface \
13+
// RUN: -emit-module -o /dev/null -language-mode 5
14+
15+
// RUN: %FileCheck %s --check-prefix CHECK-SWIFTINTERFACE < %t.swiftinterface
16+
17+
// RUN: %swift_frontend_plain -target %target-swift-5.1-abi-triple %s \
18+
// RUN: -enable-library-evolution -module-name Test \
19+
// RUN: -emit-module-interface-path %t.swiftinterface \
20+
// RUN: -emit-module -o /dev/null -swift-version 5
21+
22+
// RUN: %FileCheck %s --check-prefix CHECK-SWIFTINTERFACE < %t.swiftinterface
23+
24+
// CHECK-SWIFTINTERFACE: swift-module-flags:
25+
// CHECK-SWIFTINTERFACE-SAME: -enable-library-evolution
26+
// CHECK-SWIFTINTERFACE-SAME: -module-name Test
27+
// CHECK-SWIFTINTERFACE-SAME: {{-swift-version|-language-mode}} 5

0 commit comments

Comments
 (0)