summary refs log tree commit diff
diff options
context:
space:
mode:
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2017-06-14 13:36:30 +0200
committerBrian Anderson <banderson@mozilla.com>2017-06-22 21:26:05 +0000
commit8d6d9d12f1d529c69454289ea6f4d07f074013b4 (patch)
tree99df55b377ab28e8e6d9f92722c629a56263f2ab
parent21b276f32c3a6d6c59c3650d17ded90409fb06b4 (diff)
downloadrust-8d6d9d12f1d529c69454289ea6f4d07f074013b4.tar.gz
rust-8d6d9d12f1d529c69454289ea6f4d07f074013b4.zip
Fix formatting and add a test for destruction order of unbound values
-rw-r--r--src/librustc/hir/lowering.rs15
-rw-r--r--src/test/run-pass/for-loop-lifetime-of-unbound-values.rs28
2 files changed, 39 insertions, 4 deletions
diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs
index 00d42e3cc65..485c41f7feb 100644
--- a/src/librustc/hir/lowering.rs
+++ b/src/librustc/hir/lowering.rs
@@ -2188,14 +2188,16 @@ impl<'a> LoweringContext<'a> {
 
                 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)
                 };
@@ -2230,7 +2232,7 @@ impl<'a> LoweringContext<'a> {
                 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,
@@ -2249,7 +2251,12 @@ impl<'a> LoweringContext<'a> {
                 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
index 00000000000..a273fb579fa
--- /dev/null
+++ b/src/test/run-pass/for-loop-lifetime-of-unbound-values.rs
@@ -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