Skip to content

Commit 8e9d0fa

Browse files
committed
adding a err macro for each of the InterpError variants
1 parent 9782b37 commit 8e9d0fa

File tree

18 files changed

+140
-124
lines changed

18 files changed

+140
-124
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use super::{
44
Pointer, InterpResult, AllocId, ScalarMaybeUndef, write_target_uint, read_target_uint, Scalar,
55
};
66

7-
use super::error::UnsupportedInfo::*;
87
use crate::ty::layout::{Size, Align};
98
use syntax::ast::Mutability;
109
use std::iter;
@@ -245,7 +244,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
245244
Ok(&self.get_bytes(cx, ptr, size_with_null)?[..size])
246245
}
247246
// This includes the case where `offset` is out-of-bounds to begin with.
248-
None => err!(Unsupported(UnterminatedCString(ptr.erase_tag()))),
247+
None => err!(UnterminatedCString(ptr.erase_tag())),
249248
}
250249
}
251250

@@ -447,7 +446,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
447446
if self.relocations(cx, ptr, size).is_empty() {
448447
Ok(())
449448
} else {
450-
err!(Unsupported(ReadPointerAsBytes))
449+
err!(ReadPointerAsBytes)
451450
}
452451
}
453452

@@ -517,7 +516,7 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
517516
self.undef_mask.is_range_defined(
518517
ptr.offset,
519518
ptr.offset + size,
520-
).or_else(|idx| err!(Unsupported(ReadUndefBytes(idx))))
519+
).or_else(|idx| err!(ReadUndefBytes(idx)))
521520
}
522521

523522
pub fn mark_definedness(

src/librustc/mir/interpret/mod.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,47 @@
22
33
#[macro_export]
44
macro_rules! err {
5-
($($tt:tt)*) => { Err($crate::mir::interpret::InterpError::$($tt)*.into()) };
5+
($($tt:tt)*) => {
6+
Err($crate::mir::interpret::InterpError::Unsupported(
7+
$crate::mir::interpret::UnsupportedInfo::$($tt)*
8+
).into())
9+
};
10+
}
11+
12+
#[macro_export]
13+
macro_rules! err_inval {
14+
($($tt:tt)*) => {
15+
Err($crate::mir::interpret::InterpError::InvalidProgram(
16+
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
17+
).into())
18+
};
19+
}
20+
21+
#[macro_export]
22+
macro_rules! err_ub {
23+
($($tt:tt)*) => {
24+
Err($crate::mir::interpret::InterpError::UndefinedBehaviour(
25+
$crate::mir::interpret::UndefinedBehaviourInfo::$($tt)*
26+
).into())
27+
};
28+
}
29+
30+
#[macro_export]
31+
macro_rules! err_panic {
32+
($($tt:tt)*) => {
33+
Err($crate::mir::interpret::InterpError::Panic(
34+
$crate::mir::interpret::PanicMessage::$($tt)*
35+
).into())
36+
};
37+
}
38+
39+
#[macro_export]
40+
macro_rules! err_exhaust {
41+
($($tt:tt)*) => {
42+
Err($crate::mir::interpret::InterpError::ResourceExhaustion(
43+
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
44+
).into())
45+
};
646
}
747

848
mod error;

src/librustc/mir/interpret/pointer.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use std::fmt::{self, Display};
22

3-
use super::error::UnsupportedInfo::*;
43
use crate::mir;
54
use crate::ty::layout::{self, HasDataLayout, Size};
65
use rustc_macros::HashStable;
76

8-
use super::{
9-
AllocId, InterpResult, PanicMessage
10-
};
7+
use super::{AllocId, InterpResult};
118

129
/// Used by `check_in_alloc` to indicate context of check
1310
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
@@ -77,13 +74,13 @@ pub trait PointerArithmetic: layout::HasDataLayout {
7774
#[inline]
7875
fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
7976
let (res, over) = self.overflowing_offset(val, i);
80-
if over { err!(Panic(PanicMessage::Overflow(mir::BinOp::Add))) } else { Ok(res) }
77+
if over { err_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
8178
}
8279

8380
#[inline]
8481
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
8582
let (res, over) = self.overflowing_signed_offset(val, i128::from(i));
86-
if over { err!(Panic(PanicMessage::Overflow(mir::BinOp::Add))) } else { Ok(res) }
83+
if over { err_panic!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
8784
}
8885
}
8986

@@ -199,11 +196,7 @@ impl<'tcx, Tag> Pointer<Tag> {
199196
msg: CheckInAllocMsg,
200197
) -> InterpResult<'tcx, ()> {
201198
if self.offset > allocation_size {
202-
err!(Unsupported(PointerOutOfBounds {
203-
ptr: self.erase_tag(),
204-
msg,
205-
allocation_size,
206-
}))
199+
err!(PointerOutOfBounds { ptr: self.erase_tag(),msg,allocation_size })
207200
} else {
208201
Ok(())
209202
}

src/librustc/mir/interpret/value.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::fmt;
22
use rustc_macros::HashStable;
33
use rustc_apfloat::{Float, ieee::{Double, Single}};
44

