Skip to content

[CSGen] Prevent @concurrent on closures from skipping throws infe… #81549

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 1 commit into from
May 16, 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
13 changes: 7 additions & 6 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,11 +1414,6 @@ FunctionType::ExtInfo ClosureEffectsRequest::evaluate(
bool async = expr->getAsyncLoc().isValid();
bool sendable = expr->getAttrs().hasAttribute<SendableAttr>();

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

if (throws || async) {
return ASTExtInfoBuilder()
.withThrows(throws, /*FIXME:*/Type())
Expand All @@ -1432,11 +1427,17 @@ FunctionType::ExtInfo ClosureEffectsRequest::evaluate(
if (!body)
return ASTExtInfoBuilder().withSendable(sendable).build();

// `@concurrent` attribute is only valid on asynchronous function types.
bool asyncFromAttr = false;
if (expr->getAttrs().hasAttribute<ConcurrentAttr>()) {
asyncFromAttr = true;
}

auto throwFinder = FindInnerThrows(expr);
body->walk(throwFinder);
return ASTExtInfoBuilder()
.withThrows(throwFinder.foundThrow(), /*FIXME:*/Type())
.withAsync(bool(findAsyncNode(expr)))
.withAsync(asyncFromAttr || bool(findAsyncNode(expr)))
.withSendable(sendable)
.build();
}
Expand Down
21 changes: 21 additions & 0 deletions test/attr/execution_behavior_attrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,24 @@ _ = { @MainActor @concurrent in
_ = { @concurrent () -> Int in
// expected-error@-1 {{@concurrent on non-async closure}}
}

// Make sure that explicit use of `@concurrent` doesn't interfere with inference of `throws` from the body.
do {
func acceptsThrowing(_ body: () async throws -> Void) async {
}

struct Invocation {
func throwingFn() async throws {
}
}

func test(invocation: Invocation) async {
await acceptsThrowing({ @concurrent in try await invocation.throwingFn() }) // Ok
await acceptsThrowing({ @concurrent [invocation] in try await invocation.throwingFn() }) // Ok

await acceptsThrowing({ @concurrent in // Ok
_ = 42
try await invocation.throwingFn()
})
}
}