Skip to content

[Concurrency] Downgrade missing await errors to warnings when referencing @preconcurrency declarations. #70261

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
29 changes: 26 additions & 3 deletions lib/Sema/TypeCheckEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,22 @@ class AbstractFunction {
return decomposeFunction(fn);
}

bool isPreconcurrency() const {
switch (getKind()) {
case Kind::Closure: {
auto *closure = dyn_cast<ClosureExpr>(getClosure());
return closure && closure->isIsolatedByPreconcurrency();
}

case Kind::Function:
return getActorIsolation(getFunction()).preconcurrency();

case Kind::Opaque:
case Kind::Parameter:
return false;
}
}

static AbstractFunction decomposeFunction(Expr *fn) {
assert(fn->getValueProvidingExpr() == fn);

Expand Down Expand Up @@ -1054,8 +1070,11 @@ class ApplyClassifier {
}

/// Whether a missing 'await' error on accessing an async var should be
/// downgraded to a warning. This is only the case for synchronous access
/// to isolated global or static 'let' variables.
/// downgraded to a warning.
///
/// Missing 'await' errors are downgraded for synchronous access to isolated
/// global or static 'let' variables, which was previously accepted in
/// compiler versions before 5.10, or for declarations marked preconcurrency.
bool downgradeAsyncAccessToWarning(Decl *decl) {
if (auto *var = dyn_cast<VarDecl>(decl)) {
ActorReferenceResult::Options options = llvm::None;
Expand All @@ -1067,7 +1086,7 @@ class ApplyClassifier {
}
}

return false;
return decl->preconcurrency();
}

Classification classifyLookup(LookupExpr *E) {
Expand Down Expand Up @@ -1192,6 +1211,10 @@ class ApplyClassifier {
ctx, E->isImplicitlyAsync().has_value(), E->implicitlyThrows(),
PotentialEffectReason::forApply());

// Downgrade missing 'await' errors for preconcurrency references.
result.setDowngradeToWarning(
result.hasAsync() && fnRef.isPreconcurrency());

auto classifyApplyEffect = [&](EffectKind kind) {
if (!fnType->hasEffect(kind) &&
!(kind == EffectKind::Async && E->isImplicitlyAsync()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Sub: Super {

func g2() {
Task.detached {
self.f() // expected-error{{expression is 'async' but is not marked with 'await'}}
self.f() // expected-warning{{expression is 'async' but is not marked with 'await'}}
// expected-note@-1{{calls to instance method 'f()' from outside of its actor context are implicitly asynchronous}}
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/Concurrency/predates_concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func testCalls(x: X) {
}

func testCallsWithAsync() async {
onMainActorAlways() // expected-error{{expression is 'async' but is not marked with 'await'}}
onMainActorAlways() // expected-warning{{expression is 'async' but is not marked with 'await'}}
// expected-note@-1{{calls to global function 'onMainActorAlways()' from outside of its actor context are implicitly asynchronous}}

let _: () -> Void = onMainActorAlways // expected-warning {{converting function value of type '@MainActor () -> ()' to '() -> Void' loses global actor 'MainActor'}}

let c = MyModelClass() // expected-error{{expression is 'async' but is not marked with 'await'}}
let c = MyModelClass() // expected-warning{{expression is 'async' but is not marked with 'await'}}
// expected-note@-1{{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}
c.f() // expected-error{{expression is 'async' but is not marked with 'await'}}
c.f() // expected-warning{{expression is 'async' but is not marked with 'await'}}
// expected-note@-1{{calls to instance method 'f()' from outside of its actor context are implicitly asynchronous}}
}

Expand Down