Skip to content

Commit dff4a64

Browse files
committed
Don't call things "discriminant reads" just because they behave like ones
1 parent 6ed9182 commit dff4a64

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/types/closure.md

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ Async closures always capture all input arguments, regardless of whether or not
9898
## Capture Precision
9999

100100
r[type.closure.capture.precision.capture-path]
101-
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
102-
any [further projections performed by matching against patterns][pattern-wildcards].
101+
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].
103102

104103
[pattern-wildcards]: type.closure.capture.precision.wildcard
105104

@@ -305,9 +304,7 @@ c();
305304
```
306305

307306
r[type.closure.capture.precision.discriminants.non-exhaustive]
308-
If [the `#[non_exhaustive]` attribute][non_exhaustive] is applied to an enum
309-
defined in an external crate, it is considered to have multiple variants,
310-
even if only one variant is actually present.
307+
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.
311308

312309
[non_exhaustive]: attributes.type-system.non_exhaustive
313310

@@ -331,8 +328,7 @@ c();
331328
```
332329

333330
r[type.closure.capture.precision.discriminants.range-patterns]
334-
Matching against a [range pattern][patterns.range] constitutes a discriminant read, even if
335-
the range matches all possible values.
331+
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.
336332

337333
```rust,compile_fail,E0506
338334
let mut x = 7_u8;
@@ -344,11 +340,10 @@ c();
344340
```
345341

346342
r[type.closure.capture.precision.discriminants.slice-patterns]
347-
Matching against a [slice pattern][patterns.slice] constitutes a discriminant read if
348-
the slice pattern needs to inspect the length of the scrutinee.
343+
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`.
349344

350345
```rust,compile_fail,E0506
351-
let mut x: &mut [i32] = &mut [1, 2, 3];
346+
let x: &mut [i32] = &mut [1, 2, 3];
352347
let c = || match x { // captures `*x` by ImmBorrow
353348
[_, _, _] => println!("three elements"),
354349
_ => println!("something else"),
@@ -357,7 +352,7 @@ x[0] += 1; // ERROR: cannot assign to `x[_]` because it is borrowed
357352
c();
358353
```
359354

360-
Thus, matching against an array doesn't constitute a discriminant read, as the length is fixed.
355+
As such, matching against an array doesn't itself cause any borrows, as the lengthh is fixed and doesn't need to be read.
361356

362357
```rust
363358
let mut x: [i32; 3] = [1, 2, 3];
@@ -368,10 +363,10 @@ x[0] += 1; // `x` can be modified while the closure is live
368363
c();
369364
```
370365

371-
Likewise, a slice pattern that matches slices of all possible lengths does not constitute a discriminant read.
366+
Likewise, a slice pattern that matches slices of all possible lengths does not constitute a read.
372367

373368
```rust
374-
let mut x: &mut [i32] = &mut [1, 2, 3];
369+
let x: &mut [i32] = &mut [1, 2, 3];
375370
let c = || match x { // does not capture `x`
376371
[..] => println!("always matches"),
377372
};

0 commit comments

Comments
 (0)