Skip to content

Commit 90426ed

Browse files
committed
moving some variants from InterpError to EvalErrorPanic
1 parent fd352b0 commit 90426ed

File tree

14 files changed

+88
-78
lines changed

14 files changed

+88
-78
lines changed

src/librustc/mir/interpret/error.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,6 @@ pub enum InterpError<'tcx, O> {
284284
Unimplemented(String),
285285
DerefFunctionPointer,
286286
ExecuteMemory,
287-
BoundsCheck { len: O, index: O },
288-
Overflow(mir::BinOp),
289-
OverflowNeg,
290-
DivisionByZero,
291-
RemainderByZero,
292287
Intrinsic(String),
293288
InvalidChar(u128),
294289
StackFrameLimitReached,
@@ -332,7 +327,6 @@ pub enum InterpError<'tcx, O> {
332327
InfiniteLoop,
333328
}
334329

335-
336330
pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
337331

338332
impl<'tcx, O> InterpError<'tcx, O> {
@@ -383,8 +377,6 @@ impl<'tcx, O> InterpError<'tcx, O> {
383377
"tried to dereference a function pointer",
384378
ExecuteMemory =>
385379
"tried to treat a memory pointer as a function pointer",
386-
BoundsCheck{..} =>
387-
"array index out of bounds",
388380
Intrinsic(..) =>
389381
"intrinsic failed",
390382
NoMirFor(..) =>
@@ -436,8 +428,32 @@ impl<'tcx, O> InterpError<'tcx, O> {
436428
two",
437429
Unreachable =>
438430
"entered unreachable code",
439-
Panic { .. } =>
431+
Panic(EvalErrorPanic::Panic{..}) =>
440432
"the evaluated program panicked",
433+
Panic(EvalErrorPanic::BoundsCheck{..}) =>
434+
"array index out of bounds",
435+
Panic(EvalErrorPanic::Overflow(mir::BinOp::Add)) =>
436+
"attempt to add with overflow",
437+
Panic(EvalErrorPanic::Overflow(mir::BinOp::Sub)) =>
438+
"attempt to subtract with overflow",
439+
Panic(EvalErrorPanic::Overflow(mir::BinOp::Mul)) =>
440+
"attempt to multiply with overflow",
441+
Panic(EvalErrorPanic::Overflow(mir::BinOp::Div)) =>
442+
"attempt to divide with overflow",
443+
Panic(EvalErrorPanic::Overflow(mir::BinOp::Rem)) =>
444+
"attempt to calculate the remainder with overflow",
445+
Panic(EvalErrorPanic::OverflowNeg) =>
446+
"attempt to negate with overflow",
447+
Panic(EvalErrorPanic::Overflow(mir::BinOp::Shr)) =>
448+
"attempt to shift right with overflow",
449+
Panic(EvalErrorPanic::Overflow(mir::BinOp::Shl)) =>
450+
"attempt to shift left with overflow",
451+
Panic(EvalErrorPanic::Overflow(op)) =>
452+
bug!("{:?} cannot overflow", op),
453+
Panic(EvalErrorPanic::DivisionByZero) =>
454+
"attempt to divide by zero",
455+
Panic(EvalErrorPanic::RemainderByZero) =>
456+
"attempt to calculate the remainder with a divisor of zero",
441457
ReadFromReturnPointer =>
442458
"tried to read from the return pointer",
443459
PathNotFound(_) =>
@@ -450,17 +466,6 @@ impl<'tcx, O> InterpError<'tcx, O> {
450466
"encountered overly generic constant",
451467
ReferencedConstant =>
452468
"referenced constant has errors",
453-
Overflow(mir::BinOp::Add) => "attempt to add with overflow",
454-
Overflow(mir::BinOp::Sub) => "attempt to subtract with overflow",
455-
Overflow(mir::BinOp::Mul) => "attempt to multiply with overflow",
456-
Overflow(mir::BinOp::Div) => "attempt to divide with overflow",
457-
Overflow(mir::BinOp::Rem) => "attempt to calculate the remainder with overflow",
458-
OverflowNeg => "attempt to negate with overflow",
459-
Overflow(mir::BinOp::Shr) => "attempt to shift right with overflow",
460-
Overflow(mir::BinOp::Shl) => "attempt to shift left with overflow",
461-
Overflow(op) => bug!("{:?} cannot overflow", op),
462-
DivisionByZero => "attempt to divide by zero",
463-
RemainderByZero => "attempt to calculate the remainder with a divisor of zero",
464469
GeneratorResumedAfterReturn => "generator resumed after completion",
465470
GeneratorResumedAfterPanic => "generator resumed after panicking",
466471
InfiniteLoop =>
@@ -507,8 +512,6 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for InterpError<'tcx, O> {
507512
callee_ty, caller_ty),
508513
FunctionArgCountMismatch =>
509514
write!(f, "tried to call a function with incorrect number of arguments"),
510-
BoundsCheck { ref len, ref index } =>
511-
write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index),
512515
ReallocatedWrongMemoryKind(ref old, ref new) =>
513516
write!(f, "tried to reallocate memory from {} to {}", old, new),
514517
DeallocatedWrongMemoryKind(ref old, ref new) =>
@@ -532,8 +535,10 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for InterpError<'tcx, O> {
532535
write!(f, "incorrect alloc info: expected size {} and align {}, \
533536
got size {} and align {}",
534537
size.bytes(), align.bytes(), size2.bytes(), align2.bytes()),
535-
Panic { .. } =>
536-
write!(f, "the evaluated program panicked"),
538+
Panic(EvalErrorPanic::Panic { ref msg, line, col, ref file }) =>
539+
write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col),
540+
Panic(EvalErrorPanic::BoundsCheck { ref len, ref index }) =>
541+
write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index),
537542
InvalidDiscriminant(val) =>
538543
write!(f, "encountered invalid enum discriminant {}", val),
539544
Exit(code) =>

