Skip to content

Commit 98d51f1

Browse files
committed
Add a code example as comment in init_capture_kind_for_place
1 parent 0b05bc2 commit 98d51f1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

compiler/rustc_hir_typeck/src/upvar.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16901690
//
16911691
// If the data will be moved out of this place, then the place will be truncated
16921692
// at the first Deref in `adjust_for_move_closure` and then moved into the closure.
1693+
//
1694+
// For example:
1695+
//
1696+
// struct Buffer<'a> {
1697+
// x: &'a String,
1698+
// y: Vec<u8>,
1699+
// }
1700+
//
1701+
// fn get<'a>(b: Buffer<'a>) -> impl Sized + 'a {
1702+
// let c = move || b.x;
1703+
// drop(b);
1704+
// c
1705+
// }
1706+
//
1707+
// Even though the closure is declared as move, when we are capturing borrowed data (in
1708+
// this case, *b.x) we prefer to capture by reference.
1709+
// Otherwise you'd get an error in 2021 immediately because you'd be trying to take
1710+
// ownership of the (borrowed) String or else you'd take ownership of b, as in 2018 and
1711+
// before, which is also an error.
16931712
hir::CaptureBy::Value { .. } | hir::CaptureBy::Use { .. }
16941713
if !place.deref_tys().any(Ty::is_ref) =>
16951714
{

0 commit comments

Comments
 (0)