adding throw_ and err_ macros for InterpError

This commit is contained in:
Saleem Jaffer 2019-07-30 15:25:12 +05:30
parent 2a33fbff22
commit 69daf844da
21 changed files with 111 additions and 114 deletions

View File

@ -244,7 +244,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
Ok(&self.get_bytes(cx, ptr, size_with_null)?[..size])
}
// This includes the case where `offset` is out-of-bounds to begin with.
None => err!(UnterminatedCString(ptr.erase_tag())),
None => throw_err!(UnterminatedCString(ptr.erase_tag())),
}
}
@ -446,7 +446,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
if self.relocations(cx, ptr, size).is_empty() {
Ok(())
} else {
err!(ReadPointerAsBytes)
throw_err!(ReadPointerAsBytes)
}
}
@ -516,7 +516,7 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
self.undef_mask.is_range_defined(
ptr.offset,
ptr.offset + size,
).or_else(|idx| err!(ReadUndefBytes(idx)))
).or_else(|idx| throw_err!(ReadUndefBytes(idx)))
}
pub fn mark_definedness(

View File

@ -184,7 +184,7 @@ pub fn struct_error<'tcx>(tcx: TyCtxtAt<'tcx>, msg: &str) -> DiagnosticBuilder<'
/// Packages the kind of error we got from the const code interpreter
/// up with a Rust-level backtrace of where the error occured.
/// Thsese should always be constructed by calling `.into()` on
/// a `InterpError`. In `librustc_mir::interpret`, we have the `err!`
/// a `InterpError`. In `librustc_mir::interpret`, we have the `throw_err!`
/// macro for this.
#[derive(Debug, Clone)]
pub struct InterpErrorInfo<'tcx> {

View File

@ -1,20 +0,0 @@
//! macros to do something like `.ok_or_else(|| inval!(TooGeneric).into())` rather than
//! `.ok_or_else(|| InterpError::InvalidProgram(TooGeneric).into())`
#[macro_export]
macro_rules! inval {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::InvalidProgram(
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
)
};
}
#[macro_export]
macro_rules! unsup {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::Unsupported(
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
)
};
}

View File