src/librustc/mir/interpret/pointer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ty::layout::{self, HasDataLayout, Size};
55
use rustc_macros::HashStable;
66

77
use super::{
8-
AllocId, InterpResult,
8+
AllocId, InterpResult, EvalErrorPanic
99
};
1010

1111
/// Used by `check_in_alloc` to indicate context of check
@@ -76,13 +76,13 @@ pub trait PointerArithmetic: layout::HasDataLayout {
7676
#[inline]
7777
fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
7878
let (res, over) = self.overflowing_offset(val, i);
79-
if over { err!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
79+
if over { err!(Panic(EvalErrorPanic::Overflow(mir::BinOp::Add))) } else { Ok(res) }
8080
}
8181

8282
#[inline]
8383
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
8484
let (res, over) = self.overflowing_signed_offset(val, i128::from(i));
85-
if over { err!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
85+
if over { err!(Panic(EvalErrorPanic::Overflow(mir::BinOp::Add))) } else { Ok(res) }
8686
}
8787
}
8888

src/librustc/mir/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::hir::def::{CtorKind, Namespace};
88
use crate::hir::def_id::DefId;
99
use crate::hir::{self, InlineAsm as HirInlineAsm};
10-
use crate::mir::interpret::{ConstValue, InterpError, Scalar};
10+
use crate::mir::interpret::{ConstValue, EvalErrorPanic, InterpError::Panic, Scalar};
1111
use crate::mir::visit::MirVisitable;
1212
use crate::rustc_serialize as serialize;
1313
use crate::ty::adjustment::PointerCast;
@@ -3087,11 +3087,11 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
30873087
}
30883088
}
30893089
Assert { ref cond, expected, ref msg, target, cleanup } => {
3090-
let msg = if let InterpError::BoundsCheck { ref len, ref index } = *msg {
3091-
InterpError::BoundsCheck {
3090+
let msg = if let Panic(EvalErrorPanic::BoundsCheck { ref len, ref index }) = *msg {
3091+
Panic(EvalErrorPanic::BoundsCheck {
30923092
len: len.fold_with(folder),
30933093
index: index.fold_with(folder),
3094-
}
3094+
})
30953095
} else {
30963096
msg.clone()
30973097
};
@@ -3132,7 +3132,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
31323132
}
31333133
Assert { ref cond, ref msg, .. } => {
31343134
if cond.visit_with(visitor) {
3135-
if let InterpError::BoundsCheck { ref len, ref index } = *msg {
3135+
if let Panic(EvalErrorPanic::BoundsCheck { ref len, ref index }) = *msg {
31363136
len.visit_with(visitor) || index.visit_with(visitor)
31373137
} else {
31383138
false

src/librustc/mir/visit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ macro_rules! make_mir_visitor {
514514
msg: & $($mutability)? AssertMessage<'tcx>,
515515
location: Location) {
516516
use crate::mir::interpret::InterpError::*;
517-
if let BoundsCheck { len, index } = msg {
517+
use crate::mir::interpret::EvalErrorPanic::BoundsCheck;
518+
if let Panic(BoundsCheck { len, index }) = msg {
518519
self.visit_operand(len, location);
519520
self.visit_operand(index, location);
520521
}

src/librustc_codegen_ssa/mir/block.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc::middle::lang_items;
22
use rustc::ty::{self, Ty, TypeFoldable, Instance};
33
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, FnTypeExt};
44
use rustc::mir::{self, Place, PlaceBase, Static, StaticKind};
5-
use rustc::mir::interpret::InterpError;
5+
use rustc::mir::interpret::{InterpError, EvalErrorPanic};
66
use rustc_target::abi::call::{ArgType, FnType, PassMode, IgnoreMode};
77
use rustc_target::spec::abi::Abi;
88
use crate::base;
@@ -368,7 +368,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
368368
// checked operation, just a comparison with the minimum
369369
// value, so we have to check for the assert message.
370370
if !bx.check_overflow() {
371-
if let mir::interpret::InterpError::OverflowNeg = *msg {
371+
if let InterpError::Panic(EvalErrorPanic::OverflowNeg) = *msg {
372372
const_cond = Some(expected);
373373
}
374374
}
@@ -403,7 +403,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
403403

404404
// Put together the arguments to the panic entry point.
405405
let (lang_item, args) = match *msg {
406-
InterpError::BoundsCheck { ref len, ref index } => {
406+
InterpError::Panic(EvalErrorPanic::BoundsCheck { ref len, ref index }) => {
407407
let len = self.codegen_operand(&mut bx, len).immediate();
408408
let index = self.codegen_operand(&mut bx, index).immediate();
409409

src/librustc_mir/borrow_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,8 @@ impl<'cx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx
734734
cleanup: _,
735735
} => {
736736
self.consume_operand(loc, (cond, span), flow_state);
737-
use rustc::mir::interpret::InterpError::BoundsCheck;
738-
if let BoundsCheck { ref len, ref index } = *msg {
737+
use rustc::mir::interpret::{InterpError::Panic, EvalErrorPanic};
738+
if let Panic(EvalErrorPanic::BoundsCheck { ref len, ref index }) = *msg {
739739
self.consume_operand(loc, (len, span), flow_state);
740740
self.consume_operand(loc, (index, span), flow_state);
741741
}

src/librustc_mir/borrow_check/nll/invalidation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
207207
cleanup: _,
208208
} => {
209209
self.consume_operand(location, cond);
210-
use rustc::mir::interpret::InterpError::BoundsCheck;
211-
if let BoundsCheck { ref len, ref index } = *msg {
210+
use rustc::mir::interpret::{InterpError::Panic, EvalErrorPanic::BoundsCheck};
211+
if let Panic(BoundsCheck { ref len, ref index }) = *msg {
212212
self.consume_operand(location, len);
213213
self.consume_operand(location, index);
214214
}

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc::infer::canonical::QueryRegionConstraints;
2828
use rustc::infer::outlives::env::RegionBoundPairs;
2929
use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime, NLLRegionVariableOrigin};
3030
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
31-
use rustc::mir::interpret::{InterpError::BoundsCheck, ConstValue};
31+
use rustc::mir::interpret::{InterpError::Panic, ConstValue, EvalErrorPanic};
3232
use rustc::mir::tcx::PlaceTy;
3333
use rustc::mir::visit::{PlaceContext, Visitor, NonMutatingUseContext};
3434
use rustc::mir::*;
@@ -1589,7 +1589,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15891589
span_mirbug!(self, term, "bad Assert ({:?}, not bool", cond_ty);
15901590
}
15911591

1592-
if let BoundsCheck { ref len, ref index } = *msg {
1592+
if let Panic(EvalErrorPanic::BoundsCheck { ref len, ref index }) = *msg {
15931593
if len.ty(body, tcx) != tcx.types.usize {
15941594
span_mirbug!(self, len, "bounds-check length non-usize {:?}", len)
15951595
}

src/librustc_mir/build/expr/as_place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::build::expr::category::Category;
44
use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
55
use crate::build::{BlockAnd, BlockAndExtension, Builder};
66
use crate::hair::*;
7-
use rustc::mir::interpret::InterpError::BoundsCheck;
7+
use rustc::mir::interpret::{InterpError::Panic, EvalErrorPanic::BoundsCheck};
88
use rustc::mir::*;
99
use rustc::ty::{CanonicalUserTypeAnnotation, Variance};
1010

@@ -105,10 +105,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
105105
),
106106
);
107107

108-
let msg = BoundsCheck {
108+
let msg = Panic(BoundsCheck {
109109
len: Operand::Move(len),
110110
index: Operand::Copy(Place::from(idx)),
111-
};
111+
});
112112
let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
113113
success.and(slice.index(idx))
114114
}

src/librustc_mir/build/expr/as_rvalue.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::build::expr::category::{Category, RvalueFunc};
77
use crate::build::{BlockAnd, BlockAndExtension, Builder};
88
use crate::hair::*;
99
use rustc::middle::region;
10-
use rustc::mir::interpret::InterpError;
10+
use rustc::mir::interpret::{InterpError::Panic, EvalErrorPanic};
1111
use rustc::mir::*;
1212
use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty, UpvarSubsts};
1313
use syntax_pos::Span;
@@ -101,7 +101,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
101101
block,
102102
Operand::Move(is_min),
103103
false,
104-
InterpError::OverflowNeg,
104+
Panic(EvalErrorPanic::OverflowNeg),
105105
expr_span,
106106
);
107107
}
@@ -401,7 +401,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
401401
let val = result_value.clone().field(val_fld, ty);
402402
let of = result_value.field(of_fld, bool_ty);
403403

404-
let err = InterpError::Overflow(op);
404+
let err = Panic(EvalErrorPanic::Overflow(op));
405405

406406
block = self.assert(block, Operand::Move(of), false, err, span);
407407

@@ -412,9 +412,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
412412
// and 2. there are two possible failure cases, divide-by-zero and overflow.
413413

414414
let (zero_err, overflow_err) = if op == BinOp::Div {
415-
(InterpError::DivisionByZero, InterpError::Overflow(op))
415+
(Panic(EvalErrorPanic::DivisionByZero), Panic(EvalErrorPanic::Overflow(op)))
416416
} else {
417-
(InterpError::RemainderByZero, InterpError::Overflow(op))
417+
(Panic(EvalErrorPanic::RemainderByZero), Panic(EvalErrorPanic::Overflow(op)))
418418
};
419419

420420
// Check for / 0

src/librustc_mir/interpret/operator.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc::mir;
22
use rustc::ty::{self, layout::TyLayout};
33
use syntax::ast::FloatTy;
44
use rustc_apfloat::Float;
5-
use rustc::mir::interpret::{InterpResult, Scalar};
5+
use rustc::mir::interpret::{InterpResult, EvalErrorPanic, Scalar};
66

77
use super::{InterpCx, PlaceTy, Immediate, Machine, ImmTy};
88

@@ -173,8 +173,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
173173
return Ok((Scalar::from_bool(op(&l, &r)), false));
174174
}
175175
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
176-
Div if r == 0 => return err!(DivisionByZero),
177-
Rem if r == 0 => return err!(RemainderByZero),
176+
Div if r == 0 => return err!(Panic(EvalErrorPanic::DivisionByZero)),
177+
Rem if r == 0 => return err!(Panic(EvalErrorPanic::RemainderByZero)),
178178
Div => Some(i128::overflowing_div),
179179
Rem => Some(i128::overflowing_rem),
180180
Add => Some(i128::overflowing_add),
@@ -231,8 +231,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
231231
Add => u128::overflowing_add,
232232
Sub => u128::overflowing_sub,
233233
Mul => u128::overflowing_mul,
234-
Div if r == 0 => return err!(DivisionByZero),
235-
Rem if r == 0 => return err!(RemainderByZero),
234+
Div if r == 0 => return err!(Panic(EvalErrorPanic::DivisionByZero)),
235+
Rem if r == 0 => return err!(Panic(EvalErrorPanic::RemainderByZero)),
236236
Div => u128::overflowing_div,
237237
Rem => u128::overflowing_rem,
238238
_ => bug!(),

src/librustc_mir/interpret/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc::ty::TypeFoldable;
1313

1414
use super::{
1515
GlobalId, AllocId, Allocation, Scalar, InterpResult, Pointer, PointerArithmetic,
16-
InterpCx, Machine, AllocMap, AllocationExtra,
16+
InterpCx, Machine, AllocMap, AllocationExtra, EvalErrorPanic,
1717
RawConst, Immediate, ImmTy, ScalarMaybeUndef, Operand, OpTy, MemoryKind, LocalValue
1818
};
1919

@@ -356,7 +356,7 @@ where
356356
// This can be violated because this runs during promotion on code where the
357357
// type system has not yet ensured that such things don't happen.
358358
debug!("tried to access element {} of array/slice with length {}", field, len);
359-
return err!(BoundsCheck { len, index: field });
359+
return err!(Panic(EvalErrorPanic::BoundsCheck { len, index: field }));
360360
}
361361
stride * field
362362
}

src/librustc_mir/interpret/terminator.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax::source_map::Span;
77
use rustc_target::spec::abi::Abi;
88

99
use super::{
10-
InterpResult, PointerArithmetic, InterpError, Scalar,
10+
InterpResult, PointerArithmetic, InterpError, Scalar, EvalErrorPanic,
1111
InterpCx, Machine, Immediate, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
1212
};
1313

@@ -137,19 +137,23 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
137137
// Compute error message
138138
use rustc::mir::interpret::InterpError::*;
139139
return match *msg {
140-
BoundsCheck { ref len, ref index } => {
140+
Panic(EvalErrorPanic::BoundsCheck { ref len, ref index }) => {
141141
let len = self.read_immediate(self.eval_operand(len, None)?)
142142
.expect("can't eval len").to_scalar()?
143143
.to_bits(self.memory().pointer_size())? as u64;
144144
let index = self.read_immediate(self.eval_operand(index, None)?)
145145
.expect("can't eval index").to_scalar()?
146146
.to_bits(self.memory().pointer_size())? as u64;
147-
err!(BoundsCheck { len, index })
147+
err!(Panic(EvalErrorPanic::BoundsCheck { len, index }))
148148
}
149-
Overflow(op) => Err(Overflow(op).into()),
150-
OverflowNeg => Err(OverflowNeg.into()),
151-
DivisionByZero => Err(DivisionByZero.into()),
152-
RemainderByZero => Err(RemainderByZero.into()),
149+
Panic(EvalErrorPanic::Overflow(op)) =>
150+
Err(Panic(EvalErrorPanic::Overflow(op)).into()),
151+
Panic(EvalErrorPanic::OverflowNeg) =>
152+
Err(Panic(EvalErrorPanic::OverflowNeg).into()),
153+
Panic(EvalErrorPanic::DivisionByZero) =>
154+
Err(Panic(EvalErrorPanic::DivisionByZero).into()),
155+
Panic(EvalErrorPanic::RemainderByZero) =>
156+
Err(Panic(EvalErrorPanic::RemainderByZero).into()),
153157
GeneratorResumedAfterReturn |
154158
GeneratorResumedAfterPanic => unimplemented!(),
155159
_ => bug!(),

0 commit comments

Comments
 (0)