Skip to content

Commit 410385d

Browse files
committed
add macro to reduce boilerplate and keep readable messages
1 parent cda81da commit 410385d

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/librustc/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl InterpError<'_> {
577577
/// waste of resources.
578578
pub fn allocates(&self) -> bool {
579579
match self {
580-
// Zero-sized boxes to not allocate.
580+
// Zero-sized boxes do not allocate.
581581
InterpError::MachineStop(b) => mem::size_of_val(&**b) > 0,
582582
InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_))
583583
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationFailure(_))

src/librustc_mir/transform/const_prop.rs

+24-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::borrow::Cow;
55
use std::cell::Cell;
66

7-
use rustc::mir::interpret::{InterpResult, MachineStopType, Scalar};
7+
use rustc::mir::interpret::{InterpResult, Scalar};
88
use rustc::mir::visit::{
99
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
1010
};
@@ -39,6 +39,24 @@ use crate::transform::{MirPass, MirSource};
3939
/// The maximum number of bytes that we'll allocate space for a return value.
4040
const MAX_ALLOC_LIMIT: u64 = 1024;
4141

42+
/// Macro for machine-specific `InterpError` without allocation.
43+
/// (These will never be shown to the user, but they help diagnose ICEs.)
44+
macro_rules! throw_machine_stop_str {
45+
($($tt:tt)*) => {{
46+
// We make a new local type for it. The type itself does not carry any information,
47+
// but its vtable (for the `MachineStopType` trait) does.
48+
struct Zst;
49+
// Debug-printing this type shows the desired string.
50+
impl std::fmt::Debug for Zst {
51+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52+
write!(f, $($tt)*)
53+
}
54+
}
55+
impl rustc::mir::interpret::MachineStopType for Zst {}
56+
throw_machine_stop!(Zst)
57+
}};
58+
}
59+
4260
pub struct ConstProp;
4361

4462
impl<'tcx> MirPass<'tcx> for ConstProp {
@@ -192,10 +210,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
192210
_ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
193211
_unwind: Option<BasicBlock>,
194212
) -> InterpResult<'tcx> {
195-
#[derive(Debug)]
196-
struct ConstPropIntrinsic;
197-
impl MachineStopType for ConstPropIntrinsic {}
198-
throw_machine_stop!(ConstPropIntrinsic)
213+
throw_machine_stop_str!("calling intrinsics isn't supported in ConstProp")
199214
}
200215

201216
fn assert_panic(
@@ -216,11 +231,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
216231
_left: ImmTy<'tcx>,
217232
_right: ImmTy<'tcx>,
218233
) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
219-
#[derive(Debug)]
220-
struct ConstPropPtrOp;
221-
impl MachineStopType for ConstPropPtrOp {}
222234
// We can't do this because aliasing of memory can differ between const eval and llvm
223-
throw_machine_stop!(ConstPropPtrOp)
235+
throw_machine_stop_str!("pointer arithmetic or comparisons aren't supported in ConstProp")
224236
}
225237

226238
#[inline(always)]
@@ -243,10 +255,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
243255
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
244256
_dest: PlaceTy<'tcx>,
245257
) -> InterpResult<'tcx> {
246-
#[derive(Debug)]
247-
struct ConstPropBox;
248-
impl MachineStopType for ConstPropBox {}
249-
throw_machine_stop!(ConstPropBox)
258+
throw_machine_stop_str!("can't const prop heap allocations")
250259
}
251260

252261
fn access_local(
@@ -257,10 +266,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
257266
let l = &frame.locals[local];
258267

259268
if l.value == LocalValue::Uninitialized {
260-
#[derive(Debug)]
261-
struct ConstPropUninitLocal;
262-
impl MachineStopType for ConstPropUninitLocal {}
263-
throw_machine_stop!(ConstPropUninitLocal)
269+
throw_machine_stop_str!("tried to access an uninitialized local")
264270
}
265271

266272
l.access()
@@ -270,13 +276,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
270276
_memory_extra: &(),
271277
allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
272278
) -> InterpResult<'tcx> {
273-
#[derive(Debug)]
274-
struct ConstPropGlobalMem;
275-
impl MachineStopType for ConstPropGlobalMem {}
276279
// if the static allocation is mutable or if it has relocations (it may be legal to mutate
277280
// the memory behind that in the future), then we can't const prop it
278281
if allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0 {
279-
throw_machine_stop!(ConstPropGlobalMem)
282+
throw_machine_stop_str!("can't eval mutable statics in ConstProp")
280283
}
281284

282285
Ok(())

0 commit comments

Comments
 (0)