Skip to content

[Sema]: fix @discardableResult interaction with implicit @Sendable conversions #83475

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
Aug 4, 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
21 changes: 20 additions & 1 deletion lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,26 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
: valueE;

if (auto *Fn = dyn_cast<ApplyExpr>(expr)) {
if (auto *calledValue = Fn->getCalledValue()) {
/// Some concurrency-related things may have intermediate conversions that
/// we want to look through, e.g.
///
/// ```swift
/// @discardableResult
/// @MainActor
/// func foo() -> () -> Void {
/// return {}
/// }
///
/// @MainActor
/// func test() {
/// foo()
/// // ^ return value implicitly wapped in a `@Sendable` conversion
/// }
/// ```
/// Attempt to look through function conversions when resolving the
/// called value.
if (auto *calledValue =
Fn->getCalledValue(/*skipFunctionConversions=*/true)) {
if (auto *FD = dyn_cast<AbstractFunctionDecl>(calledValue)) {
if (FD->getAttrs().hasAttribute<DiscardableResultAttr>()) {
isDiscardable = true;
Expand Down
20 changes: 20 additions & 0 deletions test/Concurrency/attr_discardableResult_async_await.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,23 @@ func mainActorAsyncDiscardable() async -> Int { 0 }
func consumesMainActorAsyncDiscardable() async {
await mainActorAsyncDiscardable() // ok
}

// https://github.com/swiftlang/swift/issues/83463

@MainActor
@discardableResult
func returnsDiscardableFunc() -> () -> Void { return {} }

@MainActor
func testDiscardsSyncFuncWithImplicitSendableConversion() {
returnsDiscardableFunc()
}

@MainActor
@discardableResult
func mainActorAsyncReturnsDiscardableFunc() async -> () -> Void { return {} }

@MainActor
func discardsAsyncFuncWithImplicitSendableConversion() async {
await mainActorAsyncReturnsDiscardableFunc()
}