Skip to content

Commit 67d1cf1

Browse files
Ariel Ben-Yehudaarielb1
authored andcommitted
introduce an early pass to clear dead blocks
this makes the the MIR assignment pass complete successfully
1 parent 999f176 commit 67d1cf1

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

src/librustc_mir/mir_map.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ extern crate rustc_front;
2222
use build;
2323
use graphviz;
2424
use pretty;
25-
use transform::{simplify_cfg, type_check, no_landing_pads};
25+
use transform::{clear_dead_blocks, simplify_cfg, type_check};
26+
use transform::{no_landing_pads};
2627
use rustc::dep_graph::DepNode;
2728
use rustc::mir::repr::Mir;
2829
use hair::cx::Cx;
@@ -148,6 +149,7 @@ impl<'a, 'm, 'tcx> Visitor<'tcx> for InnerDump<'a,'m,'tcx> {
148149

149150
match build_mir(Cx::new(&infcx), implicit_arg_tys, id, span, decl, body) {
150151
Ok(mut mir) => {
152+
clear_dead_blocks::ClearDeadBlocks::new().run_on_mir(&mut mir, self.tcx);
151153
type_check::TypeckMir::new(&infcx).run_on_mir(&mut mir, self.tcx);
152154
no_landing_pads::NoLandingPads.run_on_mir(&mut mir, self.tcx);
153155
if self.tcx.sess.opts.mir_opt_level > 0 {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! A pass that erases the contents of dead blocks. This is required
12+
//! because rustc allows for ill-typed block terminators in dead
13+
//! blocks.
14+
//!
15+
//! This pass does not renumber or remove the blocks, to have the
16+
//! MIR better match the source.
17+
18+
use rustc::middle::ty;
19+
use rustc::mir::repr::*;
20+
use rustc::mir::transform::MirPass;
21+
22+
pub struct ClearDeadBlocks;
23+
24+
impl ClearDeadBlocks {
25+
pub fn new() -> ClearDeadBlocks {
26+
ClearDeadBlocks
27+
}
28+
29+
fn clear_dead_blocks(&self, mir: &mut Mir) {
30+
let mut seen = vec![false; mir.basic_blocks.len()];
31+
32+
// These blocks are always required.
33+
seen[START_BLOCK.index()] = true;
34+
seen[END_BLOCK.index()] = true;
35+
36+
let mut worklist = vec![START_BLOCK];
37+
while let Some(bb) = worklist.pop() {
38+
for succ in mir.basic_block_data(bb).terminator().successors().iter() {
39+
if !seen[succ.index()] {
40+
seen[succ.index()] = true;
41+
worklist.push(*succ);
42+
}
43+
}
44+
}
45+
46+
for (block, seen) in mir.basic_blocks.iter_mut().zip(seen) {
47+
if !seen {
48+
*block = BasicBlockData {
49+
statements: vec![],
50+
terminator: Some(Terminator::Return),
51+
is_cleanup: false
52+
};
53+
}
54+
}
55+
}
56+
}
57+
58+
impl MirPass for ClearDeadBlocks {
59+
fn run_on_mir<'tcx>(&mut self, mir: &mut Mir<'tcx>, _tcx: &ty::ctxt<'tcx>) {
60+
self.clear_dead_blocks(mir);
61+
}
62+
}

src/librustc_mir/transform/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
pub mod clear_dead_blocks;
1112
pub mod simplify_cfg;
1213
pub mod erase_regions;
1314
pub mod no_landing_pads;

src/librustc_mir/transform/type_check.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,6 @@ impl<'a, 'tcx> TypeckMir<'a, 'tcx> {
125125
let tcx = self.tcx();
126126
match stmt.kind {
127127
StatementKind::Assign(ref lv, ref rv) => {
128-
match lv {
129-
&Lvalue::ReturnPointer if mir.return_ty == ty::FnDiverging => {
130-
// HACK: buggy writes
131-
return;
132-
}
133-
_ => {}
134-
}
135-
136128
let lv_ty = mir.lvalue_ty(tcx, lv).to_ty(tcx);
137129
let rv_ty = mir.rvalue_ty(tcx, rv);
138130
if let Some(rv_ty) = rv_ty {

0 commit comments

Comments
 (0)