5-
use super::error::UnsupportedInfo::*;
65
use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size, Align}, subst::SubstsRef};
76
use crate::ty::PlaceholderConst;
87
use crate::hir::def_id::DefId;
@@ -361,7 +360,7 @@ impl<'tcx, Tag> Scalar<Tag> {
361360
Scalar::check_data(data, size);
362361
Ok(data)
363362
}
364-
Scalar::Ptr(_) => err!(Unsupported(ReadPointerAsBytes)),
363+
Scalar::Ptr(_) => err!(ReadPointerAsBytes),
365364
}
366365
}
367366

@@ -374,8 +373,8 @@ impl<'tcx, Tag> Scalar<Tag> {
374373
#[inline]
375374
pub fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
376375
match self {
377-
Scalar::Raw { data: 0, .. } => err!(Unsupported(InvalidNullPointerUsage)),
378-
Scalar::Raw { .. } => err!(Unsupported(ReadBytesAsPointer)),
376+
Scalar::Raw { data: 0, .. } => err!(InvalidNullPointerUsage),
377+
Scalar::Raw { .. } => err!(ReadBytesAsPointer),
379378
Scalar::Ptr(p) => Ok(p),
380379
}
381380
}
@@ -407,15 +406,15 @@ impl<'tcx, Tag> Scalar<Tag> {
407406
match self {
408407
Scalar::Raw { data: 0, size: 1 } => Ok(false),
409408
Scalar::Raw { data: 1, size: 1 } => Ok(true),
410-
_ => err!(Unsupported(InvalidBool)),
409+
_ => err!(InvalidBool),
411410
}
412411
}
413412

414413
pub fn to_char(self) -> InterpResult<'tcx, char> {
415414
let val = self.to_u32()?;
416415
match ::std::char::from_u32(val) {
417416
Some(c) => Ok(c),
418-
None => err!(Unsupported(InvalidChar(val as u128))),
417+
None => err!(InvalidChar(val as u128)),
419418
}
420419
}
421420

@@ -538,7 +537,7 @@ impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
538537
pub fn not_undef(self) -> InterpResult<'static, Scalar<Tag>> {
539538
match self {
540539
ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
541-
ScalarMaybeUndef::Undef => err!(Unsupported(ReadUndefBytes(Size::from_bytes(0)))),
540+
ScalarMaybeUndef::Undef => err!(ReadUndefBytes(Size::from_bytes(0))),
542541
}
543542
}
544543

src/librustc_mir/const_eval.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
353353
ecx.goto_block(ret)?; // fully evaluated and done
354354
Ok(None)
355355
} else {
356-
err!(Unsupported(
357-
MachineError(format!("calling non-const function `{}`", instance))
358-
))
356+
err!(MachineError(format!("calling non-const function `{}`", instance)))
359357
};
360358
}
361359
}
@@ -415,7 +413,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
415413
_tcx: TyCtxt<'tcx>,
416414
_def_id: DefId,
417415
) -> InterpResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
418-
err!(Unsupported(ReadForeignStatic))
416+
err!(ReadForeignStatic)
419417
}
420418

421419
#[inline(always)]

src/librustc_mir/interpret/cast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_apfloat::{Float, FloatConvert};
99
use rustc::mir::interpret::{
1010
Scalar, InterpResult, Pointer, PointerArithmetic, InterpError,
1111
};
12-
use rustc::mir::{CastKind, interpret::{UnsupportedInfo::*, InvalidProgramInfo::*}};
12+
use rustc::mir::{CastKind, interpret::{InvalidProgramInfo::*}};
1313

1414

1515
use super::{InterpCx, Machine, PlaceTy, OpTy, Immediate, FnVal};
@@ -200,7 +200,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
200200
},
201201

202202
// Casts to bool are not permitted by rustc, no need to handle them here.
203-
_ => err!(Unsupported(Unimplemented(format!("int to {:?} cast", dest_layout.ty)))),
203+
_ => err!(Unimplemented(format!("int to {:?} cast", dest_layout.ty))),
204204
}
205205
}
206206

src/librustc_mir/interpret/eval_context.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use rustc::mir::interpret::{
1717
ErrorHandled,
1818
GlobalId, Scalar, Pointer, FrameInfo, AllocId,
1919
InterpResult, InterpError,
20-
truncate, sign_extend, UnsupportedInfo::*, InvalidProgramInfo::*,
21-
ResourceExhaustionInfo::*, UndefinedBehaviourInfo::*,
20+
truncate, sign_extend, InvalidProgramInfo::*,
2221
};
2322
use rustc_data_structures::fx::FxHashMap;
2423

