Skip to content

Commit 072649e

Browse files
committed
Added new ui tests to show what errors MIR can now find at compile time
1 parent 86927ed commit 072649e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// build-fail
2+
3+
fn main() {
4+
divide_by_zero();
5+
mod_by_zero();
6+
oob_error_for_slices();
7+
}
8+
9+
fn divide_by_zero() {
10+
let y = 0;
11+
let _z = 1 / y; //~ ERROR this operation will panic at runtime [unconditional_panic]
12+
}
13+
14+
fn mod_by_zero() {
15+
let y = 0;
16+
let _z = 1 % y; //~ ERROR this operation will panic at runtime [unconditional_panic]
17+
}
18+
19+
fn oob_error_for_slices() {
20+
let a: *const [_] = &[1, 2, 3];
21+
unsafe {
22+
let _b = (*a)[3]; //~ ERROR this operation will panic at runtime [unconditional_panic]
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: this operation will panic at runtime
2+
--> $DIR/mir_detects_invalid_ops.rs:11:14
3+
|
4+
LL | let _z = 1 / y;
5+
| ^^^^^ attempt to divide by zero
6+
|
7+
= note: `#[deny(unconditional_panic)]` on by default
8+
9+
error: this operation will panic at runtime
10+
--> $DIR/mir_detects_invalid_ops.rs:16:14
11+
|
12+
LL | let _z = 1 % y;
13+
| ^^^^^ attempt to calculate the remainder with a divisor of zero
14+
15+
error: this operation will panic at runtime
16+
--> $DIR/mir_detects_invalid_ops.rs:22:18
17+
|
18+
LL | let _b = (*a)[3];
19+
| ^^^^^^^ index out of bounds: the len is 3 but the index is 3
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)