Skip to content

[Sema]Coerce to rvalue when withoutActuallyEscaping receives an lvalue argument #79238

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8217,6 +8217,17 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
// Resolve into a MakeTemporarilyEscapableExpr.
auto *args = apply->getArgs();
assert(args->size() == 2 && "should have two arguments");

auto appliedWrappers =
solution.getAppliedPropertyWrappers(calleeLocator.getAnchor());
auto fnType = cs.getType(fn)->getAs<FunctionType>();
args = coerceCallArguments(
args, fnType, declRef, apply,
locator.withPathElement(ConstraintLocator::ApplyArgument),
appliedWrappers);
if (!args)
return nullptr;

auto *nonescaping = args->getExpr(0);
auto *body = args->getExpr(1);
auto bodyTy = cs.getType(body)->getWithoutSpecifierType();
Expand Down
9 changes: 5 additions & 4 deletions lib/Sema/TypeOfReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ isInvalidPartialApplication(ConstraintSystem &cs,
/// the full opened type and the reference's type.
static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
ConstraintSystem &CS, ConstraintLocator *locator,
DeclTypeCheckingSemantics semantics) {
DeclTypeCheckingSemantics semantics, DeclContext *useDC) {
switch (semantics) {
case DeclTypeCheckingSemantics::Normal:
llvm_unreachable("Decl does not have special type checking semantics!");
Expand Down Expand Up @@ -2261,10 +2261,11 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
0);
FunctionType::Param arg(escapeClosure);
bool isAsync = CS.isAsynchronousContext(useDC);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a problem with this approach because closures are allowed to assume async keyword but ClosureEffectsRequest only checks presence of await so context cannot always tell you at this point exactly whether a closure is async or not when it has withoutActuallyEscaping in its body.

I this the main issue here is that we need to set the bit on an interface type at the point where we don't actually know whether a type that is passed in as the first argument is async or not.

I think we need a special constraint that would allow to form bodyClosure type based on escapeClosureType and the same thing for refType when types are sufficiently resolved...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meanwhile maybe there is a way to workaround in SILGen the fact that there is always a async as a temporary fix?

Copy link
Contributor Author

@stzn stzn Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for my late reply.

I've been exploring potential workarounds in SILGen for the async requirement, but it's challenging to me to find a viable solution for now.

I attempted several approaches, including implementing the following change:
stzn@e07814b

I mainly tried to make types async (e.g. from () -> () to () async -> ()).

While this modification resolved the SIL verification failure for the inout sync pattern, it unfortunately triggered a different assertion failure when running the following code:

func checkAssumeMainActor() {
    MainActor.assumeIsolated {}
}

The resulting error message was:

Assertion failed: ((!F || opTI->isABICompatibleWith(resTI, *F).isCompatible()) && "Can not convert in between ABI incompatible function types"), function create, file SILInstructions.cpp, line 2819.

Despite attempting to make various components async, I haven't been able to resolve this assertion error. Furthermore, I'm uncertain whether my current approach is the correct path forward.

Would you be able to provide some guidance on this matter?

Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xedin
I'd like to hear your thoughts on the workaround direction. Thank you.

auto bodyClosure = FunctionType::get(arg, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(true)
.withAsync(true)
.withAsync(isAsync)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was originally added in #35664

.withThrows(true, thrownError)
.build());
FunctionType::Param args[] = {
Expand All @@ -2275,7 +2276,7 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
auto refType = FunctionType::get(args, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(false)
.withAsync(true)
.withAsync(isAsync)
.withThrows(true, thrownError)
.build());
return {refType, refType, refType, refType, Type()};
Expand Down Expand Up @@ -2370,7 +2371,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
DeclReferenceType declRefType;
if (semantics != DeclTypeCheckingSemantics::Normal) {
declRefType = getTypeOfReferenceWithSpecialTypeCheckingSemantics(
*this, locator, semantics);
*this, locator, semantics, useDC);
} else if (auto baseTy = choice.getBaseType()) {
// Retrieve the type of a reference to the specific declaration choice.
assert(!baseTy->hasTypeParameter());
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/without_actually_escaping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func rethrowThroughWAE(_ zz: (Int, Int, Int) throws -> Int, _ value: Int) throws
}

let _: ((Int) -> Int, (@escaping (Int) -> Int) -> ()) -> () = withoutActuallyEscaping(_:do:)
// expected-error@-1 {{invalid conversion from 'async' function of type '((Int) -> Int, (@escaping (Int) -> Int) async -> ()) async -> ()' to synchronous function type '((Int) -> Int, (@escaping (Int) -> Int) -> ()) -> ()'}}
// expected-error@-1 {{cannot convert value of type '((Swift.Int) -> Swift.Int, (@escaping (Swift.Int) -> Swift.Int) -> ()) -> ()' to specified type '((Swift.Int) -> Swift.Int, (@escaping (Swift.Int) -> Swift.Int) -> ()) -> ()'}}


// Failing to propagate @noescape into non-single-expression
Expand Down
71 changes: 71 additions & 0 deletions test/Sema/without_actually_escaping.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// RUN: %target-typecheck-verify-swift

func noEscapeWAE(f: () -> Void) {
withoutActuallyEscaping(f) { $0() }
}

func escapingWAE(f: @escaping () -> Void) {
withoutActuallyEscaping(f) { $0() }
}

func inoutWAE(f: inout () -> Void) {
withoutActuallyEscaping(f) { $0() }
}

func rethrowThroughInoutWAE(f: inout () throws -> Void) throws {
try withoutActuallyEscaping(f) { try $0() }
}

func consumingWAE(f: consuming @escaping () -> Void) {
withoutActuallyEscaping(f) { $0() }
}

func rethrowThroughConsumingWAE(f: consuming @escaping () throws -> Void) throws {
try withoutActuallyEscaping(f) { try $0() }
}

func sendingEscapingAsyncWAE(f: sending @escaping () async -> Void) async {
await withoutActuallyEscaping(f) { await $0() }
}

func rethrowThroughSendingEscapingAsyncWAE(f: sending @escaping () async throws -> Void) async throws {
try await withoutActuallyEscaping(f) { try await $0() }
}

func sendingNoEscapeAsyncWAE(f: sending () async -> Void) async {
await withoutActuallyEscaping(f) { await $0() }
}

func rethrowThroughSendingNoEscapeAsyncWAE(f: sending () async throws -> Void) async throws {
try await withoutActuallyEscaping(f) { try await $0() }
}

func passNoEscapeClosureViaVarWAE(f: () -> Void) {
var x = f
withoutActuallyEscaping(x) { $0() }
x = {}
}

func passEscapingClosureViaVarWAE(f: @escaping () -> Void) {
var x = f
withoutActuallyEscaping(x) { $0() }
x = {}
}

func passInoutClosureViaVarWAE(f: inout () -> Void) {
var x = f
withoutActuallyEscaping(x) { $0() }
x = {}
}

func passConsumingClosureViaVarWAE(f: consuming @escaping () -> Void) {
var x = f
withoutActuallyEscaping(x) { $0() }
x = {}
}

func passSendingClosureViaVarWAE(f: consuming @escaping () -> Void) {
var x = f
withoutActuallyEscaping(x) { $0() }
x = {}
}