Skip to content

Commit 5c5c8eb

Browse files
committed
Auto merge of #66927 - RalfJung:engines-dont-panic, r=oli-obk
Miri core engine: use throw_ub instead of throw_panic See #66902 for context: panicking is not really an "interpreter error", but just part of a normal Rust execution. This is a first step towards removing the `InterpError::Panic` variant: the core Miri engine does not use it any more. ConstProp and ConstEval still use it, though. This will be addressed in future PRs. From what I can tell, all the error messages this removes are actually duplicates. r? @oli-obk @wesleywiser
2 parents 0a953cd + 15f159a commit 5c5c8eb

22 files changed

+84
-274
lines changed

src/librustc/mir/interpret/error.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ pub enum UndefinedBehaviorInfo {
370370
Unreachable,
371371
/// An enum discriminant was set to a value which was outside the range of valid values.
372372
InvalidDiscriminant(ScalarMaybeUndef),
373+
/// A slice/array index projection went out-of-bounds.
374+
BoundsCheckFailed { len: u64, index: u64 },
375+
/// Something was divided by 0 (x / 0).
376+
DivisionByZero,
377+
/// Something was "remainded" by 0 (x % 0).
378+
RemainderByZero,
379+
/// Overflowing inbounds pointer arithmetic.
380+
PointerArithOverflow,
373381
}
374382

375383
impl fmt::Debug for UndefinedBehaviorInfo {
@@ -379,9 +387,18 @@ impl fmt::Debug for UndefinedBehaviorInfo {
379387
Ub(msg) | UbExperimental(msg) =>
380388
write!(f, "{}", msg),
381389
Unreachable =>
382-
write!(f, "entered unreachable code"),
390+
write!(f, "entering unreachable code"),
383391
InvalidDiscriminant(val) =>
384-
write!(f, "encountered invalid enum discriminant {}", val),
392+
write!(f, "encountering invalid enum discriminant {}", val),
393+
BoundsCheckFailed { ref len, ref index } =>
394+
write!(f, "indexing out of bounds: the len is {:?} but the index is {:?}",
395+
len, index),
396+
DivisionByZero =>
397+
write!(f, "dividing by zero"),
398+
RemainderByZero =>
399+
write!(f, "calculating the remainder with a divisor of zero"),
400+
PointerArithOverflow =>
401+
write!(f, "overflowing in-bounds pointer arithmetic"),
385402
}
386403
}
387404
}

src/librustc/mir/interpret/pointer.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{AllocId, InterpResult};
22

3-
use crate::mir;
43
use crate::ty::layout::{self, HasDataLayout, Size};
54

65
use rustc_macros::HashStable;
@@ -88,13 +87,13 @@ pub trait PointerArithmetic: layout::HasDataLayout {
8887
#[inline]
8988
fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
9089
let (res, over) = self.overflowing_offset(val, i);
91-
if over { throw_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
90+
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
9291
}
9392

9493
#[inline]
9594
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
9695
let (res, over) = self.overflowing_signed_offset(val, i128::from(i));
97-
if over { throw_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
96+
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
9897
}
9998
}
10099

src/librustc_mir/interpret/operator.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
177177
return Ok((Scalar::from_bool(op(&l, &r)), false, self.tcx.types.bool));
178178
}
179179
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
180-
Div if r == 0 => throw_panic!(DivisionByZero),
181-
Rem if r == 0 => throw_panic!(RemainderByZero),
180+
Div if r == 0 => throw_ub!(DivisionByZero),
181+
Rem if r == 0 => throw_ub!(RemainderByZero),
182182
Div => Some(i128::overflowing_div),
183183
Rem => Some(i128::overflowing_rem),
184184
Add => Some(i128::overflowing_add),
@@ -234,8 +234,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
234234
Add => u128::overflowing_add,
235235
Sub => u128::overflowing_sub,
236236
Mul => u128::overflowing_mul,
237-
Div if r == 0 => throw_panic!(DivisionByZero),
238-
Rem if r == 0 => throw_panic!(RemainderByZero),
237+
Div if r == 0 => throw_ub!(DivisionByZero),
238+
Rem if r == 0 => throw_ub!(RemainderByZero),
239239
Div => u128::overflowing_div,
240240
Rem => u128::overflowing_rem,
241241
_ => bug!(),

