Skip to content

Commit 152f0d3

Browse files
committed
code review fixes
1 parent 5585445 commit 152f0d3

File tree

13 files changed

+105
-109
lines changed

13 files changed

+105
-109
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,12 @@ impl<'tcx> ConstEvalErr<'tcx> {
137137
message: &str,
138138
lint_root: Option<hir::HirId>,
139139
) -> Result<DiagnosticBuilder<'tcx>, ErrorHandled> {
140-
use InvalidProgramInfo::*;
141140
match self.error {
142-
InterpError::InvalidProgram(Layout(LayoutError::Unknown(_))) |
143-
InterpError::InvalidProgram(TooGeneric) =>
141+
err_inval!(Layout(LayoutError::Unknown(_))) |
142+
err_inval!(TooGeneric) =>
144143
return Err(ErrorHandled::TooGeneric),
145-
InterpError::InvalidProgram(Layout(LayoutError::SizeOverflow(_))) |
146-
InterpError::InvalidProgram(TypeckError) =>
144+
err_inval!(Layout(LayoutError::SizeOverflow(_))) |
145+
err_inval!(TypeckError) =>
147146
return Err(ErrorHandled::Reported),
148147
_ => {},
149148
}
@@ -549,7 +548,9 @@ impl fmt::Debug for UnsupportedOpInfo<'tcx> {
549548

550549
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
551550
pub enum ResourceExhaustionInfo {
551+
/// The stack grew too big.
552552
StackFrameLimitReached,
553+
/// The program ran into an infinite loop.
553554
InfiniteLoop,
554555
}
555556

src/librustc/mir/interpret/mod.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@ macro_rules! throw_ub {
2121

2222
#[macro_export]
2323
macro_rules! throw_panic {
24-
($($tt:tt)*) => {
25-
return Err($crate::mir::interpret::InterpError::Panic(
26-
$crate::mir::interpret::PanicInfo::$($tt)*
27-
).into())
28-
};
24+
($($tt:tt)*) => { return Err(err_panic!($($tt)*).into()) };
2925
}
3026

3127
#[macro_export]
3228
macro_rules! throw_exhaust {
33-
($($tt:tt)*) => {
34-
return Err($crate::mir::interpret::InterpError::ResourceExhaustion(
35-
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
36-
).into())
37-
};
29+
($($tt:tt)*) => { return Err(err_exhaust!($($tt)*).into()) };
3830
}
3931

4032
#[macro_export]
@@ -55,6 +47,24 @@ macro_rules! err_unsup {
5547
};
5648
}
5749

50+
#[macro_export]
51+
macro_rules! err_exhaust {
52+
($($tt:tt)*) => {
53+
$crate::mir::interpret::InterpError::ResourceExhaustion(
54+
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
55+
)
56+
};
57+
}
58+
59+
#[macro_export]
60+
macro_rules! err_panic {
61+
($($tt:tt)*) => {
62+
$crate::mir::interpret::InterpError::Panic(
63+
$crate::mir::interpret::PanicInfo::$($tt)*
64+
)
65+
};
66+
}
67+
5868
mod error;
5969
mod value;
6070
mod allocation;

src/librustc_mir/const_eval.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use syntax::source_map::{Span, DUMMY_SP};
2222
use crate::interpret::{self,
2323
PlaceTy, MPlaceTy, OpTy, ImmTy, Immediate, Scalar,
2424
RawConst, ConstValue,
25-
InterpResult, InterpErrorInfo, InterpError, GlobalId, InterpCx, StackPopCleanup,
25+
InterpResult, InterpErrorInfo, GlobalId, InterpCx, StackPopCleanup,
2626
Allocation, AllocId, MemoryKind,
27-
snapshot, RefTracking, intern_const_alloc_recursive, UnsupportedOpInfo,
27+
snapshot, RefTracking, intern_const_alloc_recursive,
2828
};
2929

3030
/// Number of steps until the detector even starts doing anything.
@@ -183,7 +183,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
183183

184184
impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalError {
185185
fn into(self) -> InterpErrorInfo<'tcx> {
186-
InterpError::Unsupported(UnsupportedOpInfo::MachineError(self.to_string())).into()
186+
err_unsup!(MachineError(self.to_string())).into()
187187
}
188188
}
189189

