Skip to content

Add StrictConcurrency as an always-enabled experimental feature #66991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ EXPERIMENTAL_FEATURE(ReferenceBindings, false)
/// Enable the explicit 'import Builtin' and allow Builtin usage.
EXPERIMENTAL_FEATURE(BuiltinModule, true)

// Enable strict concurrency.
EXPERIMENTAL_FEATURE(StrictConcurrency, true)

#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3173,6 +3173,10 @@ static bool usesFeatureExistentialAny(Decl *decl) {
return false;
}

static bool usesFeatureStrictConcurrency(Decl *decl) {
return false;
}

static bool usesFeatureImportObjcForwardDeclarations(Decl *decl) {
ClangNode clangNode = decl->getClangNode();
if (!clangNode)
Expand Down
37 changes: 36 additions & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions test/Concurrency/experimental_feature_strictconcurrency.swift
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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}}
}