Skip to content

Commit

Permalink
Rollup merge of rust-lang#62927 - RalfJung:panic, r=oli-obk
Browse files Browse the repository at this point in the history
use PanicMessage in MIR, kill InterpError::description

r? @oli-obk @eddyb
Cc @saleemjaffer rust-lang/const-eval#4
  • Loading branch information
Centril authored Jul 24, 2019
2 parents 0466237 + ff18786 commit e27927d
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 238 deletions.
337 changes: 163 additions & 174 deletions src/librustc/mir/interpret/error.rs

Large diffs are not rendered by default.

31 changes: 19 additions & 12 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::hir::def::{CtorKind, Namespace};
use crate::hir::def_id::DefId;
use crate::hir::{self, InlineAsm as HirInlineAsm};
use crate::mir::interpret::{ConstValue, PanicMessage, InterpError::Panic, Scalar};
use crate::mir::interpret::{ConstValue, PanicMessage, Scalar};
use crate::mir::visit::MirVisitable;
use crate::rustc_serialize as serialize;
use crate::ty::adjustment::PointerCast;
Expand Down Expand Up @@ -3152,13 +3152,16 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
}
}
Assert { ref cond, expected, ref msg, target, cleanup } => {
let msg = if let Panic(PanicMessage::BoundsCheck { ref len, ref index }) = *msg {
Panic(PanicMessage::BoundsCheck {
len: len.fold_with(folder),
index: index.fold_with(folder),
})
} else {
msg.clone()
use PanicMessage::*;
let msg = match msg {
BoundsCheck { ref len, ref index } =>
BoundsCheck {
len: len.fold_with(folder),
index: index.fold_with(folder),
},
Panic { .. } | Overflow(_) | OverflowNeg | DivisionByZero | RemainderByZero |
GeneratorResumedAfterReturn | GeneratorResumedAfterPanic =>
msg.clone(),
};
Assert { cond: cond.fold_with(folder), expected, msg, target, cleanup }
}
Expand Down Expand Up @@ -3197,10 +3200,14 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
}
Assert { ref cond, ref msg, .. } => {
if cond.visit_with(visitor) {
if let Panic(PanicMessage::BoundsCheck { ref len, ref index }) = *msg {
len.visit_with(visitor) || index.visit_with(visitor)
} else {
false
use PanicMessage::*;
match msg {
BoundsCheck { ref len, ref index } =>
len.visit_with(visitor) || index.visit_with(visitor),
Panic { .. } | Overflow(_) | OverflowNeg |
DivisionByZero | RemainderByZero |
GeneratorResumedAfterReturn | GeneratorResumedAfterPanic =>
false
}
} else {
false
Expand Down
15 changes: 10 additions & 5 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,16 @@ macro_rules! make_mir_visitor {
fn super_assert_message(&mut self,
msg: & $($mutability)? AssertMessage<'tcx>,
location: Location) {
use crate::mir::interpret::InterpError::*;
use crate::mir::interpret::PanicMessage::BoundsCheck;
if let Panic(BoundsCheck { len, index }) = msg {
self.visit_operand(len, location);
self.visit_operand(index, location);
use crate::mir::interpret::PanicMessage::*;
match msg {
BoundsCheck { len, index } => {
self.visit_operand(len, location);
self.visit_operand(index, location);
}
Panic { .. } | Overflow(_) | OverflowNeg | DivisionByZero | RemainderByZero |
GeneratorResumedAfterReturn | GeneratorResumedAfterPanic => {
// Nothing to visit
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc::middle::lang_items;
use rustc::ty::{self, Ty, TypeFoldable, Instance};
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, FnTypeExt};
use rustc::mir::{self, Place, PlaceBase, Static, StaticKind};
use rustc::mir::interpret::{InterpError, PanicMessage};
use rustc::mir::interpret::PanicMessage;
use rustc_target::abi::call::{ArgType, FnType, PassMode, IgnoreMode};
use rustc_target::spec::abi::Abi;
use crate::base;
Expand Down Expand Up @@ -368,7 +368,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// checked operation, just a comparison with the minimum
// value, so we have to check for the assert message.
if !bx.check_overflow() {
if let InterpError::Panic(PanicMessage::OverflowNeg) = *msg {
if let PanicMessage::OverflowNeg = *msg {
const_cond = Some(expected);
}
}
Expand Down Expand Up @@ -402,8 +402,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);

// Put together the arguments to the panic entry point.
let (lang_item, args) = match *msg {
InterpError::Panic(PanicMessage::BoundsCheck { ref len, ref index }) => {
let (lang_item, args) = match msg {
PanicMessage::BoundsCheck { ref len, ref index } => {
let len = self.codegen_operand(&mut bx, len).immediate();
let index = self.codegen_operand(&mut bx, index).immediate();

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,8 @@ impl<'cx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx
cleanup: _,
} => {
self.consume_operand(loc, (cond, span), flow_state);
use rustc::mir::interpret::{InterpError::Panic, PanicMessage};
if let Panic(PanicMessage::BoundsCheck { ref len, ref index }) = *msg {
use rustc::mir::interpret::PanicMessage;
if let PanicMessage::BoundsCheck { ref len, ref index } = *msg {
self.consume_operand(loc, (len, span), flow_state);
self.consume_operand(loc, (index, span), flow_state);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/nll/invalidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
cleanup: _,
} => {
self.consume_operand(location, cond);
use rustc::mir::interpret::{InterpError::Panic, PanicMessage::BoundsCheck};
if let Panic(BoundsCheck { ref len, ref index }) = *msg {
use rustc::mir::interpret::PanicMessage;
if let PanicMessage::BoundsCheck { ref len, ref index } = *msg {
self.consume_operand(location, len);
self.consume_operand(location, index);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc::infer::canonical::QueryRegionConstraints;
use rustc::infer::outlives::env::RegionBoundPairs;
use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime, NLLRegionVariableOrigin};
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc::mir::interpret::{InterpError::Panic, ConstValue, PanicMessage};
use rustc::mir::interpret::{ConstValue, PanicMessage};
use rustc::mir::tcx::PlaceTy;
use rustc::mir::visit::{PlaceContext, Visitor, NonMutatingUseContext};
use rustc::mir::*;
Expand Down Expand Up @@ -1606,7 +1606,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
span_mirbug!(self, term, "bad Assert ({:?}, not bool", cond_ty);
}

if let Panic(PanicMessage::BoundsCheck { ref len, ref index }) = *msg {
if let PanicMessage::BoundsCheck { ref len, ref index } = *msg {
if len.ty(body, tcx) != tcx.types.usize {
span_mirbug!(self, len, "bounds-check length non-usize {:?}", len)
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::build::expr::category::Category;
use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
use crate::build::{BlockAnd, BlockAndExtension, Builder};
use crate::hair::*;
use rustc::mir::interpret::{InterpError::Panic, PanicMessage::BoundsCheck};
use rustc::mir::interpret::{PanicMessage::BoundsCheck};
use rustc::mir::*;
use rustc::ty::{CanonicalUserTypeAnnotation, Variance};

Expand Down Expand Up @@ -105,10 +105,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
),
);

let msg = Panic(BoundsCheck {
let msg = BoundsCheck {
len: Operand::Move(len),
index: Operand::Copy(Place::from(idx)),
});
};
let success = this.assert(block, Operand::Move(lt), true, msg, expr_span);
success.and(slice.index(idx))
}
Expand Down
13 changes: 7 additions & 6 deletions src/librustc_mir/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::build::expr::category::{Category, RvalueFunc};
use crate::build::{BlockAnd, BlockAndExtension, Builder};
use crate::hair::*;
use rustc::middle::region;
use rustc::mir::interpret::{InterpError::Panic, PanicMessage};
use rustc::mir::interpret::PanicMessage;
use rustc::mir::*;
use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty, UpvarSubsts};
use syntax_pos::Span;
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block,
Operand::Move(is_min),
false,
Panic(PanicMessage::OverflowNeg),
PanicMessage::OverflowNeg,
expr_span,
);
}
Expand Down Expand Up @@ -401,7 +401,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let val = result_value.clone().field(val_fld, ty);
let of = result_value.field(of_fld, bool_ty);

let err = Panic(PanicMessage::Overflow(op));
let err = PanicMessage::Overflow(op);

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

Expand All @@ -411,11 +411,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Checking division and remainder is more complex, since we 1. always check
// and 2. there are two possible failure cases, divide-by-zero and overflow.

let (zero_err, overflow_err) = if op == BinOp::Div {
(Panic(PanicMessage::DivisionByZero), Panic(PanicMessage::Overflow(op)))
let zero_err = if op == BinOp::Div {
PanicMessage::DivisionByZero
} else {
(Panic(PanicMessage::RemainderByZero), Panic(PanicMessage::Overflow(op)))
PanicMessage::RemainderByZero
};
let overflow_err = PanicMessage::Overflow(op);

// Check for / 0
let is_zero = self.temp(bool_ty, span);
Expand Down
35 changes: 19 additions & 16 deletions src/librustc_mir/interpret/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syntax::source_map::Span;
use rustc_target::spec::abi::Abi;

use super::{
InterpResult, PointerArithmetic, InterpError, Scalar, PanicMessage,
InterpResult, PointerArithmetic, InterpError, Scalar,
InterpCx, Machine, Immediate, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
};

Expand Down Expand Up @@ -135,28 +135,31 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
self.goto_block(Some(target))?;
} else {
// Compute error message
use rustc::mir::interpret::InterpError::*;
return match *msg {
Panic(PanicMessage::BoundsCheck { ref len, ref index }) => {
use rustc::mir::interpret::PanicMessage::*;
return match msg {
BoundsCheck { ref len, ref index } => {
let len = self.read_immediate(self.eval_operand(len, None)?)
.expect("can't eval len").to_scalar()?
.to_bits(self.memory().pointer_size())? as u64;
let index = self.read_immediate(self.eval_operand(index, None)?)
.expect("can't eval index").to_scalar()?
.to_bits(self.memory().pointer_size())? as u64;
err!(Panic(PanicMessage::BoundsCheck { len, index }))
err!(Panic(BoundsCheck { len, index }))
}
Panic(PanicMessage::Overflow(op)) =>
Err(Panic(PanicMessage::Overflow(op)).into()),
Panic(PanicMessage::OverflowNeg) =>
Err(Panic(PanicMessage::OverflowNeg).into()),
Panic(PanicMessage::DivisionByZero) =>
Err(Panic(PanicMessage::DivisionByZero).into()),
Panic(PanicMessage::RemainderByZero) =>
Err(Panic(PanicMessage::RemainderByZero).into()),
GeneratorResumedAfterReturn |
GeneratorResumedAfterPanic => unimplemented!(),
_ => bug!(),
Overflow(op) =>
err!(Panic(Overflow(*op))),
OverflowNeg =>
err!(Panic(OverflowNeg)),
DivisionByZero =>
err!(Panic(DivisionByZero)),
RemainderByZero =>
err!(Panic(RemainderByZero)),
GeneratorResumedAfterReturn =>
err!(Panic(GeneratorResumedAfterReturn)),
GeneratorResumedAfterPanic =>
err!(Panic(GeneratorResumedAfterPanic)),
Panic { .. } =>
bug!("`Panic` variant cannot occur in MIR"),
};
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc::mir::{
use rustc::mir::visit::{
Visitor, PlaceContext, MutatingUseContext, MutVisitor, NonMutatingUseContext,
};
use rustc::mir::interpret::{InterpError::Panic, Scalar, GlobalId, InterpResult, PanicMessage};
use rustc::mir::interpret::{Scalar, GlobalId, InterpResult, InterpError, PanicMessage};
use rustc::ty::{self, Instance, ParamEnv, Ty, TyCtxt};
use syntax_pos::{Span, DUMMY_SP};
use rustc::ty::subst::InternalSubsts;
Expand Down Expand Up @@ -314,8 +314,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
| HeapAllocNonPowerOfTwoAlignment(_)
| Unreachable
| ReadFromReturnPointer
| GeneratorResumedAfterReturn
| GeneratorResumedAfterPanic
| ReferencedConstant
| InfiniteLoop
=> {
Expand Down Expand Up @@ -595,7 +593,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
)
} else {
if overflow {
let err = Panic(PanicMessage::Overflow(op)).into();
let err = InterpError::Panic(PanicMessage::Overflow(op)).into();
let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
return None;
}
Expand Down Expand Up @@ -809,7 +807,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
self.super_terminator(terminator, location);
let source_info = terminator.source_info;
match &mut terminator.kind {
TerminatorKind::Assert { expected, msg, ref mut cond, .. } => {
TerminatorKind::Assert { expected, ref msg, ref mut cond, .. } => {
if let Some(value) = self.eval_operand(&cond, source_info) {
trace!("assertion on {:?} should be {:?}", value, expected);
let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
Expand All @@ -831,13 +829,13 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
.hir()
.as_local_hir_id(self.source.def_id())
.expect("some part of a failing const eval must be local");
use rustc::mir::interpret::InterpError::*;
let msg = match msg {
Panic(PanicMessage::Overflow(_)) |
Panic(PanicMessage::OverflowNeg) |
Panic(PanicMessage::DivisionByZero) |
Panic(PanicMessage::RemainderByZero) => msg.description().to_owned(),
Panic(PanicMessage::BoundsCheck { ref len, ref index }) => {
PanicMessage::Overflow(_) |
PanicMessage::OverflowNeg |
PanicMessage::DivisionByZero |
PanicMessage::RemainderByZero =>
msg.description().to_owned(),
PanicMessage::BoundsCheck { ref len, ref index } => {
let len = self
.eval_operand(len, source_info)
.expect("len must be const");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ fn create_generator_resume_function<'tcx>(

let mut cases = create_cases(body, &transform, |point| Some(point.resume));

use rustc::mir::interpret::InterpError::{
use rustc::mir::interpret::PanicMessage::{
GeneratorResumedAfterPanic,
GeneratorResumedAfterReturn,
};
Expand Down

0 comments on commit e27927d

Please sign in to comment.