Skip to content

Commit 62bec71

Browse files
committed
Fix incorrect double assignment in MIR for while loops
1 parent 5f9c044 commit 62bec71

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/librustc_mir/build/expr/into.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
184184
loop_block_end = this.as_local_operand(loop_block, cond_expr)
185185
);
186186
body_block = this.cfg.start_new_block();
187-
let term =
188-
TerminatorKind::if_(this.hir.tcx(), cond, body_block, exit_block);
187+
let false_block = this.cfg.start_new_block();
188+
let term = TerminatorKind::if_(
189+
this.hir.tcx(),
190+
cond,
191+
body_block,
192+
false_block,
193+
);
189194
this.cfg.terminate(loop_block_end, source_info, term);
190195

191196
// if the test is false, there's no `break` to assign `destination`, so
192-
// we have to do it; this overwrites any `break`-assigned value but it's
193-
// always `()` anyway
194-
this.cfg
195-
.push_assign_unit(exit_block, source_info, destination);
197+
// we have to do it
198+
this.cfg.push_assign_unit(false_block, source_info, destination);
199+
this.cfg.terminate(
200+
false_block,
201+
source_info,
202+
TerminatorKind::Goto { target: exit_block },
203+
);
196204
} else {
197205
body_block = this.cfg.start_new_block();
198206
let diverge_cleanup = this.diverge_cleanup();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// We used to incorrectly assign to `x` twice when generating MIR for this
2+
// function, preventing this from compiling.
3+
4+
// check-pass
5+
6+
fn main() {
7+
let x = while false {
8+
break;
9+
};
10+
let y = 'l: while break 'l {};
11+
}

0 commit comments

Comments
 (0)