Skip to content

Commit 8d6d9d1

Browse files
Zoxcbrson
authored andcommitted
Fix formatting and add a test for destruction order of unbound values
1 parent 21b276f commit 8d6d9d1

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/librustc/hir/lowering.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,14 +2188,16 @@ impl<'a> LoweringContext<'a> {
21882188

21892189
let next_ident = self.str_to_ident("next");
21902190
let next_pat = self.pat_ident(e.span, next_ident);
2191-
2191+
21922192
// `::std::option::Option::Some(val) => next = val`
21932193
let pat_arm = {
21942194
let val_ident = self.str_to_ident("val");
21952195
let val_pat = self.pat_ident(e.span, val_ident);
21962196
let val_expr = P(self.expr_ident(e.span, val_ident, val_pat.id));
21972197
let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
2198-
let assign = P(self.expr(e.span, hir::ExprAssign(next_expr, val_expr), ThinVec::new()));
2198+
let assign = P(self.expr(e.span,
2199+
hir::ExprAssign(next_expr, val_expr),
2200+
ThinVec::new()));
21992201
let some_pat = self.pat_some(e.span, val_pat);
22002202
self.arm(hir_vec![some_pat], assign)
22012203
};
@@ -2230,7 +2232,7 @@ impl<'a> LoweringContext<'a> {
22302232
let match_stmt = respan(e.span, hir::StmtExpr(match_expr, self.next_id()));
22312233

22322234
let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
2233-
2235+
22342236
// `let next`
22352237
let next_let = self.stmt_let_pat(e.span,
22362238
None,
@@ -2249,7 +2251,12 @@ impl<'a> LoweringContext<'a> {
22492251
let body_expr = P(self.expr_block(body_block, ThinVec::new()));
22502252
let body_stmt = respan(e.span, hir::StmtExpr(body_expr, self.next_id()));
22512253

2252-
let loop_block = P(self.block_all(e.span, hir_vec![next_let, match_stmt, pat_let, body_stmt], None));
2254+
let loop_block = P(self.block_all(e.span,
2255+
hir_vec![next_let,
2256+
match_stmt,
2257+
pat_let,
2258+
body_stmt],
2259+
None));
22532260

22542261
// `[opt_ident]: loop { ... }`
22552262
let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::cell::Cell;
2+
3+
struct Flag<'a>(&'a Cell<bool>);
4+
5+
impl<'a> Drop for Flag<'a> {
6+
fn drop(&mut self) {
7+
self.0.set(false)
8+
}
9+
}
10+
11+
fn main() {
12+
let alive2 = Cell::new(true);
13+
for _i in std::iter::once(Flag(&alive2)) {
14+
// The Flag value should be alive in the for loop body
15+
assert_eq!(alive2.get(), true);
16+
}
17+
// The Flag value should be dead outside of the loop
18+
assert_eq!(alive2.get(), false);
19+
20+
let alive = Cell::new(true);
21+
for _ in std::iter::once(Flag(&alive)) {
22+
// The Flag value should be alive in the for loop body even if it wasn't
23+
// bound by the for loop
24+
assert_eq!(alive.get(), true);
25+
}
26+
// The Flag value should be dead outside of the loop
27+
assert_eq!(alive.get(), false);
28+
}

0 commit comments

Comments
 (0)