Skip to content

Commit

Permalink
Add some tests for constant propagation
Browse files Browse the repository at this point in the history
The results aren't ideal but they represent the current state.
  • Loading branch information
wesleywiser committed May 15, 2019
1 parent 317daf7 commit b1c4fb2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/test/mir-opt/const_prop/array_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
fn main() {
let x: u32 = [0, 1, 2, 3][2];
}

// END RUST SOURCE
// START rustc.main.ConstProp.before.mir
// bb0: {
// ...
// _2 = [const 0u32, const 1u32, const 2u32, const 3u32];
// ...
// _3 = const 2usize;
// _4 = const 4usize;
// _5 = Lt(_3, _4);
// assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb1;
// }
// bb1: {
// _1 = _2[_3];
// ...
// return;
// }
// END rustc.main.ConstProp.before.mir
// START rustc.main.ConstProp.after.mir
// bb0: {
// ...
// _5 = const true;
// assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb1;
// }
// bb1: {
// _1 = _2[_3];
// ...
// return;
// }
// END rustc.main.ConstProp.after.mir
21 changes: 21 additions & 0 deletions src/test/mir-opt/const_prop/checked_add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// compile-flags: -C overflow-checks=on

fn main() {
let x: u32 = 1 + 1;
}

// END RUST SOURCE
// START rustc.main.ConstProp.before.mir
// bb0: {
// ...
// _2 = CheckedAdd(const 1u32, const 1u32);
// assert(!move (_2.1: bool), "attempt to add with overflow") -> bb1;
// }
// END rustc.main.ConstProp.before.mir
// START rustc.main.ConstProp.after.mir
// bb0: {
// ...
// _2 = (const 2u32, const false);
// assert(!move (_2.1: bool), "attempt to add with overflow") -> bb1;
// }
// END rustc.main.ConstProp.after.mir
37 changes: 37 additions & 0 deletions src/test/mir-opt/const_prop/slice_len.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
fn test() -> &'static [u32] {
&[1, 2]
}

fn main() {
let x = test()[0];
}

// END RUST SOURCE
// START rustc.main.ConstProp.before.mir
// bb1: {
// ...
// _3 = const 0usize;
// _4 = Len((*_2));
// _5 = Lt(_3, _4);
// assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb2;
// }
// bb2: {
// _1 = (*_2)[_3];
// ...
// return;
// }
// END rustc.main.ConstProp.before.mir
// START rustc.main.ConstProp.after.mir
// bb0: {
// ...
// _3 = const 0usize;
// _4 = Len((*_2));
// _5 = Lt(_3, _4);
// assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb2;
// }
// bb2: {
// _1 = (*_2)[_3];
// ...
// return;
// }
// END rustc.main.ConstProp.after.mir

0 comments on commit b1c4fb2

Please sign in to comment.