@@ -136,7 +135,7 @@ pub enum LocalValue<Tag=(), Id=AllocId> {
136135
impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> {
137136
pub fn access(&self) -> InterpResult<'tcx, Operand<Tag>> {
138137
match self.value {
139-
LocalValue::Dead => err!(Unsupported(DeadLocal)),
138+
LocalValue::Dead => err!(DeadLocal),
140139
LocalValue::Uninitialized =>
141140
bug!("The type checker should prevent reading from a never-written local"),
142141
LocalValue::Live(val) => Ok(val),
@@ -149,7 +148,7 @@ impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> {
149148
&mut self,
150149
) -> InterpResult<'tcx, Result<&mut LocalValue<Tag>, MemPlace<Tag>>> {
151150
match self.value {
152-
LocalValue::Dead => err!(Unsupported(DeadLocal)),
151+
LocalValue::Dead => err!(DeadLocal),
153152
LocalValue::Live(Operand::Indirect(mplace)) => Ok(Err(mplace)),
154153
ref mut local @ LocalValue::Live(Operand::Immediate(_)) |
155154
ref mut local @ LocalValue::Uninitialized => {
@@ -303,7 +302,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
303302
&substs,
304303
)),
305304
None => if substs.needs_subst() {
306-
err!(InvalidProgram(TooGeneric)).into()
305+
err_inval!(TooGeneric).into()
307306
} else {
308307
Ok(substs)
309308
},
@@ -337,14 +336,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
337336
&& self.tcx.has_typeck_tables(did)
338337
&& self.tcx.typeck_tables_of(did).tainted_by_errors
339338
{
340-
return err!(InvalidProgram(TypeckError));
339+
return err_inval!(TypeckError);
341340
}
342341
trace!("load mir {:?}", instance);
343342
match instance {
344343
ty::InstanceDef::Item(def_id) => if self.tcx.is_mir_available(did) {
345344
Ok(self.tcx.optimized_mir(did))
346345
} else {
347-
err!(Unsupported(NoMirFor(self.tcx.def_path_str(def_id))))
346+
err!(NoMirFor(self.tcx.def_path_str(def_id)))
348347
},
349348
_ => Ok(self.tcx.instance_mir(instance)),
350349
}
@@ -357,7 +356,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
357356
match self.stack.last() {
358357
Some(frame) => Ok(self.monomorphize_with_substs(t, frame.instance.substs)?),
359358
None => if t.needs_subst() {
360-
err!(InvalidProgram(TooGeneric)).into()
359+
err_inval!(TooGeneric).into()
361360
} else {
362361
Ok(t)
363362
},
@@ -374,7 +373,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
374373
let substituted = t.subst(*self.tcx, substs);
375374

376375
if substituted.needs_subst() {
377-
return err!(InvalidProgram(TooGeneric));
376+
return err_inval!(TooGeneric);
378377
}
379378

380379
Ok(self.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), substituted))
@@ -573,7 +572,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
573572
info!("ENTERING({}) {}", self.cur_frame(), self.frame().instance);
574573

575574
if self.stack.len() > self.tcx.sess.const_eval_stack_frame_limit {
576-
err!(ResourceExhaustion(StackFrameLimitReached))
575+
err_exhaust!(StackFrameLimitReached)
577576
} else {
578577
Ok(())
579578
}
@@ -621,7 +620,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
621620
}
622621
} else {
623622
// Uh, that shouldn't happen... the function did not intend to return
624-
return err!(UndefinedBehaviour(Unreachable));
623+
return err_ub!(Unreachable);
625624
}
626625
// Jump to new block -- *after* validation so that the spans make more sense.
627626
match frame.return_to_block {

src/librustc_mir/interpret/intern.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,7 @@ pub fn intern_const_alloc_recursive(
328328
}
329329
} else if ecx.memory().dead_alloc_map.contains_key(&alloc_id) {
330330
// dangling pointer
331-
return err!(Unsupported(ValidationFailure(
332-
"encountered dangling pointer in final constant".into(),
333-
)))
331+
return err!(ValidationFailure("encountered dangling pointer in final constant".into()))
334332
}
335333
}
336334
Ok(())

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
104104
};
105105
let out_val = if intrinsic_name.ends_with("_nonzero") {
106106
if bits == 0 {
107-
return err!(
108-
Unsupported(Intrinsic(format!("{} called on 0", intrinsic_name)))
109-
);
107+
return err!(Intrinsic(format!("{} called on 0", intrinsic_name)));
110108
}
111109
numeric_intrinsic(intrinsic_name.trim_end_matches("_nonzero"), bits, kind)?
112110
} else {
@@ -192,9 +190,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
192190
if overflowed {
193191
let layout = self.layout_of(substs.type_at(0))?;
194192
let r_val = r.to_scalar()?.to_bits(layout.size)?;
195-
return err!(Unsupported(Intrinsic(
193+
return err!(Intrinsic(
196194
format!("Overflowing shift by {} in {}", r_val, intrinsic_name),
197-
)));
195+
));
198196
}
199197
self.write_scalar(val, dest)?;
200198
}

src/librustc_mir/interpret/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
251251
_mem: &Memory<'mir, 'tcx, Self>,
252252
_ptr: Pointer<Self::PointerTag>,
253253
) -> InterpResult<'tcx, u64> {
254-
err!(Unsupported(ReadPointerAsBytes))
254+
err!(ReadPointerAsBytes)
255255
}
256256
}

0 commit comments

Comments
 (0)