@@ -360,7 +360,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
360360
Ok(Some(match ecx.load_mir(instance.def) {
361361
Ok(body) => body,
362362
Err(err) => {
363-
if let InterpError::Unsupported(UnsupportedOpInfo::NoMirFor(ref path)) = err.kind {
363+
if let err_unsup!(NoMirFor(ref path)) = err.kind {
364364
return Err(
365365
ConstEvalError::NeedsRfc(format!("calling extern function `{}`", path))
366366
.into(),
@@ -697,9 +697,8 @@ pub fn const_eval_raw_provider<'tcx>(
697697
// promoting runtime code is only allowed to error if it references broken constants
698698
// any other kind of error will be reported to the user as a deny-by-default lint
699699
_ => if let Some(p) = cid.promoted {
700-
use crate::interpret::InvalidProgramInfo::ReferencedConstant;
701700
let span = tcx.promoted_mir(def_id)[p].span;
702-
if let InterpError::InvalidProgram(ReferencedConstant) = err.error {
701+
if let err_inval!(ReferencedConstant) = err.error {
703702
err.report_as_error(
704703
tcx.at(span),
705704
"evaluation of constant expression failed",

src/librustc_mir/interpret/eval_context.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use rustc_data_structures::indexed_vec::IndexVec;
1616
use rustc::mir::interpret::{
1717
ErrorHandled,
1818
GlobalId, Scalar, Pointer, FrameInfo, AllocId,
19-
InterpResult, InterpError,
20-
truncate, sign_extend, InvalidProgramInfo,
19+
InterpResult, truncate, sign_extend,
2120
};
2221
use rustc_data_structures::fx::FxHashMap;
2322

@@ -193,7 +192,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> LayoutOf for InterpCx<'mir, 'tcx, M> {
193192
self.tcx
194193
.layout_of(self.param_env.and(ty))
195194
.map_err(|layout| {
196-
InterpError::InvalidProgram(InvalidProgramInfo::Layout(layout)).into()
195+
err_inval!(Layout(layout)).into()
197196
})
198197
}
199198
}
@@ -698,9 +697,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
698697
let val = self.tcx.const_eval_raw(param_env.and(gid)).map_err(|err| {
699698
match err {
700699
ErrorHandled::Reported =>
701-
InterpError::InvalidProgram(InvalidProgramInfo::ReferencedConstant),
700+
err_inval!(ReferencedConstant),
702701
ErrorHandled::TooGeneric =>
703-
InterpError::InvalidProgram(InvalidProgramInfo::TooGeneric),
702+
err_inval!(TooGeneric),
704703
}
705704
})?;
706705
self.raw_const_to_mplace(val)

src/librustc_mir/interpret/intern.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
//! memory, we need to extract all memory allocations to the global memory pool so they stay around.
55
66
use rustc::ty::{Ty, TyCtxt, ParamEnv, self};
7-
use rustc::mir::interpret::{
8-
InterpResult, ErrorHandled, UnsupportedOpInfo,
9-
};
7+
use rustc::mir::interpret::{InterpResult, ErrorHandled};
108
use rustc::hir;
119
use rustc::hir::def_id::DefId;
1210
use super::validity::RefTracking;
@@ -16,7 +14,7 @@ use syntax::ast::Mutability;
1614
use syntax_pos::Span;
1715

1816
use super::{
19-
ValueVisitor, MemoryKind, Pointer, AllocId, MPlaceTy, InterpError, Scalar,
17+
ValueVisitor, MemoryKind, Pointer, AllocId, MPlaceTy, Scalar,
2018
};
2119
use crate::const_eval::{CompileTimeInterpreter, CompileTimeEvalContext};
2220

@@ -293,7 +291,7 @@ pub fn intern_const_alloc_recursive(
293291
if let Err(error) = interned {
294292
// This can happen when e.g. the tag of an enum is not a valid discriminant. We do have
295293
// to read enum discriminants in order to find references in enum variant fields.
296-
if let InterpError::Unsupported(UnsupportedOpInfo::ValidationFailure(_)) = error.kind {
294+
if let err_unsup!(ValidationFailure(_)) = error.kind {
297295
let err = crate::const_eval::error_to_const_error(&ecx, error);
298296
match err.struct_error(ecx.tcx, "it is undefined behavior to use this value") {
299297
Ok(mut diag) => {

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use syntax::symbol::Symbol;
66
use rustc::ty;
77
use rustc::ty::layout::{LayoutOf, Primitive, Size};
88
use rustc::mir::BinOp;
9-
use rustc::mir::interpret::{
10-
InterpResult, InterpError, Scalar, PanicInfo, UnsupportedOpInfo,
11-
};
9+
use rustc::mir::interpret::{InterpResult, Scalar};
1210

1311
use super::{
1412
Machine, PlaceTy, OpTy, InterpCx, Immediate,
@@ -100,7 +98,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10098
let bits = self.read_scalar(args[0])?.to_bits(layout_of.size)?;
10199
let kind = match layout_of.abi {
102100
ty::layout::Abi::Scalar(ref scalar) => scalar.value,
103-
_ => Err(InterpError::Unsupported(UnsupportedOpInfo::TypeNotPrimitive(ty)))?,
101+
_ => Err(err_unsup!(TypeNotPrimitive(ty)))?,
104102
};
105103
let out_val = if intrinsic_name.ends_with("_nonzero") {
106104
if bits == 0 {
@@ -250,7 +248,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
250248
let file = Symbol::intern(self.read_str(file_place)?);
251249
let line = self.read_scalar(line.into())?.to_u32()?;
252250
let col = self.read_scalar(col.into())?.to_u32()?;
253-
return Err(InterpError::Panic(PanicInfo::Panic { msg, file, line, col }).into());
251+
throw_panic!(Panic { msg, file, line, col })
254252
} else if Some(def_id) == self.tcx.lang_items().begin_panic_fn() {
255253
assert!(args.len() == 2);
256254
// &'static str, &(&'static str, u32, u32)
@@ -268,7 +266,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
268266
let file = Symbol::intern(self.read_str(file_place)?);
269267
let line = self.read_scalar(line.into())?.to_u32()?;
270268
let col = self.read_scalar(col.into())?.to_u32()?;
271-
return Err(InterpError::Panic(PanicInfo::Panic { msg, file, line, col }).into());
269+
throw_panic!(Panic { msg, file, line, col })
272270
} else {
273271
return Ok(false);
274272
}

src/librustc_mir/interpret/machine.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc::mir;
1010
use rustc::ty::{self, TyCtxt};
1111

1212
use super::{
13-
Allocation, AllocId, InterpResult, InterpError, Scalar, AllocationExtra,
14-
InterpCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer, Memory, UnsupportedOpInfo,
13+
Allocation, AllocId, InterpResult, Scalar, AllocationExtra,
14+
InterpCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer, Memory,
1515
};
1616

1717
/// Whether this kind of memory is allowed to leak
@@ -240,9 +240,9 @@ pub trait Machine<'mir, 'tcx>: Sized {
240240
int: u64,
241241
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
242242
Err((if int == 0 {
243-
InterpError::Unsupported(UnsupportedOpInfo::InvalidNullPointerUsage)
243+
err_unsup!(InvalidNullPointerUsage)
244244
} else {
245-
InterpError::Unsupported(UnsupportedOpInfo::ReadBytesAsPointer)
245+
err_unsup!(ReadBytesAsPointer)
246246
}).into())
247247
}
248248

src/librustc_mir/interpret/memory.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use syntax::ast::Mutability;
1818

1919
use super::{
2020
Pointer, AllocId, Allocation, GlobalId, AllocationExtra,
21-
InterpResult, Scalar, InterpError, GlobalAlloc, PointerArithmetic,
22-
Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg, InvalidProgramInfo,
21+
InterpResult, Scalar, GlobalAlloc, PointerArithmetic,
22+
Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg,
2323
};
2424

2525
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
@@ -250,18 +250,17 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
250250
Some(alloc) => alloc,
251251
None => {
252252
// Deallocating static memory -- always an error
253-
match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
254-
Some(GlobalAlloc::Function(..)) => throw_unsup!(DeallocatedWrongMemoryKind(
253+
return Err(match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
254+
Some(GlobalAlloc::Function(..)) => err_unsup!(DeallocatedWrongMemoryKind(
255255
"function".to_string(),
256256
format!("{:?}", kind),
257257
)),
258-
Some(GlobalAlloc::Static(..)) |
259-
Some(GlobalAlloc::Memory(..)) => throw_unsup!(DeallocatedWrongMemoryKind(
260-
"static".to_string(),
261-
format!("{:?}", kind),
262-
)),
263-
None => throw_unsup!(DoubleFree)
258+
Some(GlobalAlloc::Static(..)) | Some(GlobalAlloc::Memory(..)) => err_unsup!(
259+
DeallocatedWrongMemoryKind("static".to_string(), format!("{:?}", kind))
260+
),
261+
None => err_unsup!(DoubleFree),
264262
}
263+
.into());
265264
}
266265
};
267266

@@ -437,11 +436,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
437436
assert!(tcx.is_static(def_id));
438437
match err {
439438
ErrorHandled::Reported =>
440-
InterpError::InvalidProgram(
441-
InvalidProgramInfo::ReferencedConstant
442-
),
439+
err_inval!(ReferencedConstant),
443440
ErrorHandled::TooGeneric =>
444-
InterpError::InvalidProgram(InvalidProgramInfo::TooGeneric),
441+
err_inval!(TooGeneric),
445442
}
446443
})?;
447444
// Make sure we use the ID of the resolved memory, not the lazy one!

src/librustc_mir/interpret/operand.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use rustc::ty::layout::{
1111
use rustc::mir::interpret::{
1212
GlobalId, AllocId,
1313
ConstValue, Pointer, Scalar,
14-
InterpResult, InterpError,
15-
sign_extend, truncate, UnsupportedOpInfo,
14+
InterpResult, sign_extend, truncate,
1615
};
1716
use super::{
1817
InterpCx, Machine,
@@ -332,7 +331,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
332331
let len = mplace.len(self)?;
333332
let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len as u64))?;
334333
let str = ::std::str::from_utf8(bytes).map_err(|err| {
335-
InterpError::Unsupported(UnsupportedOpInfo::ValidationFailure(err.to_string()))
334+
err_unsup!(ValidationFailure(err.to_string()))
336335
})?;
337336
Ok(str)
338337
}
@@ -604,7 +603,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
604603
let raw_discr = discr_val.to_scalar_or_undef();
605604
trace!("discr value: {:?}", raw_discr);
606605
// post-process
607-
use rustc::mir::interpret::UnsupportedOpInfo::InvalidDiscriminant;
608606
Ok(match *discr_kind {
609607
layout::DiscriminantKind::Tag => {
610608
let bits_discr = match raw_discr.to_bits(discr_val.layout.size) {
@@ -649,7 +647,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
649647
let variants_start = niche_variants.start().as_u32() as u128;
650648
let variants_end = niche_variants.end().as_u32() as u128;
651649
let raw_discr = raw_discr.not_undef().map_err(|_| {
652-
InterpError::Unsupported(InvalidDiscriminant(ScalarMaybeUndef::Undef))
650+
err_unsup!(InvalidDiscriminant(ScalarMaybeUndef::Undef))
653651
})?;
654652
match raw_discr.to_bits_or_ptr(discr_val.layout.size, self) {
655653
Err(ptr) => {

src/librustc_mir/interpret/snapshot.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use rustc::ich::StableHashingContextProvider;
1111
use rustc::mir;
1212
use rustc::mir::interpret::{
1313
AllocId, Pointer, Scalar,
14-
Relocations, Allocation, UndefMask,
15-
InterpResult, InterpError, ResourceExhaustionInfo,
14+
Relocations, Allocation, UndefMask, InterpResult,
1615
};
1716

1817
use rustc::ty::{self, TyCtxt};
@@ -77,7 +76,7 @@ impl<'mir, 'tcx> InfiniteLoopDetector<'mir, 'tcx> {
7776
}
7877

7978
// Second cycle
80-
Err(InterpError::ResourceExhaustion(ResourceExhaustionInfo::InfiniteLoop).into())
79+
throw_exhaust!(InfiniteLoop)
8180
}
8281
}
8382

src/librustc_mir/interpret/terminator.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,29 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
136136
} else {
137137
// Compute error message
138138
use rustc::mir::interpret::PanicInfo::*;
139-
match msg {
139+
return Err(match msg {
140140
BoundsCheck { ref len, ref index } => {
141-
let len = self.read_immediate(self.eval_operand(len, None)?)
142-
.expect("can't eval len").to_scalar()?
141+
let len = self
142+
.read_immediate(self.eval_operand(len, None)?)
143+
.expect("can't eval len")
144+
.to_scalar()?
143145
.to_bits(self.memory().pointer_size())? as u64;
144-
let index = self.read_immediate(self.eval_operand(index, None)?)
145-
.expect("can't eval index").to_scalar()?
146+
let index = self
147+
.read_immediate(self.eval_operand(index, None)?)
148+
.expect("can't eval index")
149+
.to_scalar()?
146150
.to_bits(self.memory().pointer_size())? as u64;
147-
throw_panic!(BoundsCheck { len, index })
151+
err_panic!(BoundsCheck { len, index })
148152
}
149-
Overflow(op) =>
150-
throw_panic!(Overflow(*op)),
151-
OverflowNeg =>
152-
throw_panic!(OverflowNeg),
153-
DivisionByZero =>
154-
throw_panic!(DivisionByZero),
155-
RemainderByZero =>
156-
throw_panic!(RemainderByZero),
157-
GeneratorResumedAfterReturn =>
158-
throw_panic!(GeneratorResumedAfterReturn),
159-
GeneratorResumedAfterPanic =>
160-
throw_panic!(GeneratorResumedAfterPanic),
161-
Panic { .. } =>
162-
bug!("`Panic` variant cannot occur in MIR"),
163-
};
153+
Overflow(op) => err_panic!(Overflow(*op)),
154+
OverflowNeg => err_panic!(OverflowNeg),
155+
DivisionByZero => err_panic!(DivisionByZero),
156+
RemainderByZero => err_panic!(RemainderByZero),
157+
GeneratorResumedAfterReturn => err_panic!(GeneratorResumedAfterReturn),
158+
GeneratorResumedAfterPanic => err_panic!(GeneratorResumedAfterPanic),
159+
Panic { .. } => bug!("`Panic` variant cannot occur in MIR"),
160+
}
161+
.into());
164162
}
165163
}
166164

0 commit comments

Comments
 (0)