]> git.lizzy.rs Git - rust.git/commitdiff
Explain the fake read injection better
authorRemy Rakic <remy.rakic@gmail.com>
Fri, 14 Sep 2018 16:07:17 +0000 (18:07 +0200)
committerRemy Rakic <remy.rakic@gmail.com>
Tue, 18 Sep 2018 12:34:51 +0000 (14:34 +0200)
src/librustc_mir/build/matches/mod.rs

index 91d439d430c2ce992eb34f41d775844bda15e994..5c72679800c4428c9e179a4679d7d6939938831f 100644 (file)
@@ -265,9 +265,20 @@ pub fn expr_into_pattern(
                     self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard);
                 unpack!(block = self.into(&place, block, initializer));
 
-                // Inject a fake read of the newly created binding
-                // to test the fallout of fixing issue #53695 where NLL
-                // allows creating unused variables that are effectively unusable.
+
+                // Officially, the semantics of
+                //
+                // `let pattern = <expr>;`
+                //
+                // is that `<expr>` is evaluated into a temporary and then this temporary is
+                // into the pattern.
+                //
+                // However, if we see the simple pattern `let var = <expr>`, we optimize this to
+                // evaluate `<expr>` directly into the variable `var`. This is mostly unobservable,
+                // but in some cases it can affect the borrow checker, as in #53695.
+                // Therefore, we insert a "fake read" here to ensure that we get
+                // appropriate errors.
+                //
                 let source_info = self.source_info(irrefutable_pat.span);
                 self.cfg.push(
                     block,
@@ -318,9 +329,9 @@ pub fn expr_into_pattern(
                     },
                 );
 
-                // Inject a fake read of the newly created binding
-                // to test the fallout of fixing issue #53695 where NLL
-                // allows creating unused variables that are effectively unusable.
+                // Similarly to the `let var = <expr>` case, we insert a "fake read" here to
+                // ensure that we get appropriate errors when this usually unobservable
+                // optimization affects the borrow checker.
                 self.cfg.push(
                     block,
                     Statement {