-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix issue where implicit self was unexpectedly not allowed in nested weak self closure in Swift 6 mode #78738
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
Fix issue where implicit self was unexpectedly not allowed in nested weak self closure in Swift 6 mode #78738
Conversation
…weak self closure in Swift 6 mode
doVoidStuff { [weak self] in | ||
doVoidStuff { [self] in | ||
guard let self else { return } | ||
self.x += 1 | ||
} | ||
} |
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.
This looks funny but has been allowed since at least Swift 4.2: https://swiftfiddle.com/irzj67qcezajlj7u5hyq4te2fq
func withEscapingClosure(_ closure: @escaping () -> Void) { closure() }
class Test {
var x = 0
func test() {
withEscapingClosure { [weak self] in
withEscapingClosure { [self] in
self?.x += 1
guard let self = self else { return }
self.x += 1
}
}
}
}
@@ -670,7 +756,7 @@ final class AutoclosureTests { | |||
doVoidStuff { [weak self] in | |||
doVoidStuff { [self] in | |||
guard let self else { return } | |||
method() // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}} |
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.
The explicit self version of this has been supported since at least Swift 4.2 so it seems perfectly fine for the implicit self version to be supported
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.
Yeah this seems reasonable to me, this is the case that came up last time
@@ -670,7 +756,7 @@ final class AutoclosureTests { | |||
doVoidStuff { [weak self] in | |||
doVoidStuff { [self] in | |||
guard let self else { return } | |||
method() // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}} |
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.
Yeah this seems reasonable to me, this is the case that came up last time
…ftlang/swift into cal--fix-nested-weak-self-issue Tag build swift-DEVELOPMENT-SNAPSHOT-2025-01-31-a
02d0e9b
to
f55b571
Compare
actor TestActor { | ||
func setUp() { | ||
doVoidStuff { [weak self] in | ||
Task { [weak self] in | ||
guard let self else { return } | ||
await test() | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
func test() { } | ||
} |
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.
Here's a test case from another issue that was filed recently about this same bug: #79014
3708ef0
to
b648b0c
Compare
b648b0c
to
be0e68e
Compare
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.
Thanks for all the additional tests!
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.
Updated @hamishknight! -- simplified the code like you suggested while also making sure to reject these other cases we were talking about.
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.
Thanks! I think we can further simplify the logic here, but otherwise this is looking good!
…ftlang/swift into cal--fix-nested-weak-self-issue Tag build swift-DEVELOPMENT-SNAPSHOT-2025-02-06-a
b8e9c23
to
14632b7
Compare
Ok should be good now @hamishknight, thanks for the great suggestions as always! |
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.
Very nice, thanks!
@swift-ci please test |
@swift-ci please test source compatibility |
This PR fixes an issue that @harlanhaskins reported where implicit self is unexpectedly rejected in Swift 6 mode:
This is allowed in Swift 5 mode and should continue being supported.
Please review: @hamishknight @xedin @Jumhyn