Skip to content

[Concurrency] Extend @execution(...) attribute support to closures #79975

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 2 commits into from
Mar 13, 2025
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -8343,6 +8343,10 @@ ERROR(attr_execution_only_on_async,none,
"cannot use '@execution' on non-async %kind0",
(ValueDecl *))

ERROR(attr_execution_only_on_async_closure,none,
"cannot use '@execution' on non-async closure",
())

ERROR(attr_execution_type_attr_only_on_async,none,
"cannot use '@execution' on non-async function type",
())
Expand Down
10 changes: 10 additions & 0 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,16 @@ namespace {
return FunctionTypeIsolation::forGlobalActor(actorType);
}

if (auto *execution =
closure->getAttrs().getAttribute<ExecutionAttr>()) {
switch (execution->getBehavior()) {
case ExecutionKind::Caller:
return FunctionTypeIsolation::forNonIsolatedCaller();
case ExecutionKind::Concurrent:
return FunctionTypeIsolation::forNonIsolated();
}
}

return FunctionTypeIsolation::forNonIsolated();
}();
extInfo = extInfo.withIsolation(isolation);
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,12 @@ FunctionType::ExtInfo ClosureEffectsRequest::evaluate(
bool throws = expr->getThrowsLoc().isValid();
bool async = expr->getAsyncLoc().isValid();
bool sendable = expr->getAttrs().hasAttribute<SendableAttr>();

// `@execution(...)` attribute is only valid on asynchronous function types.
if (expr->getAttrs().hasAttribute<ExecutionAttr>()) {
async = true;
}

if (throws || async) {
return ASTExtInfoBuilder()
.withThrows(throws, /*FIXME:*/Type())
Expand Down
38 changes: 38 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8222,6 +8222,44 @@ class ClosureAttributeChecker
// Nothing else to check.
}

void visitExecutionAttr(ExecutionAttr *attr) {
if (!ctx.LangOpts.hasFeature(Feature::ExecutionAttribute)) {
visitDeclAttribute(attr);
return;
}

// `@execution(...)` implies `async`.
if (closure->hasExplicitResultType() &&
closure->getAsyncLoc().isInvalid()) {
ctx.Diags
.diagnose(attr->getLocation(),
diag::attr_execution_only_on_async_closure)
.fixItRemove(attr->getRangeWithAt());
attr->setInvalid();
}

if (auto actorType = getExplicitGlobalActor(closure)) {
ctx.Diags
.diagnose(
attr->getLocation(),
diag::attr_execution_type_attr_incompatible_with_global_isolation,
actorType)
.fixItRemove(attr->getRangeWithAt());
attr->setInvalid();
}

if (auto *paramList = closure->getParameters()) {
if (llvm::any_of(*paramList, [](auto *P) { return P->isIsolated(); })) {
ctx.Diags
.diagnose(
attr->getLocation(),
diag::attr_execution_type_attr_incompatible_with_isolated_param)
.fixItRemove(attr->getRangeWithAt());
attr->setInvalid();
}
}
}

void visitNonisolatedAttr(NonisolatedAttr *attr) {
if (attr->isUnsafe() ||
!ctx.LangOpts.hasFeature(Feature::ClosureIsolation)) {
Expand Down
11 changes: 11 additions & 0 deletions test/Concurrency/attr_execution_conversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ do {
// expected-error@-1 {{cannot convert value of type '(@execution(caller) () async -> ()).Type' to expected argument type '(@isolated(any) () async -> Void).Type'}}
}

do {
let _: () -> Void = { @execution(concurrent) in
// expected-error@-1 {{invalid conversion from 'async' function of type '() async -> Void' to synchronous function type '() -> Void'}}
}

func test(_: () -> Void) {}

test { @execution(caller) in
// expected-error@-1 {{cannot pass function of type '@execution(caller) () async -> ()' to parameter expecting synchronous function type}}
}
}

// Converting to `@execution(caller)` function
class NonSendable {}
Expand Down
18 changes: 18 additions & 0 deletions test/attr/attr_execution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,21 @@ struct InfersMainActor : P {
struct IsolatedType {
@execution(concurrent) func test() async {}
}

_ = { @execution(caller) in // Ok
}

_ = { @execution(concurrent) in // Ok
}

_ = { @MainActor @execution(concurrent) in
// expected-error@-1 {{cannot use '@execution' because function type is isolated to a global actor 'MainActor'}}
}

_ = { @execution(concurrent) () -> Int in
// expected-error@-1 {{'@execution' on non-async closure}}
}

_ = { @execution(caller) (x: isolated (any Actor)?) in
// expected-error@-1 {{cannot use '@execution' together with an isolated parameter}}
}