about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWesley Wiser <wwiser@gmail.com>2019-05-03 21:58:16 -0400
committerWesley Wiser <wwiser@gmail.com>2019-05-15 06:07:06 -0400
commitb1c4fb2c07b42d2f2ef0a2446e65a964f139862b (patch)
tree4b4a382754ee28956a6c741c33b863837479c8c8
parent317daf77de8e3cc065982de7975647b4fb8fcc85 (diff)
downloadrust-b1c4fb2c07b42d2f2ef0a2446e65a964f139862b.tar.gz
rust-b1c4fb2c07b42d2f2ef0a2446e65a964f139862b.zip
Add some tests for constant propagation
The results aren't ideal but they represent the current state.
-rw-r--r--src/test/mir-opt/const_prop/array_index.rs33
-rw-r--r--src/test/mir-opt/const_prop/checked_add.rs21
-rw-r--r--src/test/mir-opt/const_prop/slice_len.rs37
3 files changed, 91 insertions, 0 deletions
diff --git a/src/test/mir-opt/const_prop/array_index.rs b/src/test/mir-opt/const_prop/array_index.rs
new file mode 100644
index 00000000000..4b97af68ff0
--- /dev/null
+++ b/src/test/mir-opt/const_prop/array_index.rs
@@ -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
diff --git a/src/test/mir-opt/const_prop/checked_add.rs b/src/test/mir-opt/const_prop/checked_add.rs
new file mode 100644
index 00000000000..0718316307c
--- /dev/null
+++ b/src/test/mir-opt/const_prop/checked_add.rs
@@ -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
diff --git a/src/test/mir-opt/const_prop/slice_len.rs b/src/test/mir-opt/const_prop/slice_len.rs
new file mode 100644
index 00000000000..3435ca07f4c
--- /dev/null
+++ b/src/test/mir-opt/const_prop/slice_len.rs
@@ -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