Skip to content

Commit fb59f1f

Browse files
committed
Frontend: Enable @_spiOnly by default in Swift 6
Enabling `@_spiOnly` also enables stronger type-checking of SPI decls. As this could be source breaking, it has always been opt-in. Turn it on by default in Swift 6 mode where the stronger type-checking will also become expected. At the same time, disable the old alternative to `@_spiOnly` which was designed to be compatible with old compilers. Any user of that feature should move to `@_spiOnly` or `package import` instead.
1 parent e1c04c9 commit fb59f1f

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ ERROR(error_unknown_library_level, none,
4444
"unknown library level '%0', "
4545
"expected one of 'api', 'spi', 'ipi', or 'other'", (StringRef))
4646

47+
ERROR(error_old_spi_only_import_unsupported, none,
48+
"'-experimental-spi-imports' is unsupported in Swift 6, "
49+
"use '@_spiOnly' instead",
50+
())
51+
4752
ERROR(error_unknown_require_explicit_availability, none,
4853
"unknown argument '%0', passed to -require-explicit-availability, "
4954
"expected 'error', 'warn' or 'ignore'",

lib/Frontend/CompilerInvocation.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,17 @@ bool CompilerInvocation::parseArgs(
33373337
}
33383338
}
33393339

3340+
// With Swift 6, enable @_spiOnly by default and forbid the old version.
3341+
// This also enables proper error reporting of ioi references from spi decls.
3342+
if (LangOpts.EffectiveLanguageVersion.isVersionAtLeast(6)) {
3343+
LangOpts.EnableSPIOnlyImports = true;
3344+
3345+
if (ParsedArgs.hasArg(OPT_experimental_spi_imports)) {
3346+
Diags.diagnose(SourceLoc(), diag::error_old_spi_only_import_unsupported);
3347+
return true;
3348+
}
3349+
}
3350+
33403351
return false;
33413352
}
33423353

test/SPI/experimental_spi_imports_swiftinterface.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
// RUN: %FileCheck -check-prefix=CHECK-PUBLIC %s < %t/main.swiftinterface
1717
// RUN: %FileCheck -check-prefix=CHECK-PRIVATE %s < %t/main.private.swiftinterface
1818

19+
/// The flag is rejected in Swift 6.
20+
// RUN: not %target-swift-frontend -typecheck % -swift-version 6 \
21+
// RUN: -experimental-spi-imports 2>&1 | %FileCheck %s -check-prefix=CHECK-6
22+
// CHECK-6: error: '-experimental-spi-imports' is unsupported in Swift 6, use '@_spiOnly' instead
23+
1924
@_spi(dummy) @_implementationOnly import ExperimentalImported
2025
// CHECK-PUBLIC-NOT: import ExperimentalImported
2126
// CHECK-PRIVATE: @_implementationOnly @_spi{{.*}} import ExperimentalImported

test/SPI/spi_only_import_flag_check.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
// RUN: -module-name Client -emit-module-path %t/Client.swiftmodule \
1717
// RUN: -experimental-spi-only-imports
1818

19+
/// Attribute is accepted without the flag in Swift 6.
20+
// RUN: %target-swift-frontend -emit-module %t/Client.swift -I %t \
21+
// RUN: -module-name Client -emit-module-path %t/Client.swiftmodule \
22+
// RUN: -swift-version 6
23+
1924
/// Attribute is accepted without the flag when in a swiftinterface.
2025
// RUN: %target-swift-typecheck-module-from-interface(%t/Client.private.swiftinterface) \
2126
// RUN: -I %t -module-name Client

0 commit comments

Comments
 (0)