Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix incorrect double assignment in MIR for while loops
  • Loading branch information
matthewjasper committed Jun 25, 2019
commit 62bec714462129adcc622575d69db558b3750a6e
20 changes: 14 additions & 6 deletions src/librustc_mir/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
loop_block_end = this.as_local_operand(loop_block, cond_expr)
);
body_block = this.cfg.start_new_block();
let term =
TerminatorKind::if_(this.hir.tcx(), cond, body_block, exit_block);
let false_block = this.cfg.start_new_block();
let term = TerminatorKind::if_(
this.hir.tcx(),
cond,
body_block,
false_block,
);
this.cfg.terminate(loop_block_end, source_info, term);

// if the test is false, there's no `break` to assign `destination`, so
// we have to do it; this overwrites any `break`-assigned value but it's
// always `()` anyway
this.cfg
.push_assign_unit(exit_block, source_info, destination);
// we have to do it
this.cfg.push_assign_unit(false_block, source_info, destination);
this.cfg.terminate(
false_block,
source_info,
TerminatorKind::Goto { target: exit_block },
);
} else {
body_block = this.cfg.start_new_block();
let diverge_cleanup = this.diverge_cleanup();
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/nll/assign-while-to-immutable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// We used to incorrectly assign to `x` twice when generating MIR for this
// function, preventing this from compiling.

// check-pass

fn main() {
let x = while false {
break;
};
let y = 'l: while break 'l {};
}