src/librustc_mir/interpret/place.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,8 @@ where
384384
layout::FieldPlacement::Array { stride, .. } => {
385385
let len = base.len(self)?;
386386
if field >= len {
387-
// This can be violated because the index (field) can be a runtime value
388-
// provided by the user.
389-
debug!("tried to access element {} of array/slice with length {}", field, len);
390-
throw_panic!(BoundsCheck { len, index: field });
387+
// This can only be reached in ConstProp and non-rustc-MIR.
388+
throw_ub!(BoundsCheckFailed { len, index: field });
391389
}
392390
stride * field
393391
}

src/test/compile-fail/consts/const-err3.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ fn main() {
1414
//~^ ERROR const_err
1515
let _e = [5u8][1];
1616
//~^ ERROR const_err
17-
//~| ERROR this expression will panic at runtime
1817
black_box(b);
1918
black_box(c);
2019
black_box(d);

src/test/ui/consts/array-literal-index-oob.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error: reaching this expression at runtime will panic or abort
1212
LL | &{[1, 2, 3][4]};
1313
| --^^^^^^^^^^^^-
1414
| |
15-
| index out of bounds: the len is 3 but the index is 4
15+
| indexing out of bounds: the len is 3 but the index is 4
1616

1717
error: aborting due to 2 previous errors
1818

src/test/ui/consts/const-err2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fn main() {
2323
//~^ ERROR const_err
2424
let _e = [5u8][1];
2525
//~^ ERROR index out of bounds
26-
//~| ERROR this expression will panic at runtime
2726
black_box(a);
2827
black_box(b);
2928
black_box(c);

src/test/ui/consts/const-err2.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,5 @@ error: index out of bounds: the len is 1 but the index is 1
3434
LL | let _e = [5u8][1];
3535
| ^^^^^^^^
3636

37-
error: this expression will panic at runtime
38-
--> $DIR/const-err2.rs:24:14
39-
|
40-
LL | let _e = [5u8][1];
41-
| ^^^^^^^^ index out of bounds: the len is 1 but the index is 1
42-
43-
error: aborting due to 6 previous errors
37+
error: aborting due to 5 previous errors
4438

src/test/ui/consts/const-err3.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fn main() {
2323
//~^ ERROR const_err
2424
let _e = [5u8][1];
2525
//~^ ERROR const_err
26-
//~| ERROR this expression will panic at runtime
2726
black_box(a);
2827
black_box(b);
2928
black_box(c);

src/test/ui/consts/const-err3.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,5 @@ error: index out of bounds: the len is 1 but the index is 1
3434
LL | let _e = [5u8][1];
3535
| ^^^^^^^^
3636

37-
error: this expression will panic at runtime
38-
--> $DIR/const-err3.rs:24:14
39-
|
40-
LL | let _e = [5u8][1];
41-
| ^^^^^^^^ index out of bounds: the len is 1 but the index is 1
42-
43-
error: aborting due to 6 previous errors
37+
error: aborting due to 5 previous errors
4438

src/test/ui/consts/const-eval/promoted_errors.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ fn main() {
88
//~^ ERROR const_err
99
println!("{}", 1/(1-1));
1010
//~^ ERROR attempt to divide by zero [const_err]
11-
//~| ERROR reaching this expression at runtime will panic or abort [const_err]
11+
//~| ERROR const_err
1212
let _x = 1/(1-1);
1313
//~^ ERROR const_err
14-
//~| ERROR const_err
1514
println!("{}", 1/(false as u32));
1615
//~^ ERROR attempt to divide by zero [const_err]
17-
//~| ERROR reaching this expression at runtime will panic or abort [const_err]
16+
//~| ERROR const_err
1817
let _x = 1/(false as u32);
1918
//~^ ERROR const_err
20-
//~| ERROR const_err
2119
}

src/test/ui/consts/const-eval/promoted_errors.stderr

+6-18
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,31 @@ error: reaching this expression at runtime will panic or abort
2020
--> $DIR/promoted_errors.rs:9:20
2121
|
2222
LL | println!("{}", 1/(1-1));
23-
| ^^^^^^^ attempt to divide by zero
23+
| ^^^^^^^ dividing by zero
2424

2525
error: attempt to divide by zero
2626
--> $DIR/promoted_errors.rs:12:14
2727
|
2828
LL | let _x = 1/(1-1);
2929
| ^^^^^^^
3030

31-
error: this expression will panic at runtime
32-
--> $DIR/promoted_errors.rs:12:14
33-
|
34-
LL | let _x = 1/(1-1);
35-
| ^^^^^^^ attempt to divide by zero
36-
3731
error: attempt to divide by zero
38-
--> $DIR/promoted_errors.rs:15:20
32+
--> $DIR/promoted_errors.rs:14:20
3933
|
4034
LL | println!("{}", 1/(false as u32));
4135
| ^^^^^^^^^^^^^^^^
4236

4337
error: reaching this expression at runtime will panic or abort
44-
--> $DIR/promoted_errors.rs:15:20
38+
--> $DIR/promoted_errors.rs:14:20
4539
|
4640
LL | println!("{}", 1/(false as u32));
47-
| ^^^^^^^^^^^^^^^^ attempt to divide by zero
41+
| ^^^^^^^^^^^^^^^^ dividing by zero
4842

4943
error: attempt to divide by zero
50-
--> $DIR/promoted_errors.rs:18:14
44+
--> $DIR/promoted_errors.rs:17:14
5145
|
5246
LL | let _x = 1/(false as u32);
5347
| ^^^^^^^^^^^^^^^^
5448

55-
error: this expression will panic at runtime
56-
--> $DIR/promoted_errors.rs:18:14
57-
|
58-
LL | let _x = 1/(false as u32);
59-
| ^^^^^^^^^^^^^^^^ attempt to divide by zero
60-
61-
error: aborting due to 9 previous errors
49+
error: aborting due to 7 previous errors
6250

src/test/ui/consts/const-eval/promoted_errors2.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ fn main() {
99
//~^ ERROR attempt to subtract with overflow
1010
println!("{}", 1/(1-1));
1111
//~^ ERROR attempt to divide by zero [const_err]
12-
//~| ERROR reaching this expression at runtime will panic or abort [const_err]
12+
//~| ERROR const_err
1313
let _x = 1/(1-1);
1414
//~^ ERROR const_err
15-
//~| ERROR const_err
1615
println!("{}", 1/(false as u32));
1716
//~^ ERROR attempt to divide by zero [const_err]
18-
//~| ERROR reaching this expression at runtime will panic or abort [const_err]
17+
//~| ERROR const_err
1918
let _x = 1/(false as u32);
2019
//~^ ERROR const_err
21-
//~| ERROR const_err
2220
}

src/test/ui/consts/const-eval/promoted_errors2.stderr

+6-18
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,31 @@ error: reaching this expression at runtime will panic or abort
2626
--> $DIR/promoted_errors2.rs:10:20
2727
|
2828
LL | println!("{}", 1/(1-1));
29-
| ^^^^^^^ attempt to divide by zero
29+
| ^^^^^^^ dividing by zero
3030

3131
error: attempt to divide by zero
3232
--> $DIR/promoted_errors2.rs:13:14
3333
|
3434
LL | let _x = 1/(1-1);
3535
| ^^^^^^^
3636

37-
error: this expression will panic at runtime
38-
--> $DIR/promoted_errors2.rs:13:14
39-
|
40-
LL | let _x = 1/(1-1);
41-
| ^^^^^^^ attempt to divide by zero
42-
4337
error: attempt to divide by zero
44-
--> $DIR/promoted_errors2.rs:16:20
38+
--> $DIR/promoted_errors2.rs:15:20
4539
|
4640
LL | println!("{}", 1/(false as u32));
4741
| ^^^^^^^^^^^^^^^^
4842

4943
error: reaching this expression at runtime will panic or abort
50-
--> $DIR/promoted_errors2.rs:16:20
44+
--> $DIR/promoted_errors2.rs:15:20
5145
|
5246
LL | println!("{}", 1/(false as u32));
53-
| ^^^^^^^^^^^^^^^^ attempt to divide by zero
47+
| ^^^^^^^^^^^^^^^^ dividing by zero
5448

5549
error: attempt to divide by zero
56-
--> $DIR/promoted_errors2.rs:19:14
50+
--> $DIR/promoted_errors2.rs:18:14
5751
|
5852
LL | let _x = 1/(false as u32);
5953
| ^^^^^^^^^^^^^^^^
6054

61-
error: this expression will panic at runtime
62-
--> $DIR/promoted_errors2.rs:19:14
63-
|
64-
LL | let _x = 1/(false as u32);
65-
| ^^^^^^^^^^^^^^^^ attempt to divide by zero
66-
67-
error: aborting due to 10 previous errors
55+
error: aborting due to 8 previous errors
6856

src/test/ui/consts/const-prop-ice.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
fn main() {
22
[0; 3][3u64 as usize]; //~ ERROR the len is 3 but the index is 3
3-
//~| ERROR this expression will panic at runtime
43
}

src/test/ui/consts/const-prop-ice.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,5 @@ LL | [0; 3][3u64 as usize];
66
|
77
= note: `#[deny(const_err)]` on by default
88

9-
error: this expression will panic at runtime
10-
--> $DIR/const-prop-ice.rs:2:5
11-
|
12-
LL | [0; 3][3u64 as usize];
13-
| ^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 3 but the index is 3
14-
15-
error: aborting due to 2 previous errors
9+
error: aborting due to previous error
1610

src/test/ui/issues/issue-54348.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
fn main() {
22
[1][0u64 as usize];
33
[1][1.5 as usize]; //~ ERROR index out of bounds
4-
//~| ERROR this expression will panic at runtime
54
[1][1u64 as usize]; //~ ERROR index out of bounds
6-
//~| ERROR this expression will panic at runtime
75
}

src/test/ui/issues/issue-54348.stderr

+2-14
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,11 @@ LL | [1][1.5 as usize];
66
|
77
= note: `#[deny(const_err)]` on by default
88

9-
error: this expression will panic at runtime
10-
--> $DIR/issue-54348.rs:3:5
11-
|
12-
LL | [1][1.5 as usize];
13-
| ^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
14-
159
error: index out of bounds: the len is 1 but the index is 1
16-
--> $DIR/issue-54348.rs:5:5
10+
--> $DIR/issue-54348.rs:4:5
1711
|
1812
LL | [1][1u64 as usize];
1913
| ^^^^^^^^^^^^^^^^^^
2014

21-
error: this expression will panic at runtime
22-
--> $DIR/issue-54348.rs:5:5
23-
|
24-
LL | [1][1u64 as usize];
25-
| ^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
26-
27-
error: aborting due to 4 previous errors
15+
error: aborting due to 2 previous errors
2816

src/test/ui/issues/issue-8460-const.rs

-10
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,14 @@ fn main() {
2323
//~| ERROR this expression will panic at runtime
2424
assert!(thread::spawn(move|| { 1isize / 0; }).join().is_err());
2525
//~^ ERROR attempt to divide by zero
26-
//~| ERROR this expression will panic at runtime
2726
assert!(thread::spawn(move|| { 1i8 / 0; }).join().is_err());
2827
//~^ ERROR attempt to divide by zero
29-
//~| ERROR this expression will panic at runtime
3028
assert!(thread::spawn(move|| { 1i16 / 0; }).join().is_err());
3129
//~^ ERROR attempt to divide by zero
32-
//~| ERROR this expression will panic at runtime
3330
assert!(thread::spawn(move|| { 1i32 / 0; }).join().is_err());
3431
//~^ ERROR attempt to divide by zero
35-
//~| ERROR this expression will panic at runtime
3632
assert!(thread::spawn(move|| { 1i64 / 0; }).join().is_err());
3733
//~^ ERROR attempt to divide by zero
38-
//~| ERROR this expression will panic at runtime
3934
assert!(thread::spawn(move|| { isize::MIN % -1; }).join().is_err());
4035
//~^ ERROR attempt to calculate the remainder with overflow
4136
//~| ERROR this expression will panic at runtime
@@ -53,17 +48,12 @@ fn main() {
5348
//~| ERROR this expression will panic at runtime
5449
assert!(thread::spawn(move|| { 1isize % 0; }).join().is_err());
5550
//~^ ERROR attempt to calculate the remainder with a divisor of zero
56-
//~| ERROR this expression will panic at runtime
5751
assert!(thread::spawn(move|| { 1i8 % 0; }).join().is_err());
5852
//~^ ERROR attempt to calculate the remainder with a divisor of zero
59-
//~| ERROR this expression will panic at runtime
6053
assert!(thread::spawn(move|| { 1i16 % 0; }).join().is_err());
6154
//~^ ERROR attempt to calculate the remainder with a divisor of zero
62-
//~| ERROR this expression will panic at runtime
6355
assert!(thread::spawn(move|| { 1i32 % 0; }).join().is_err());
6456
//~^ ERROR attempt to calculate the remainder with a divisor of zero
65-
//~| ERROR this expression will panic at runtime
6657
assert!(thread::spawn(move|| { 1i64 % 0; }).join().is_err());
6758
//~^ ERROR attempt to calculate the remainder with a divisor of zero
68-
//~| ERROR this expression will panic at runtime
6959
}

0 commit comments

Comments
 (0)