@ -1,7 +1,7 @@
//! An interpreter for MIR used in CTFE and by miri
#[macro_export]
macro_rules! err {
macro_rules! throw_err {
($($tt:tt)*) => {
Err($crate::mir::interpret::InterpError::Unsupported(
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
@ -10,7 +10,7 @@ macro_rules! err {
}
#[macro_export]
macro_rules! err_inval {
macro_rules! throw_err_inval {
($($tt:tt)*) => {
Err($crate::mir::interpret::InterpError::InvalidProgram(
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
@ -19,7 +19,7 @@ macro_rules! err_inval {
}
#[macro_export]
macro_rules! err_ub {
macro_rules! throw_err_ub {
($($tt:tt)*) => {
Err($crate::mir::interpret::InterpError::UndefinedBehaviour(
$crate::mir::interpret::UndefinedBehaviourInfo::$($tt)*
@ -28,7 +28,7 @@ macro_rules! err_ub {
}
#[macro_export]
macro_rules! err_panic {
macro_rules! throw_err_panic {
($($tt:tt)*) => {
Err($crate::mir::interpret::InterpError::Panic(
$crate::mir::interpret::PanicInfo::$($tt)*
@ -37,7 +37,7 @@ macro_rules! err_panic {
}
#[macro_export]
macro_rules! err_exhaust {
macro_rules! throw_err_exhaust {
($($tt:tt)*) => {
Err($crate::mir::interpret::InterpError::ResourceExhaustion(
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
@ -45,11 +45,28 @@ macro_rules! err_exhaust {
};
}
#[macro_export]
macro_rules! err_inval {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::InvalidProgram(
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
)
};
}
#[macro_export]
macro_rules! err {
($($tt:tt)*) => {
$crate::mir::interpret::InterpError::Unsupported(
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
)
};
}
mod error;
mod value;
mod allocation;
mod pointer;
mod interp_error;
pub use self::error::{
InterpErrorInfo, InterpResult, InterpError, AssertMessage, ConstEvalErr, struct_error,

View File

@ -74,13 +74,13 @@ pub trait PointerArithmetic: layout::HasDataLayout {
#[inline]
fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
let (res, over) = self.overflowing_offset(val, i);
if over { err_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
if over { throw_err_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
}
#[inline]
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
let (res, over) = self.overflowing_signed_offset(val, i128::from(i));
if over { err_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
if over { throw_err_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
}
}
@ -196,7 +196,7 @@ impl<'tcx, Tag> Pointer<Tag> {
msg: CheckInAllocMsg,
) -> InterpResult<'tcx, ()> {
if self.offset > allocation_size {
err!(PointerOutOfBounds { ptr: self.erase_tag(), msg, allocation_size })
throw_err!(PointerOutOfBounds { ptr: self.erase_tag(), msg, allocation_size })
} else {
Ok(())
}

View File

@ -360,7 +360,7 @@ impl<'tcx, Tag> Scalar<Tag> {
Scalar::check_data(data, size);
Ok(data)
}
Scalar::Ptr(_) => err!(ReadPointerAsBytes),
Scalar::Ptr(_) => throw_err!(ReadPointerAsBytes),
}
}
@ -373,8 +373,8 @@ impl<'tcx, Tag> Scalar<Tag> {
#[inline]
pub fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
match self {
Scalar::Raw { data: 0, .. } => err!(InvalidNullPointerUsage),
Scalar::Raw { .. } => err!(ReadBytesAsPointer),
Scalar::Raw { data: 0, .. } => throw_err!(InvalidNullPointerUsage),
Scalar::Raw { .. } => throw_err!(ReadBytesAsPointer),
Scalar::Ptr(p) => Ok(p),
}
}
@ -406,7 +406,7 @@ impl<'tcx, Tag> Scalar<Tag> {
match self {
Scalar::Raw { data: 0, size: 1 } => Ok(false),
Scalar::Raw { data: 1, size: 1 } => Ok(true),
_ => err!(InvalidBool),
_ => throw_err!(InvalidBool),
}
}
@ -414,7 +414,7 @@ impl<'tcx, Tag> Scalar<Tag> {
let val = self.to_u32()?;
match ::std::char::from_u32(val) {
Some(c) => Ok(c),
None => err!(InvalidChar(val as u128)),
None => throw_err!(InvalidChar(val as u128)),
}
}
@ -537,7 +537,7 @@ impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
pub fn not_undef(self) -> InterpResult<'static, Scalar<Tag>> {
match self {
ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
ScalarMaybeUndef::Undef => err!(ReadUndefBytes(Size::from_bytes(0))),
ScalarMaybeUndef::Undef => throw_err!(ReadUndefBytes(Size::from_bytes(0))),
}
}

View File

@ -352,7 +352,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
ecx.goto_block(ret)?; // fully evaluated and done
Ok(None)
} else {
err!(MachineError(format!("calling non-const function `{}`", instance)))
throw_err!(MachineError(format!("calling non-const function `{}`", instance)))
};
}
}
@ -412,7 +412,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
_tcx: TyCtxt<'tcx>,
_def_id: DefId,
) -> InterpResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
err!(ReadForeignStatic)
throw_err!(ReadForeignStatic)
}
#[inline(always)]

View File

@ -85,7 +85,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
self.param_env,
def_id,
substs,
).ok_or_else(|| inval!(TooGeneric).into());
).ok_or_else(|| err_inval!(TooGeneric).into());
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance?));
self.write_scalar(Scalar::Ptr(fn_ptr.into()), dest)?;
}
@ -199,7 +199,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
},
// Casts to bool are not permitted by rustc, no need to handle them here.
_ => err!(Unimplemented(format!("int to {:?} cast", dest_layout.ty))),
_ => throw_err!(Unimplemented(format!("int to {:?} cast", dest_layout.ty))),
}
}

View File

@ -135,7 +135,7 @@ pub enum LocalValue<Tag=(), Id=AllocId> {
impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> {
pub fn access(&self) -> InterpResult<'tcx, Operand<Tag>> {
match self.value {
LocalValue::Dead => err!(DeadLocal),
LocalValue::Dead => throw_err!(DeadLocal),
LocalValue::Uninitialized =>
bug!("The type checker should prevent reading from a never-written local"),
LocalValue::Live(val) => Ok(val),
@ -148,7 +148,7 @@ impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> {
&mut self,
) -> InterpResult<'tcx, Result<&mut LocalValue<Tag>, MemPlace<Tag>>> {
match self.value {
LocalValue::Dead => err!(DeadLocal),
LocalValue::Dead => throw_err!(DeadLocal),
LocalValue::Live(Operand::Indirect(mplace)) => Ok(Err(mplace)),
ref mut local @ LocalValue::Live(Operand::Immediate(_)) |
ref mut local @ LocalValue::Uninitialized => {
@ -305,7 +305,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
&substs,
)),
None => if substs.needs_subst() {
err_inval!(TooGeneric)
throw_err_inval!(TooGeneric)
} else {
Ok(substs)
},
@ -326,7 +326,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
self.param_env,
def_id,
substs,
).ok_or_else(|| inval!(TooGeneric).into())
).ok_or_else(|| err_inval!(TooGeneric).into())
}
pub fn load_mir(
@ -339,14 +339,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
&& self.tcx.has_typeck_tables(did)
&& self.tcx.typeck_tables_of(did).tainted_by_errors
{
return err_inval!(TypeckError);
return throw_err_inval!(TypeckError);
}
trace!("load mir {:?}", instance);
match instance {
ty::InstanceDef::Item(def_id) => if self.tcx.is_mir_available(did) {
Ok(self.tcx.optimized_mir(did))
} else {
err!(NoMirFor(self.tcx.def_path_str(def_id)))
throw_err!(NoMirFor(self.tcx.def_path_str(def_id)))
},
_ => Ok(self.tcx.instance_mir(instance)),
}
@ -359,7 +359,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
match self.stack.last() {
Some(frame) => Ok(self.monomorphize_with_substs(t, frame.instance.substs)?),
None => if t.needs_subst() {
err_inval!(TooGeneric).into()
throw_err_inval!(TooGeneric).into()
} else {
Ok(t)
},
@ -376,7 +376,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let substituted = t.subst(*self.tcx, substs);
if substituted.needs_subst() {
return err_inval!(TooGeneric);
return throw_err_inval!(TooGeneric);
}
Ok(self.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), substituted))
@ -575,7 +575,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
info!("ENTERING({}) {}", self.cur_frame(), self.frame().instance);
if self.stack.len() > self.tcx.sess.const_eval_stack_frame_limit {
err_exhaust!(StackFrameLimitReached)
throw_err_exhaust!(StackFrameLimitReached)
} else {
Ok(())
}
@ -623,7 +623,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}
} else {
// Uh, that shouldn't happen... the function did not intend to return
return err_ub!(Unreachable);
return throw_err_ub!(Unreachable);
}
// Jump to new block -- *after* validation so that the spans make more sense.
match frame.return_to_block {

View File

@ -328,7 +328,7 @@ pub fn intern_const_alloc_recursive(
}
} else if ecx.memory().dead_alloc_map.contains_key(&alloc_id) {
// dangling pointer
return err!(ValidationFailure("encountered dangling pointer in final constant".into()))
return throw_err!(ValidationFailure("encountered dangling pointer in final constant".into()))
}
}
Ok(())

View File

@ -104,7 +104,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
};
let out_val = if intrinsic_name.ends_with("_nonzero") {
if bits == 0 {
return err!(Intrinsic(format!("{} called on 0", intrinsic_name)));
return throw_err!(Intrinsic(format!("{} called on 0", intrinsic_name)));
}
numeric_intrinsic(intrinsic_name.trim_end_matches("_nonzero"), bits, kind)?
} else {
@ -190,7 +190,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
if overflowed {
let layout = self.layout_of(substs.type_at(0))?;
let r_val = r.to_scalar()?.to_bits(layout.size)?;
return err!(Intrinsic(
return throw_err!(Intrinsic(
format!("Overflowing shift by {} in {}", r_val, intrinsic_name),
));
}

View File

@ -251,6 +251,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
_mem: &Memory<'mir, 'tcx, Self>,
_ptr: Pointer<Self::PointerTag>,
) -> InterpResult<'tcx, u64> {
err!(ReadPointerAsBytes)
throw_err!(ReadPointerAsBytes)
}
}

View File

@ -66,7 +66,7 @@ impl<'tcx, Other> FnVal<'tcx, Other> {
match self {
FnVal::Instance(instance) =>
Ok(instance),
FnVal::Other(_) => err!(MachineError(format!(
FnVal::Other(_) => throw_err!(MachineError(format!(
"Expected instance function pointer, got 'other' pointer"
))),
}
@ -202,7 +202,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
kind: MemoryKind<M::MemoryKinds>,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
if ptr.offset.bytes() != 0 {
return err!(ReallocateNonBasePtr);
return throw_err!(ReallocateNonBasePtr);
}
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
@ -243,7 +243,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
trace!("deallocating: {}", ptr.alloc_id);
if ptr.offset.bytes() != 0 {
return err!(DeallocateNonBasePtr);
return throw_err!(DeallocateNonBasePtr);
}
let (alloc_kind, mut alloc) = match self.alloc_map.remove(&ptr.alloc_id) {
@ -251,22 +251,22 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
None => {
// Deallocating static memory -- always an error
return match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
Some(GlobalAlloc::Function(..)) => err!(DeallocatedWrongMemoryKind(
Some(GlobalAlloc::Function(..)) => throw_err!(DeallocatedWrongMemoryKind(
"function".to_string(),
format!("{:?}", kind),
)),
Some(GlobalAlloc::Static(..)) |
Some(GlobalAlloc::Memory(..)) => err!(DeallocatedWrongMemoryKind(
Some(GlobalAlloc::Memory(..)) => throw_err!(DeallocatedWrongMemoryKind(
"static".to_string(),
format!("{:?}", kind),
)),
None => err!(DoubleFree)
None => throw_err!(DoubleFree)
}
}
};
if alloc_kind != kind {
return err!(DeallocatedWrongMemoryKind(
return throw_err!(DeallocatedWrongMemoryKind(
format!("{:?}", alloc_kind),
format!("{:?}", kind),
));
@ -274,7 +274,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
if let Some((size, align)) = old_size_and_align {
if size.bytes() != alloc.bytes.len() as u64 || align != alloc.align {
let bytes = Size::from_bytes(alloc.bytes.len() as u64);
return err!(IncorrectAllocationInformation(size, bytes, align, alloc.align));
return throw_err!(IncorrectAllocationInformation(size, bytes, align, alloc.align));
}
}
@ -319,7 +319,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
} else {
// The biggest power of two through which `offset` is divisible.
let offset_pow2 = 1 << offset.trailing_zeros();
err!(AlignmentCheckFailed {
throw_err!(AlignmentCheckFailed {
has: Align::from_bytes(offset_pow2).unwrap(),
required: align,
})
@ -341,7 +341,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
assert!(size.bytes() == 0);
// Must be non-NULL and aligned.
if bits == 0 {
return err!(InvalidNullPointerUsage);
return throw_err!(InvalidNullPointerUsage);
}
check_offset_align(bits, align)?;
None
@ -362,7 +362,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
// got picked we might be aligned even if this check fails.
// We instead have to fall back to converting to an integer and checking
// the "real" alignment.
return err!(AlignmentCheckFailed {
return throw_err!(AlignmentCheckFailed {
has: alloc_align,
required: align,
});
@ -413,9 +413,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
Some(GlobalAlloc::Memory(mem)) =>
Cow::Borrowed(mem),
Some(GlobalAlloc::Function(..)) =>
return err!(DerefFunctionPointer),
return throw_err!(DerefFunctionPointer),
None =>
return err!(DanglingPointerDeref),
return throw_err!(DanglingPointerDeref),
Some(GlobalAlloc::Static(def_id)) => {
// We got a "lazy" static that has not been computed yet.
if tcx.is_foreign_item(def_id) {
@ -505,11 +505,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
// to give us a cheap reference.
let alloc = Self::get_static_alloc(memory_extra, tcx, id)?;
if alloc.mutability == Mutability::Immutable {
return err!(ModifiedConstantMemory);
return throw_err!(ModifiedConstantMemory);
}
match M::STATIC_KIND {
Some(kind) => Ok((MemoryKind::Machine(kind), alloc.into_owned())),
None => err!(ModifiedStatic),
None => throw_err!(ModifiedStatic),
}
});
// Unpack the error type manually because type inference doesn't
@ -519,7 +519,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
Ok(a) => {
let a = &mut a.1;
if a.mutability == Mutability::Immutable {
return err!(ModifiedConstantMemory);
return throw_err!(ModifiedConstantMemory);
}
Ok(a)
}
@ -548,7 +548,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
if let Ok(_) = self.get_fn_alloc(id) {
return if let AllocCheck::Dereferencable = liveness {
// The caller requested no function pointers.
err!(DerefFunctionPointer)
throw_err!(DerefFunctionPointer)
} else {
Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
};
@ -579,7 +579,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
.expect("deallocated pointers should all be recorded in \
`dead_alloc_map`"))
} else {
err!(DanglingPointerDeref)
throw_err!(DanglingPointerDeref)
},
}
}
@ -591,7 +591,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
} else {
match self.tcx.alloc_map.lock().get(id) {
Some(GlobalAlloc::Function(instance)) => Ok(FnVal::Instance(instance)),
_ => err!(ExecuteMemory),
_ => throw_err!(ExecuteMemory),
}
}
}
@ -602,7 +602,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
) -> InterpResult<'tcx, FnVal<'tcx, M::ExtraFnVal>> {
let ptr = self.force_ptr(ptr)?; // We definitely need a pointer value.
if ptr.offset.bytes() != 0 {
return err!(InvalidFunctionPointer);
return throw_err!(InvalidFunctionPointer);
}
self.get_fn_alloc(ptr.alloc_id)
}
@ -837,7 +837,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
if (src.offset <= dest.offset && src.offset + size > dest.offset) ||
(dest.offset <= src.offset && dest.offset + size > src.offset)
{
return err!(Intrinsic(
return throw_err!(Intrinsic(
"copy_nonoverlapping called on overlapping ranges".to_string(),
));
}

View File

@ -461,7 +461,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
mir_place.iterate(|place_base, place_projection| {
let mut op = match place_base {
PlaceBase::Local(mir::RETURN_PLACE) =>
return err!(ReadFromReturnPointer),
return throw_err!(ReadFromReturnPointer),
PlaceBase::Local(local) => {
// Do not use the layout passed in as argument if the base we are looking at
// here is not the entire place.
@ -534,7 +534,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
match val.val {
ConstValue::Param(_) =>
// FIXME(oli-obk): try to monomorphize
return err_inval!(TooGeneric),
return throw_err_inval!(TooGeneric),
ConstValue::Unevaluated(def_id, substs) => {
let instance = self.resolve(def_id, substs)?;
return Ok(OpTy::from(self.const_eval_raw(GlobalId {
@ -610,7 +610,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let bits_discr = match raw_discr.to_bits(discr_val.layout.size) {
Ok(raw_discr) => raw_discr,
Err(_) =>
return err!(InvalidDiscriminant(raw_discr.erase_tag())),
return throw_err!(InvalidDiscriminant(raw_discr.erase_tag())),
};
let real_discr = if discr_val.layout.ty.is_signed() {
// going from layout tag type to typeck discriminant type
@ -637,7 +637,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
.find(|(_, var)| var.val == real_discr),
_ => bug!("tagged layout for non-adt non-generator"),
}.ok_or_else(
|| unsup!(InvalidDiscriminant(raw_discr.erase_tag()))
|| err!(InvalidDiscriminant(raw_discr.erase_tag()))
)?;
(real_discr, index.0)
},
@ -657,7 +657,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let ptr_valid = niche_start == 0 && variants_start == variants_end &&
!self.memory.ptr_may_be_null(ptr);
if !ptr_valid {
return err!(InvalidDiscriminant(raw_discr.erase_tag().into()));
return throw_err!(InvalidDiscriminant(raw_discr.erase_tag().into()));
}
(dataful_variant.as_u32() as u128, dataful_variant)
},

View File

@ -155,7 +155,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
r,
right_layout.ty
);
return err!(Unimplemented(msg));
return throw_err!(Unimplemented(msg));
}
// Operations that need special treatment for signed integers
@ -173,8 +173,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
return Ok((Scalar::from_bool(op(&l, &r)), false));
}
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
Div if r == 0 => return err_panic!(DivisionByZero),
Rem if r == 0 => return err_panic!(RemainderByZero),
Div if r == 0 => return throw_err_panic!(DivisionByZero),
Rem if r == 0 => return throw_err_panic!(RemainderByZero),
Div => Some(i128::overflowing_div),
Rem => Some(i128::overflowing_rem),
Add => Some(i128::overflowing_add),
@ -231,8 +231,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
Add => u128::overflowing_add,
Sub => u128::overflowing_sub,
Mul => u128::overflowing_mul,
Div if r == 0 => return err_panic!(DivisionByZero),
Rem if r == 0 => return err_panic!(RemainderByZero),
Div if r == 0 => return throw_err_panic!(DivisionByZero),
Rem if r == 0 => return throw_err_panic!(RemainderByZero),
Div => u128::overflowing_div,
Rem => u128::overflowing_rem,
_ => bug!(),
@ -250,7 +250,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
r,
right_layout.ty,
);
return err!(Unimplemented(msg));
return throw_err!(Unimplemented(msg));
}
};

View File

@ -356,7 +356,7 @@ where
// This can be violated because this runs during promotion on code where the
// type system has not yet ensured that such things don't happen.
debug!("tried to access element {} of array/slice with length {}", field, len);
return err_panic!(BoundsCheck { len, index: field });
return throw_err_panic!(BoundsCheck { len, index: field });
}
stride * field
}
@ -622,7 +622,7 @@ where
.layout_of(self.monomorphize(self.frame().body.return_ty())?)?,
}
}
None => return err!(InvalidNullPointerUsage),
None => return throw_err!(InvalidNullPointerUsage),
},
PlaceBase::Local(local) => PlaceTy {
// This works even for dead/uninitialized locals; we check further when writing

View File

@ -121,7 +121,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
// size of MIR constantly.
Nop => {}
InlineAsm { .. } => return err!(InlineAsm),
InlineAsm { .. } => return throw_err!(InlineAsm),
}
self.stack[frame_idx].stmt += 1;

View File

@ -19,7 +19,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
self.frame_mut().stmt = 0;
Ok(())
} else {
err_ub!(Unreachable)
throw_err_ub!(Unreachable)
}
}
@ -89,7 +89,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
},
_ => {
let msg = format!("can't handle callee of type {:?}", func.layout.ty);
return err!(Unimplemented(msg));
return throw_err!(Unimplemented(msg));
}
};
let args = self.eval_operands(args)?;
@ -144,20 +144,20 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
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!(BoundsCheck { len, index })
throw_err_panic!(BoundsCheck { len, index })
}
Overflow(op) =>
err_panic!(Overflow(*op)),
throw_err_panic!(Overflow(*op)),
OverflowNeg =>
err_panic!(OverflowNeg),
throw_err_panic!(OverflowNeg),
DivisionByZero =>
err_panic!(DivisionByZero),
throw_err_panic!(DivisionByZero),
RemainderByZero =>
err_panic!(RemainderByZero),
throw_err_panic!(RemainderByZero),
GeneratorResumedAfterReturn =>
err_panic!(GeneratorResumedAfterReturn),
throw_err_panic!(GeneratorResumedAfterReturn),
GeneratorResumedAfterPanic =>
err_panic!(GeneratorResumedAfterPanic),
throw_err_panic!(GeneratorResumedAfterPanic),
Panic { .. } =>
bug!("`Panic` variant cannot occur in MIR"),
};
@ -173,7 +173,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
`simplify_branches` mir pass"),
FalseUnwind { .. } => bug!("should have been eliminated by\
`simplify_branches` mir pass"),
Unreachable => return err_ub!(Unreachable),
Unreachable => return throw_err_ub!(Unreachable),
}
Ok(())
@ -220,13 +220,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
return Ok(());
}
let caller_arg = caller_arg.next()
.ok_or_else(|| unsup!(FunctionArgCountMismatch)) ?;
.ok_or_else(|| err!(FunctionArgCountMismatch)) ?;
if rust_abi {
debug_assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
}
// Now, check
if !Self::check_argument_compat(rust_abi, caller_arg.layout, callee_arg.layout) {
return err!(FunctionArgMismatch(caller_arg.layout.ty, callee_arg.layout.ty));
return throw_err!(FunctionArgMismatch(caller_arg.layout.ty, callee_arg.layout.ty));
}
// We allow some transmutes here
self.copy_op_transmute(caller_arg, callee_arg)
@ -254,13 +254,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
match instance.def {
ty::InstanceDef::Intrinsic(..) => {
if caller_abi != Abi::RustIntrinsic {
return err!(FunctionAbiMismatch(caller_abi, Abi::RustIntrinsic));
return throw_err!(FunctionAbiMismatch(caller_abi, Abi::RustIntrinsic));
}
// The intrinsic itself cannot diverge, so if we got here without a return
// place... (can happen e.g., for transmute returning `!`)
let dest = match dest {
Some(dest) => dest,
None => return err_ub!(Unreachable)
None => return throw_err_ub!(Unreachable)
};
M::call_intrinsic(self, instance, args, dest)?;
// No stack frame gets pushed, the main loop will just act as if the
@ -295,7 +295,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
abi,
};
if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
return err!(FunctionAbiMismatch(caller_abi, callee_abi));
return throw_err!(FunctionAbiMismatch(caller_abi, callee_abi));
}
}
@ -390,7 +390,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
// Now we should have no more caller args
if caller_iter.next().is_some() {
trace!("Caller has passed too many args");
return err!(FunctionArgCountMismatch);
return throw_err!(FunctionArgCountMismatch);
}
// Don't forget to check the return type!
if let Some(caller_ret) = dest {
@ -402,7 +402,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
caller_ret.layout,
callee_ret.layout,
) {
return err!(
return throw_err!(
FunctionRetMismatch(caller_ret.layout.ty, callee_ret.layout.ty)
);
}
@ -410,7 +410,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let local = mir::RETURN_PLACE;
let ty = self.frame().body.local_decls[local].ty;
if !self.tcx.is_ty_uninhabited_from_any_module(ty) {
return err!(FunctionRetMismatch(self.tcx.types.never, ty));
return throw_err!(FunctionRetMismatch(self.tcx.types.never, ty));
}
}
Ok(())

View File

@ -83,7 +83,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
self.param_env,
def_id,
substs,
).ok_or_else(|| inval!(TooGeneric))?;
).ok_or_else(|| err_inval!(TooGeneric))?;
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
let method_ptr = vtable.offset(ptr_size * (3 + i as u64), self)?;
self.memory

View File

@ -22,7 +22,7 @@ macro_rules! validation_failure {
} else {
format!(" at {}", where_)
};
err!(ValidationFailure(format!(
throw_err!(ValidationFailure(format!(
"encountered {}{}, but expected {}",
$what, where_, $details,
)))
@ -34,7 +34,7 @@ macro_rules! validation_failure {
} else {
format!(" at {}", where_)
};
err!(ValidationFailure(format!(
throw_err!(ValidationFailure(format!(
"encountered {}{}",
$what, where_,
)))

View File

@ -441,7 +441,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
// Need to do overflow check here: For actual CTFE, MIR
// generation emits code that does this before calling the op.
if prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
return err_panic!(OverflowNeg);
return throw_err_panic!(OverflowNeg);
}
}
UnOp::Not => {