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

@@ -509,6 +509,15 @@ static void diagnoseCxxInteropCompatMode(Arg *verArg, ArgList &Args,
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,
DiagnosticEngine &Diags,
const FrontendOptions &FrontendOpts) {
@@ -763,9 +772,33 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
addFutureFeatureIfNotImplied(Feature::BareSlashRegexLiterals);
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
// (non-release) builds for testing purposes.
if (auto feature = getExperimentalFeature(A->getValue())) {
if (auto feature = getExperimentalFeature(value)) {
#ifdef NDEBUG
if (!isFeatureAvailableInProduction(*feature)) {
Diags.diagnose(SourceLoc(),
@@ -928,6 +961,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
} else if (Args.hasArg(OPT_warn_concurrency)) {
Opts.StrictConcurrencyLevel = StrictConcurrency::Complete;
} else if (Opts.hasFeature(Feature::StrictConcurrency)) {
// Already set above.
} else {
// Default to minimal checking in Swift 5.x.
Opts.StrictConcurrencyLevel = StrictConcurrency::Minimal;