]> git.lizzy.rs Git - rust.git/commitdiff
Fix formatting and add a test for destruction order of unbound values
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Wed, 14 Jun 2017 11:36:30 +0000 (13:36 +0200)
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Wed, 14 Jun 2017 11:36:30 +0000 (13:36 +0200)
src/librustc/hir/lowering.rs
src/test/run-pass/for-loop-lifetime-of-unbound-values.rs [new file with mode: 0644]

index 1283d136d32877a4c3af437568c0f9aefda8ca5d..f2a434979535d361022c387b225dd0aaa8645f30 100644 (file)
@@ -2190,14 +2190,16 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
 
                 let next_ident = self.str_to_ident("next");
                 let next_pat = self.pat_ident(e.span, next_ident);
-                
+
                 // `::std::option::Option::Some(val) => next = val`
                 let pat_arm = {
                     let val_ident = self.str_to_ident("val");
                     let val_pat = self.pat_ident(e.span, val_ident);
                     let val_expr = P(self.expr_ident(e.span, val_ident, val_pat.id));
                     let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
-                    let assign = P(self.expr(e.span, hir::ExprAssign(next_expr, val_expr), ThinVec::new()));
+                    let assign = P(self.expr(e.span,
+                                             hir::ExprAssign(next_expr, val_expr),
+                                             ThinVec::new()));
                     let some_pat = self.pat_some(e.span, val_pat);
                     self.arm(hir_vec![some_pat], assign)
                 };
@@ -2232,7 +2234,7 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                 let match_stmt = respan(e.span, hir::StmtExpr(match_expr, self.next_id()));
 
                 let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
-                
+
                 // `let next`
                 let next_let = self.stmt_let_pat(e.span,
                     None,
@@ -2251,7 +2253,12 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                 let body_expr = P(self.expr_block(body_block, ThinVec::new()));
                 let body_stmt = respan(e.span, hir::StmtExpr(body_expr, self.next_id()));
 
-                let loop_block = P(self.block_all(e.span, hir_vec![next_let, match_stmt, pat_let, body_stmt], None));
+                let loop_block = P(self.block_all(e.span,
+                                                  hir_vec![next_let,
+                                                           match_stmt,
+                                                           pat_let,
+                                                           body_stmt],
+                                                  None));
 
                 // `[opt_ident]: loop { ... }`
                 let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident),
diff --git a/src/test/run-pass/for-loop-lifetime-of-unbound-values.rs b/src/test/run-pass/for-loop-lifetime-of-unbound-values.rs
new file mode 100644 (file)
index 0000000..a273fb5
--- /dev/null
@@ -0,0 +1,28 @@
+use std::cell::Cell;
+
+struct Flag<'a>(&'a Cell<bool>);
+
+impl<'a> Drop for Flag<'a> {
+       fn drop(&mut self) {
+           self.0.set(false)
+       }
+}
+
+fn main() {
+    let alive2 = Cell::new(true);
+    for _i in std::iter::once(Flag(&alive2)) {
+        // The Flag value should be alive in the for loop body
+        assert_eq!(alive2.get(), true);
+    }
+    // The Flag value should be dead outside of the loop
+    assert_eq!(alive2.get(), false);
+    
+    let alive = Cell::new(true);
+    for _ in std::iter::once(Flag(&alive)) {
+        // The Flag value should be alive in the for loop body even if it wasn't
+        // bound by the for loop
+        assert_eq!(alive.get(), true);
+    }
+    // The Flag value should be dead outside of the loop
+    assert_eq!(alive.get(), false);
+}
\ No newline at end of file