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

Lines changed: 19 additions & 2 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 7 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 7 deletions
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

0 commit comments

Comments
 (0)