Skip to content

Commit 2d379b3

Browse files
committed
Fix formatting and add a test for destruction order of unbound values
1 parent dbb655a commit 2d379b3

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
@@ -2190,14 +2190,16 @@ impl<'a> LoweringContext<'a> {
21902190

21912191
let next_ident = self.str_to_ident("next");
21922192
let next_pat = self.pat_ident(e.span, next_ident);
2193-
2193+
21942194
// `::std::option::Option::Some(val) => next = val`
21952195
let pat_arm = {
21962196
let val_ident = self.str_to_ident("val");
21972197
let val_pat = self.pat_ident(e.span, val_ident);
21982198
let val_expr = P(self.expr_ident(e.span, val_ident, val_pat.id));
21992199
let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
2200-
let assign = P(self.expr(e.span, hir::ExprAssign(next_expr, val_expr), ThinVec::new()));
2200+
let assign = P(self.expr(e.span,
2201+
hir::ExprAssign(next_expr, val_expr),
2202+
ThinVec::new()));
22012203
let some_pat = self.pat_some(e.span, val_pat);
22022204
self.arm(hir_vec![some_pat], assign)
22032205
};
@@ -2232,7 +2234,7 @@ impl<'a> LoweringContext<'a> {
22322234
let match_stmt = respan(e.span, hir::StmtExpr(match_expr, self.next_id()));
22332235

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

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

22562263
// `[opt_ident]: loop { ... }`
22572264
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)