Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 52b5362

Browse files
committed
Explain the fake read injection better
1 parent c3c7a5b commit 52b5362

File tree

1 file changed

+17
-6
lines changed
  • src/librustc_mir/build/matches

1 file changed

+17
-6
lines changed

src/librustc_mir/build/matches/mod.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,20 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
265265
self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard);
266266
unpack!(block = self.into(&place, block, initializer));
267267

268-
// Inject a fake read of the newly created binding
269-
// to test the fallout of fixing issue #53695 where NLL
270-
// allows creating unused variables that are effectively unusable.
268+
269+
// Officially, the semantics of
270+
//
271+
// `let pattern = <expr>;`
272+
//
273+
// is that `<expr>` is evaluated into a temporary and then this temporary is
274+
// into the pattern.
275+
//
276+
// However, if we see the simple pattern `let var = <expr>`, we optimize this to
277+
// evaluate `<expr>` directly into the variable `var`. This is mostly unobservable,
278+
// but in some cases it can affect the borrow checker, as in #53695.
279+
// Therefore, we insert a "fake read" here to ensure that we get
280+
// appropriate errors.
281+
//
271282
let source_info = self.source_info(irrefutable_pat.span);
272283
self.cfg.push(
273284
block,
@@ -318,9 +329,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
318329
},
319330
);
320331

321-
// Inject a fake read of the newly created binding
322-
// to test the fallout of fixing issue #53695 where NLL
323-
// allows creating unused variables that are effectively unusable.
332+
// Similarly to the `let var = <expr>` case, we insert a "fake read" here to
333+
// ensure that we get appropriate errors when this usually unobservable
334+
// optimization affects the borrow checker.
324335
self.cfg.push(
325336
block,
326337
Statement {

0 commit comments

Comments
 (0)