Fix formatting and add a test for destruction order of unbound values

This commit is contained in:
John Kåre Alsaker 2017-06-14 13:36:30 +02:00
parent dbb655a1e3
commit 2d379b3393
2 changed files with 39 additions and 4 deletions

View file

@ -2190,14 +2190,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)
};
@ -2232,7 +2234,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,
@ -2251,7 +2253,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),

View file

@ -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);
}