-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Proposal to unban weak let bindings #2771
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
nickolas-pohilets
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
nickolas-pohilets:mpokhylets/weak-let
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.
+123
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Feature name | ||
|
||
* Proposal: [SE-NNNN](NNNN-weak-let.md) | ||
* Authors: [Mykola Pokhylets](https://github.com/nickolas-pohilets) | ||
* Review Manager: TBD | ||
* Status: **Awaiting implementation** | ||
* Implementation: [swiftlang/swift#80440](https://github.com/swiftlang/swift/pull/80440) | ||
* Upcoming Feature Flag: `WeakLet` | ||
* Review: ([discussion](https://forums.swift.org/t/weak-captures-in-sendable-sending-closures/78498)) | ||
|
||
## Introduction | ||
|
||
Currently Swift requires weak stored variables to be mutable. | ||
This restriction is rather artificial, and causes friction with sendability checking. | ||
|
||
## Motivation | ||
|
||
Currently swift classes with weak stored properties cannot be `Sendable`, | ||
because weak properties have to be mutable, and mutable properties are | ||
not allowed in `Sendable` classes. | ||
|
||
Similarly, closures with `weak` captures cannot be `@Sendable`, | ||
because such captures are implicitly made mutable. | ||
|
||
Usually developers are not aware of this implicit mutability and have no intention to modify the captured variable. | ||
Implicit mutability of weak captures is inconsistent with `unowned` or default captures. | ||
|
||
Wrapping weak reference into a single-field struct, allows stored properties and captures to be immutable. | ||
|
||
```swift | ||
final class C: Sendable {} | ||
|
||
struct WeakRef { | ||
weak var ref: C? | ||
} | ||
|
||
final class User: Sendable { | ||
weak let ref1: C? // error: 'weak' must be a mutable variable, because it may change at runtime | ||
let ref2: WeakRef // ok | ||
} | ||
|
||
func makeClosure() -> @Sendable () -> Void { | ||
let c = C() | ||
return { [weak c] in | ||
c?.foo() // error: reference to captured var 'c' in concurrently-executing code | ||
c = nil // nobody does this | ||
} | ||
return { [c = WeakRef(ref: c)] in | ||
c.ref?.foo() // ok | ||
} | ||
} | ||
``` | ||
|
||
Existence of this workaround shows that ban on `weak let` variables is artificial, and can be lifted. | ||
|
||
Note that resetting weak references on object destruction is different from regular variable modification. | ||
Resetting on destruction is implemented in a thread-safe manner, and can safely coexist with concurrent reads or writes. | ||
But regular writing to a variable requires exclusive access to that memory location. | ||
|
||
## Proposed solution | ||
|
||
Allow `weak let` declarations for local variables and stored properties. | ||
|
||
Proposal maintains status quo regarding use of `weak` on function arguments and computed properties: | ||
* there is no valid syntax to indicate that function argument is a weak reference; | ||
* `weak` on computed properties is allowed, but has not effect. | ||
|
||
Weak captures are immutable under this proposal. If mutable capture is desired, | ||
mutable variable need to be explicit declared and captured. | ||
|
||
```swift | ||
func makeClosure() -> @Sendable () -> Void { | ||
let c = C() | ||
// Closure is @Sendable | ||
return { [weak c] in | ||
c?.foo() | ||
c = nil // error: cannot assign to value: 'c' is an immutable capture | ||
} | ||
|
||
weak var explicitlyMutable: C? = c | ||
// Closure cannot be @Sendable anymore | ||
return { | ||
explicitlyMutable?.foo() | ||
explicitlyMutable = nil // but assigned is ok | ||
} | ||
} | ||
``` | ||
|
||
## Source compatibility | ||
|
||
Allowing `weak let` bindings is a source-compatible change | ||
that makes previously invalid code valid. | ||
|
||
Treating weak captures as immutable is a source-breaking change. | ||
Any code that attempts to write to the capture will stop compiling. | ||
The overall amount of such code is expected to be small. | ||
|
||
## ABI compatibility | ||
|
||
This is an ABI-compatible change. | ||
|
||
## Implications on adoption | ||
|
||
This feature can be freely adopted and un-adopted in source | ||
code with no deployment constraints and without affecting source or ABI | ||
compatibility. |
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.
I feel like the workaround is more likely to be seen as a bug than a feature to be extended.