From 69daf844daac2f8c75ae0db662c584f05dd9838e Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Tue, 30 Jul 2019 15:25:12 +0530 Subject: [PATCH] adding throw_ and err_ macros for InterpError --- src/librustc/mir/interpret/allocation.rs | 6 ++-- src/librustc/mir/interpret/error.rs | 2 +- src/librustc/mir/interpret/interp_error.rs | 20 ----------- src/librustc/mir/interpret/mod.rs | 29 +++++++++++---- src/librustc/mir/interpret/pointer.rs | 6 ++-- src/librustc/mir/interpret/value.rs | 12 +++---- src/librustc_mir/const_eval.rs | 4 +-- src/librustc_mir/interpret/cast.rs | 4 +-- src/librustc_mir/interpret/eval_context.rs | 20 +++++------ src/librustc_mir/interpret/intern.rs | 2 +- src/librustc_mir/interpret/intrinsics.rs | 4 +-- src/librustc_mir/interpret/machine.rs | 2 +- src/librustc_mir/interpret/memory.rs | 42 +++++++++++----------- src/librustc_mir/interpret/operand.rs | 10 +++--- src/librustc_mir/interpret/operator.rs | 12 +++---- src/librustc_mir/interpret/place.rs | 4 +-- src/librustc_mir/interpret/step.rs | 2 +- src/librustc_mir/interpret/terminator.rs | 36 +++++++++---------- src/librustc_mir/interpret/traits.rs | 2 +- src/librustc_mir/interpret/validity.rs | 4 +-- src/librustc_mir/transform/const_prop.rs | 2 +- 21 files changed, 111 insertions(+), 114 deletions(-) delete mode 100644 src/librustc/mir/interpret/interp_error.rs diff --git a/src/librustc/mir/interpret/allocation.rs b/src/librustc/mir/interpret/allocation.rs index 51b2d0272a59..00f908007941 100644 --- a/src/librustc/mir/interpret/allocation.rs +++ b/src/librustc/mir/interpret/allocation.rs @@ -244,7 +244,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra> Allocation { 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 { if self.relocations(cx, ptr, size).is_empty() { Ok(()) } else { - err!(ReadPointerAsBytes) + throw_err!(ReadPointerAsBytes) } } @@ -516,7 +516,7 @@ impl<'tcx, Tag, Extra> Allocation { 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( diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 36635e62c15e..183298ee3f1e 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -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> { diff --git a/src/librustc/mir/interpret/interp_error.rs b/src/librustc/mir/interpret/interp_error.rs deleted file mode 100644 index c5a7adf1b671..000000000000 --- a/src/librustc/mir/interpret/interp_error.rs +++ /dev/null @@ -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)* - ) - }; -} diff --git a/src/librustc/mir/interpret/mod.rs b/src/librustc/mir/interpret/mod.rs index 064ff831e4ee..6e8e56a1b11f 100644 --- a/src/librustc/mir/interpret/mod.rs +++ b/src/librustc/mir/interpret/mod.rs @@ -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, diff --git a/src/librustc/mir/interpret/pointer.rs b/src/librustc/mir/interpret/pointer.rs index fbc726deb3dc..27a41bba4c1b 100644 --- a/src/librustc/mir/interpret/pointer.rs +++ b/src/librustc/mir/interpret/pointer.rs @@ -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 { 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(()) } diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index 4a59d845b3b4..9379054bdfc9 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -360,7 +360,7 @@ impl<'tcx, Tag> Scalar { Scalar::check_data(data, size); Ok(data) } - Scalar::Ptr(_) => err!(ReadPointerAsBytes), + Scalar::Ptr(_) => throw_err!(ReadPointerAsBytes), } } @@ -373,8 +373,8 @@ impl<'tcx, Tag> Scalar { #[inline] pub fn to_ptr(self) -> InterpResult<'tcx, Pointer> { 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 { 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 { 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 { pub fn not_undef(self) -> InterpResult<'static, Scalar> { match self { ScalarMaybeUndef::Scalar(scalar) => Ok(scalar), - ScalarMaybeUndef::Undef => err!(ReadUndefBytes(Size::from_bytes(0))), + ScalarMaybeUndef::Undef => throw_err!(ReadUndefBytes(Size::from_bytes(0))), } } diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index 97b89552cff3..786e2e71ec66 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -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>> { - err!(ReadForeignStatic) + throw_err!(ReadForeignStatic) } #[inline(always)] diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 4cb8351947b3..fb0ad26d5669 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -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))), } } diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index c0e61184780b..61ebcb58b91b 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -135,7 +135,7 @@ pub enum LocalValue { impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> { pub fn access(&self) -> InterpResult<'tcx, Operand> { 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, MemPlace>> { 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 { diff --git a/src/librustc_mir/interpret/intern.rs b/src/librustc_mir/interpret/intern.rs index af7da6fa5322..f362ea812f5f 100644 --- a/src/librustc_mir/interpret/intern.rs +++ b/src/librustc_mir/interpret/intern.rs @@ -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(()) diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index 0b4df6cc7bc5..d571d90e896f 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -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), )); } diff --git a/src/librustc_mir/interpret/machine.rs b/src/librustc_mir/interpret/machine.rs index 064ca0f4253b..5fc43c7712c5 100644 --- a/src/librustc_mir/interpret/machine.rs +++ b/src/librustc_mir/interpret/machine.rs @@ -251,6 +251,6 @@ pub trait Machine<'mir, 'tcx>: Sized { _mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer, ) -> InterpResult<'tcx, u64> { - err!(ReadPointerAsBytes) + throw_err!(ReadPointerAsBytes) } } diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index edaf7855e872..ee5607c49386 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -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, ) -> InterpResult<'tcx, Pointer> { 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(), )); } diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index cdc89e7055cd..346dd83f1d59 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -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) }, diff --git a/src/librustc_mir/interpret/operator.rs b/src/librustc_mir/interpret/operator.rs index eecb752e72be..5aea423e388a 100644 --- a/src/librustc_mir/interpret/operator.rs +++ b/src/librustc_mir/interpret/operator.rs @@ -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 (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)); } }; diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 75e3f70aebb3..4349c99a6196 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -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 diff --git a/src/librustc_mir/interpret/step.rs b/src/librustc_mir/interpret/step.rs index 246c90ba48e3..3aa0144fa8f6 100644 --- a/src/librustc_mir/interpret/step.rs +++ b/src/librustc_mir/interpret/step.rs @@ -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; diff --git a/src/librustc_mir/interpret/terminator.rs b/src/librustc_mir/interpret/terminator.rs index 51ed156f6cfc..539922010811 100644 --- a/src/librustc_mir/interpret/terminator.rs +++ b/src/librustc_mir/interpret/terminator.rs @@ -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(()) diff --git a/src/librustc_mir/interpret/traits.rs b/src/librustc_mir/interpret/traits.rs index 71f2cfceaf22..e55b0d0fb1f2 100644 --- a/src/librustc_mir/interpret/traits.rs +++ b/src/librustc_mir/interpret/traits.rs @@ -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 diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 9404711bd970..511ebfd023cd 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -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_, ))) diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 5043687f060d..db6b62e95ce0 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -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 => {