Added new ui tests to show what errors MIR can now find at compile time
This commit is contained in:
parent
86927eddaf
commit
072649ec5d
2 changed files with 46 additions and 0 deletions
24
src/test/ui/mir/mir_detects_invalid_ops.rs
Normal file
24
src/test/ui/mir/mir_detects_invalid_ops.rs
Normal file
|
|
@ -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]
|
||||
}
|
||||
}
|
||||
22
src/test/ui/mir/mir_detects_invalid_ops.stderr
Normal file
22
src/test/ui/mir/mir_detects_invalid_ops.stderr
Normal file
|
|
@ -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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue