diff --git a/src/test/ui/mir/mir_detects_invalid_ops.rs b/src/test/ui/mir/mir_detects_invalid_ops.rs new file mode 100644 index 000000000000..0940dbe6a5e8 --- /dev/null +++ b/src/test/ui/mir/mir_detects_invalid_ops.rs @@ -0,0 +1,24 @@ +// build-fail + +fn main() { + divide_by_zero(); + mod_by_zero(); + oob_error_for_slices(); +} + +fn divide_by_zero() { + let y = 0; + let _z = 1 / y; //~ ERROR this operation will panic at runtime [unconditional_panic] +} + +fn mod_by_zero() { + let y = 0; + let _z = 1 % y; //~ ERROR this operation will panic at runtime [unconditional_panic] +} + +fn oob_error_for_slices() { + let a: *const [_] = &[1, 2, 3]; + unsafe { + let _b = (*a)[3]; //~ ERROR this operation will panic at runtime [unconditional_panic] + } +} diff --git a/src/test/ui/mir/mir_detects_invalid_ops.stderr b/src/test/ui/mir/mir_detects_invalid_ops.stderr new file mode 100644 index 000000000000..41f03789f237 --- /dev/null +++ b/src/test/ui/mir/mir_detects_invalid_ops.stderr @@ -0,0 +1,22 @@ +error: this operation will panic at runtime + --> $DIR/mir_detects_invalid_ops.rs:11:14 + | +LL | let _z = 1 / y; + | ^^^^^ attempt to divide by zero + | + = note: `#[deny(unconditional_panic)]` on by default + +error: this operation will panic at runtime + --> $DIR/mir_detects_invalid_ops.rs:16:14 + | +LL | let _z = 1 % y; + | ^^^^^ attempt to calculate the remainder with a divisor of zero + +error: this operation will panic at runtime + --> $DIR/mir_detects_invalid_ops.rs:22:18 + | +LL | let _b = (*a)[3]; + | ^^^^^^^ index out of bounds: the len is 3 but the index is 3 + +error: aborting due to 3 previous errors +