Skip to content

Commit f976e22

Browse files
committed
fix bug in simplify_cfg with inf. loops
1 parent a61c175 commit f976e22

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/librustc_mir/transform/simplify_cfg.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc::middle::const_eval::ConstVal;
1212
use rustc::middle::ty::TyCtxt;
1313
use rustc::mir::repr::*;
1414
use rustc::mir::transform::{MirPass, Pass};
15+
use pretty;
1516
use syntax::ast::NodeId;
1617

1718
use super::remove_dead_blocks::RemoveDeadBlocks;
@@ -30,16 +31,22 @@ impl SimplifyCfg {
3031
let mut seen: Vec<BasicBlock> = Vec::with_capacity(8);
3132

3233
while mir.basic_block_data(target).statements.is_empty() {
33-
debug!("final_target: target={:?}", target);
34-
match mir.basic_block_data(target).terminator().kind {
35-
TerminatorKind::Goto { target: next } => {
36-
if seen.contains(&next) {
37-
return None;
34+
// NB -- terminator may have been swapped with `None`
35+
// below, in which case we have a cycle and just want
36+
// to stop
37+
if let Some(ref terminator) = mir.basic_block_data(target).terminator {
38+
match terminator.kind {
39+
TerminatorKind::Goto { target: next } => {
40+
if seen.contains(&next) {
41+
return None;
42+
}
43+
seen.push(next);
44+
target = next;
3845
}
39-
seen.push(next);
40-
target = next;
46+
_ => break
4147
}
42-
_ => break
48+
} else {
49+
break
4350
}
4451
}
4552

@@ -106,8 +113,11 @@ impl SimplifyCfg {
106113

107114
impl<'tcx> MirPass<'tcx> for SimplifyCfg {
108115
fn run_pass(&mut self, tcx: &TyCtxt<'tcx>, id: NodeId, mir: &mut Mir<'tcx>) {
116+
let mut counter = 0;
109117
let mut changed = true;
110118
while changed {
119+
pretty::dump_mir(tcx, "simplify_cfg", &counter, id, mir, None);
120+
counter += 1;
111121
changed = self.simplify_branches(mir);
112122
changed |= self.remove_goto_chains(mir);
113123
RemoveDeadBlocks.run_pass(tcx, id, mir);

0 commit comments

Comments
 (0)