Skip to content

Commit 67115fd

Browse files
committed
Add admonition about pointer vs pointee capturing
There's some surprising nuance to what gets captured when reading the length of a slice with a slice pattern. Let's add an admonition about this.
1 parent 1c9c88b commit 67115fd

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/types/closure.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ r[type.closure.capture.precision.slice-patterns]
378378
r[type.closure.capture.precision.slice-patterns.slices]
379379
Matching a slice against a [slice pattern][patterns.slice] other than one with only a single [rest pattern][patterns.rest] (i.e. `[..]`) reads the length from the wide pointer and captures the pointed-to place by `ImmBorrow`.
380380

381+
> [!NOTE]
382+
> Perhaps surprisingly, even though we read the length from the (wide) *pointer*, it is currently the place of the *pointee* that is captured. However, due to an optimization that reduces the precision of captures, often in practice `rustc` does capture the pointer instead.
383+
>
384+
> For details, see the doc comment on [`truncate_capture_for_optimization`](https://github.com/rust-lang/rust/blob/016baacaf6ed5698625036e3afe3900535015680/compiler/rustc_hir_typeck/src/upvar.rs#L2543-L2572).
385+
381386
```rust,compile_fail,E0502
382387
let x: &mut [u8] = &mut [];
383388
let c = || match x { // Captures `*x` by `ImmBorrow`.

0 commit comments

Comments
 (0)