Add StrictConcurrency as an always-enabled experimental feature

Upcoming and experimental features are supported via command-line flags
and also in the SwiftPM manifest. Introduce it as an experimental
feature so that it can be enabled via SwiftPM without having to resort
to unsafe flags.

The `StrictConcurrency` experimental feature can also provide a
strictness level in the same manner as `-strict-concurrency`, e.g.,
`StrictConcurrency=targeted`. If the level is not provided, it'll be
`complete`.

Note that we do not introduce this as an "upcoming" feature, because
upcoming features should be in their final "Swift 6" form before
becoming available. We are still tuning the checking for concurrency.
This commit is contained in:
Gwynne Raskind
2023-05-18 01:13:47 -05:00
committed by Doug Gregor
parent 1259125cf1
commit 5ab86d969c
5 changed files with 83 additions and 1 deletions

View File

@@ -210,6 +210,9 @@ EXPERIMENTAL_FEATURE(ReferenceBindings, false)
/// Enable the explicit 'import Builtin' and allow Builtin usage. /// Enable the explicit 'import Builtin' and allow Builtin usage.
EXPERIMENTAL_FEATURE(BuiltinModule, true) EXPERIMENTAL_FEATURE(BuiltinModule, true)
// Enable strict concurrency.
EXPERIMENTAL_FEATURE(StrictConcurrency, true)
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE #undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
#undef EXPERIMENTAL_FEATURE #undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE #undef UPCOMING_FEATURE

View File

@@ -3173,6 +3173,10 @@ static bool usesFeatureExistentialAny(Decl *decl) {
return false; return false;
} }
static bool usesFeatureStrictConcurrency(Decl *decl) {
return false;
}
static bool usesFeatureImportObjcForwardDeclarations(Decl *decl) { static bool usesFeatureImportObjcForwardDeclarations(Decl *decl) {
ClangNode clangNode = decl->getClangNode(); ClangNode clangNode = decl->getClangNode();
if (!clangNode) if (!clangNode)

View File

@@ -509,6 +509,15 @@ static void diagnoseCxxInteropCompatMode(Arg *verArg, ArgList &Args,
diags.diagnose(SourceLoc(), diag::valid_cxx_interop_modes, versStr); diags.diagnose(SourceLoc(), diag::valid_cxx_interop_modes, versStr);
} }
static llvm::Optional<StrictConcurrency>
parseStrictConcurrency(StringRef value) {
return llvm::StringSwitch<llvm::Optional<StrictConcurrency>>(value)
.Case("minimal", StrictConcurrency::Minimal)
.Case("targeted", StrictConcurrency::Targeted)
.Case("complete", StrictConcurrency::Complete)
.Default(llvm::None);
}
static bool ParseLangArgs(LangOptions &Opts, ArgList &Args, static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
DiagnosticEngine &Diags, DiagnosticEngine &Diags,
const FrontendOptions &FrontendOpts) { const FrontendOptions &FrontendOpts) {
@@ -763,9 +772,33 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
addFutureFeatureIfNotImplied(Feature::BareSlashRegexLiterals); addFutureFeatureIfNotImplied(Feature::BareSlashRegexLiterals);
for (const Arg *A : Args.filtered(OPT_enable_experimental_feature)) { for (const Arg *A : Args.filtered(OPT_enable_experimental_feature)) {
// Allow StrictConcurrency to have a value that corresponds to the
// -strict-concurrency=<blah> settings.
StringRef value = A->getValue();
if (value.startswith("StrictConcurrency")) {
auto decomposed = value.split("=");
if (decomposed.first == "StrictConcurrency") {
bool handled;
if (decomposed.second == "") {
Opts.StrictConcurrencyLevel = StrictConcurrency::Complete;
handled = true;
} else if (auto level = parseStrictConcurrency(decomposed.second)) {
Opts.StrictConcurrencyLevel = *level;
handled = true;
} else {
handled = false;
}
if (handled) {
Opts.Features.insert(Feature::StrictConcurrency);
continue;
}
}
}
// If this is a known experimental feature, allow it in +Asserts // If this is a known experimental feature, allow it in +Asserts
// (non-release) builds for testing purposes. // (non-release) builds for testing purposes.
if (auto feature = getExperimentalFeature(A->getValue())) { if (auto feature = getExperimentalFeature(value)) {
#ifdef NDEBUG #ifdef NDEBUG
if (!isFeatureAvailableInProduction(*feature)) { if (!isFeatureAvailableInProduction(*feature)) {
Diags.diagnose(SourceLoc(), Diags.diagnose(SourceLoc(),
@@ -928,6 +961,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
} else if (Args.hasArg(OPT_warn_concurrency)) { } else if (Args.hasArg(OPT_warn_concurrency)) {
Opts.StrictConcurrencyLevel = StrictConcurrency::Complete; Opts.StrictConcurrencyLevel = StrictConcurrency::Complete;
} else if (Opts.hasFeature(Feature::StrictConcurrency)) {
// Already set above.
} else { } else {
// Default to minimal checking in Swift 5.x. // Default to minimal checking in Swift 5.x.
Opts.StrictConcurrencyLevel = StrictConcurrency::Minimal; Opts.StrictConcurrencyLevel = StrictConcurrency::Minimal;

View File

@@ -0,0 +1,22 @@
// RUN: %target-typecheck-verify-swift -enable-experimental-feature StrictConcurrency
// RUN: %target-typecheck-verify-swift -enable-experimental-feature StrictConcurrency=complete
// REQUIRES: concurrency
class C1 { } // expected-note{{class 'C1' does not conform to the 'Sendable' protocol}}
class C2 { }
@available(*, unavailable)
extension C2: Sendable {} // expected-note{{conformance of 'C2' to 'Sendable' has been explicitly marked unavailable here}}
protocol TestProtocol {
associatedtype Value: Sendable
}
struct Test1: TestProtocol { // expected-warning{{type 'Test1.Value' (aka 'C1') does not conform to the 'Sendable' protocol}}
typealias Value = C1
}
struct Test2: TestProtocol { // expected-warning{{conformance of 'C2' to 'Sendable' is unavailable}}
// expected-note@-1{{in associated type 'Self.Value' (inferred as 'C2')}}
typealias Value = C2
}

View File

@@ -0,0 +1,18 @@
// RUN: %target-typecheck-verify-swift -enable-experimental-feature StrictConcurrency=targeted
// REQUIRES: concurrency
class C { // expected-note{{class 'C' does not conform to the 'Sendable' protocol}}
var counter = 0
}
func acceptsSendable<T: Sendable>(_: T) { }
func testNoConcurrency(c: C) {
acceptsSendable(c)
}
@available(SwiftStdlib 5.1, *)
func testConcurrency(c: C) async {
acceptsSendable(c) // expected-warning{{type 'C' does not conform to the 'Sendable' protocol}}
}