-
Notifications
You must be signed in to change notification settings - Fork 535
Document how closure capturing interacts with discriminant reads #1837
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
meithecatte
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
meithecatte:discriminant-reads
base: master
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
3 commits
Select commit
Hold shift + click to select a range
ab722ef
Document how closure capturing interacts with discriminant reads
meithecatte 6ed9182
Document how range and slice patterns can constitute discriminant reads
meithecatte dff4a64
Don't call things "discriminant reads" just because they behave like …
meithecatte 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 |
---|---|---|
|
@@ -98,7 +98,9 @@ Async closures always capture all input arguments, regardless of whether or not | |
## Capture Precision | ||
|
||
r[type.closure.capture.precision.capture-path] | ||
A *capture path* is a sequence starting with a variable from the environment followed by zero or more place projections that were applied to that variable. | ||
A *capture path* is a sequence starting with a variable from the environment followed by zero or more place projections that were applied to that variable, as well as any [further projections performed by matching against patterns][pattern-wildcards]. | ||
|
||
[pattern-wildcards]: type.closure.capture.precision.wildcard | ||
|
||
r[type.closure.capture.precision.place-projection] | ||
A *place projection* is a [field access], [tuple index], [dereference] (and automatic dereferences), or [array or slice index] expression applied to a variable. | ||
|
@@ -202,7 +204,7 @@ let c = || match x { // x is not captured | |
c(); | ||
``` | ||
|
||
This also includes destructuring of tuples, structs, and enums. | ||
This also includes destructuring of tuples, structs, and single-variant enums. | ||
Fields matched with the [RestPattern] or [StructPatternEtCetera] are also not considered as read, and thus those fields will not be captured. | ||
The following illustrates some of these: | ||
|
||
|
@@ -264,6 +266,114 @@ let c = || { | |
|
||
[wildcard pattern]: ../patterns.md#wildcard-pattern | ||
|
||
r[type.closure.capture.precision.discriminants] | ||
### Capturing for discriminant reads | ||
|
||
If pattern matching requires inspecting a discriminant, the relevant place will get captured by `ImmBorrow`. | ||
|
||
```rust | ||
enum Example { | ||
A(i32), | ||
B(i32), | ||
} | ||
|
||
let mut x = (Example::A(21), 37); | ||
|
||
let c = || match x { // captures `x.0` by ImmBorrow | ||
(Example::A(_), _) => println!("variant A"), | ||
(Example::B(_), _) => println!("variant B"), | ||
}; | ||
x.1 += 1; // x.1 can still be modified | ||
c(); | ||
``` | ||
|
||
r[type.closure.capture.precision.discriminants.single-variant] | ||
Matching against the only variant of an enum does not constitute a discriminant read. | ||
|
||
```rust | ||
enum Example { | ||
A(i32), | ||
} | ||
|
||
let mut x = Example::A(42); | ||
let c = || { | ||
let Example::A(_) = x; // does not capture `x` | ||
}; | ||
x = Example::A(57); // `x` can be modified while the closure is live | ||
c(); | ||
``` | ||
|
||
r[type.closure.capture.precision.discriminants.non-exhaustive] | ||
If [the `#[non_exhaustive]` attribute][non_exhaustive] is applied to an enum defined in an external crate, it is considered to have multiple variants, even if only one variant is actually present. | ||
|
||
[non_exhaustive]: attributes.type-system.non_exhaustive | ||
|
||
r[type.closure.capture.precision.discriminants.uninhabited-variant] | ||
Even if all other variants are uninhabited, the discriminant read still occurs. | ||
|
||
```rust,compile_fail,E0506 | ||
enum Void {} | ||
|
||
enum Example { | ||
A(i32), | ||
B(Void), | ||
} | ||
|
||
let mut x = Example::A(42); | ||
let c = || { | ||
let Example::A(_) = x; // captures `x` by ImmBorrow | ||
}; | ||
x = Example::A(57); // ERROR: cannot assign to `x` because it is borrowed | ||
c(); | ||
``` | ||
|
||
r[type.closure.capture.precision.discriminants.range-patterns] | ||
Matching against a [range pattern][patterns.range] performs a read of the place being matched, causing the closure to borrow it by `ImmBorrow`. This is the case even if the range matches all possible values. | ||
|
||
```rust,compile_fail,E0506 | ||
let mut x = 7_u8; | ||
let c = || { | ||
let 0..=u8::MAX = x; // captures `x` by ImmBorrow | ||
}; | ||
x += 1; // ERROR: cannot assign to `x` because it is borrowed | ||
c(); | ||
``` | ||
|
||
r[type.closure.capture.precision.discriminants.slice-patterns] | ||
Matching against a [slice pattern][patterns.slice] performs a read if the slice pattern needs to inspect the length of the scrutinee. The read will cause the closure to borrow the relevant place by `ImmBorrow`. | ||
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. Maybe say "performs a read of the pointer value" to avoid confusion about what is being read? Should we even say that this reads only the pointer metadata (not sure that's what we're doing today even)? |
||
|
||
```rust,compile_fail,E0506 | ||
let x: &mut [i32] = &mut [1, 2, 3]; | ||
let c = || match x { // captures `*x` by ImmBorrow | ||
[_, _, _] => println!("three elements"), | ||
_ => println!("something else"), | ||
}; | ||
x[0] += 1; // ERROR: cannot assign to `x[_]` because it is borrowed | ||
c(); | ||
``` | ||
|
||
As such, matching against an array doesn't itself cause any borrows, as the lengthh is fixed and doesn't need to be read. | ||
|
||
```rust | ||
let mut x: [i32; 3] = [1, 2, 3]; | ||
let c = || match x { // does not capture `x` | ||
[_, _, _] => println!("three elements, obviously"), | ||
}; | ||
x[0] += 1; // `x` can be modified while the closure is live | ||
c(); | ||
``` | ||
|
||
Likewise, a slice pattern that matches slices of all possible lengths does not constitute a read. | ||
|
||
```rust | ||
let x: &mut [i32] = &mut [1, 2, 3]; | ||
let c = || match x { // does not capture `x` | ||
[..] => println!("always matches"), | ||
}; | ||
x[0] += 1; // `x` can be modified while the closure is live | ||
c(); | ||
``` | ||
|
||
r[type.closure.capture.precision.move-dereference] | ||
### Capturing references in move contexts | ||
|
||
|
Oops, something went wrong.
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.
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.
Unless we mutably access the contents, right? E.g.
Is that already implied by the other rules?
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.
ok yep, that's the "Shared prefix" thing
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.
Yup. Do you think this deserves a clarification? (if so, how would you word it)
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.
no, I think it's clear in the context