-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
stzn
wants to merge
5
commits into
swiftlang:main
Choose a base branch
from
stzn:fix-withoutActuallyEscaping-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7365d09
[Sema]Coerce to rvalue when withoutActuallyEscaping receives an lvalu…
stzn ebca3f8
[Sema]Set isAsync only when it is in async context
stzn 4fd9a44
[Sema]Use coerceCallArguments to make sure that AST is updated correc…
stzn 93122fc
Remove expected-warning
stzn 8108c66
Fix error message
stzn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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!"); | ||
|
@@ -2261,10 +2261,11 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics( | |
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType), | ||
0); | ||
FunctionType::Param arg(escapeClosure); | ||
bool isAsync = CS.isAsynchronousContext(useDC); | ||
auto bodyClosure = FunctionType::get(arg, result, | ||
FunctionType::ExtInfoBuilder() | ||
.withNoEscape(true) | ||
.withAsync(true) | ||
.withAsync(isAsync) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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[] = { | ||
|
@@ -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()}; | ||
|
@@ -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()); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 butClosureEffectsRequest
only checks presence ofawait
so context cannot always tell you at this point exactly whether a closure is async or not when it haswithoutActuallyEscaping
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 onescapeClosureType
and the same thing forrefType
when types are sufficiently resolved...There was a problem hiding this comment.
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?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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:
The resulting error message was:
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.
There was a problem hiding this comment.
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.