Skip to content

Commit 42dfdc5

Browse files
authored
Rollup merge of rust-lang#63229 - RalfJung:miri-error, r=oli-obk
A bit of Miri error cleanup Some cleanup after rust-lang#62969. r? @oli-obk Cc @saleemjaffer
2 parents 2fd9548 + e5fc957 commit 42dfdc5

File tree

11 files changed

+90
-98
lines changed

11 files changed

+90
-98
lines changed

src/librustc/mir/interpret/error.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,16 @@ impl fmt::Debug for InvalidProgramInfo<'tcx> {
341341
}
342342

343343
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
344-
pub enum UndefinedBehaviourInfo {
344+
pub enum UndefinedBehaviorInfo {
345345
/// Handle cases which for which we do not have a fixed variant.
346346
Ub(String),
347347
/// Unreachable code was executed.
348348
Unreachable,
349349
}
350350

351-
impl fmt::Debug for UndefinedBehaviourInfo {
351+
impl fmt::Debug for UndefinedBehaviorInfo {
352352
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
353-
use UndefinedBehaviourInfo::*;
353+
use UndefinedBehaviorInfo::*;
354354
match self {
355355
Ub(ref msg) =>
356356
write!(f, "{}", msg),
@@ -363,7 +363,7 @@ impl fmt::Debug for UndefinedBehaviourInfo {
363363
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
364364
pub enum UnsupportedOpInfo<'tcx> {
365365
/// Handle cases which for which we do not have a fixed variant.
366-
Unimplemented(String),
366+
Unsupported(String),
367367

368368
// -- Everything below is not classified yet --
369369
FunctionAbiMismatch(Abi, Abi),
@@ -390,20 +390,14 @@ pub enum UnsupportedOpInfo<'tcx> {
390390
ReadUndefBytes(Size),
391391
DeadLocal,
392392
InvalidBoolOp(mir::BinOp),
393-
InlineAsm,
394393
UnimplementedTraitSelection,
395394
CalledClosureAsFunction,
396395
NoMirFor(String),
397-
/// This variant is used by machines to signal their own errors that do not
398-
/// match an existing variant.
399-
MachineError(String),
400396
DerefFunctionPointer,
401397
ExecuteMemory,
402-
Intrinsic(String),
403398
InvalidChar(u128),
404399
OutOfTls,
405400
TlsOutOfBounds,
406-
AbiViolation(String),
407401
AlignmentCheckFailed {
408402
required: Align,
409403
has: Align,
@@ -513,8 +507,6 @@ impl fmt::Debug for UnsupportedOpInfo<'tcx> {
513507
initializer"),
514508
AssumptionNotHeld =>
515509
write!(f, "`assume` argument was false"),
516-
InlineAsm =>
517-
write!(f, "miri does not support inline assembly"),
518510
ReallocateNonBasePtr =>
519511
write!(f, "tried to reallocate with a pointer not to the beginning of an \
520512
existing object"),
@@ -537,10 +529,7 @@ impl fmt::Debug for UnsupportedOpInfo<'tcx> {
537529
HeapAllocNonPowerOfTwoAlignment(_) =>
538530
write!(f, "tried to re-, de-, or allocate heap memory with alignment that is \
539531
not a power of two"),
540-
MachineError(ref msg) |
541-
Unimplemented(ref msg) |
542-
AbiViolation(ref msg) |
543-
Intrinsic(ref msg) =>
532+
Unsupported(ref msg) =>
544533
write!(f, "{}", msg),
545534
}
546535
}
@@ -572,7 +561,7 @@ pub enum InterpError<'tcx> {
572561
/// The program panicked.
573562
Panic(PanicInfo<u64>),
574563
/// The program caused undefined behavior.
575-
UndefinedBehaviour(UndefinedBehaviourInfo),
564+
UndefinedBehavior(UndefinedBehaviorInfo),
576565
/// The program did something the interpreter does not support (some of these *might* be UB
577566
/// but the interpreter is not sure).
578567
Unsupported(UnsupportedOpInfo<'tcx>),
@@ -603,7 +592,7 @@ impl fmt::Debug for InterpError<'_> {
603592
write!(f, "{:?}", msg),
604593
InvalidProgram(ref msg) =>
605594
write!(f, "{:?}", msg),
606-
UndefinedBehaviour(ref msg) =>
595+
UndefinedBehavior(ref msg) =>
607596
write!(f, "{:?}", msg),
608597
ResourceExhaustion(ref msg) =>
609598
write!(f, "{:?}", msg),

src/librustc/mir/interpret/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ macro_rules! err_inval {
2121
#[macro_export]
2222
macro_rules! err_ub {
2323
($($tt:tt)*) => {
24-
$crate::mir::interpret::InterpError::UndefinedBehaviour(
25-
$crate::mir::interpret::UndefinedBehaviourInfo::$($tt)*
24+
$crate::mir::interpret::InterpError::UndefinedBehavior(
25+
$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
2626
)
2727
};
2828
}
@@ -50,6 +50,11 @@ macro_rules! throw_unsup {
5050
($($tt:tt)*) => { return Err(err_unsup!($($tt)*).into()) };
5151
}
5252

53+
#[macro_export]
54+
macro_rules! throw_unsup_format {
55+
($($tt:tt)*) => { throw_unsup!(Unsupported(format!($($tt)*))) };
56+
}
57+
5358
#[macro_export]
5459
macro_rules! throw_inval {
5560
($($tt:tt)*) => { return Err(err_inval!($($tt)*).into()) };
@@ -60,6 +65,11 @@ macro_rules! throw_ub {
6065
($($tt:tt)*) => { return Err(err_ub!($($tt)*).into()) };
6166
}
6267

68+
#[macro_export]
69+
macro_rules! throw_ub_format {
70+
($($tt:tt)*) => { throw_ub!(Ub(format!($($tt)*))) };
71+
}
72+
6373
#[macro_export]
6474
macro_rules! throw_panic {
6575
($($tt:tt)*) => { return Err(err_panic!($($tt)*).into()) };
@@ -78,7 +88,7 @@ mod pointer;
7888
pub use self::error::{
7989
InterpErrorInfo, InterpResult, InterpError, AssertMessage, ConstEvalErr, struct_error,
8090
FrameInfo, ConstEvalRawResult, ConstEvalResult, ErrorHandled, PanicInfo, UnsupportedOpInfo,
81-
InvalidProgramInfo, ResourceExhaustionInfo, UndefinedBehaviourInfo,
91+
InvalidProgramInfo, ResourceExhaustionInfo, UndefinedBehaviorInfo,
8292
};
8393

8494
pub use self::value::{Scalar, ScalarMaybeUndef, RawConst, ConstValue};

src/librustc_mir/const_eval.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,17 @@ fn eval_body_using_ecx<'mir, 'tcx>(
181181
Ok(ret)
182182
}
183183

184-
impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalError {
185-
fn into(self) -> InterpErrorInfo<'tcx> {
186-
err_unsup!(MachineError(self.to_string())).into()
187-
}
188-
}
189-
190184
#[derive(Clone, Debug)]
191185
enum ConstEvalError {
192186
NeedsRfc(String),
193187
}
194188

189+
impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalError {
190+
fn into(self) -> InterpErrorInfo<'tcx> {
191+
err_unsup!(Unsupported(self.to_string())).into()
192+
}
193+
}
194+
195195
impl fmt::Display for ConstEvalError {
196196
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197197
use self::ConstEvalError::*;
@@ -341,7 +341,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
341341
debug!("eval_fn_call: {:?}", instance);
342342
// Only check non-glue functions
343343
if let ty::InstanceDef::Item(def_id) = instance.def {
344-
// Execution might have wandered off into other crates, so we cannot to a stability-
344+
// Execution might have wandered off into other crates, so we cannot do a stability-
345345
// sensitive check here. But we can at least rule out functions that are not const
346346
// at all.
347347
if !ecx.tcx.is_const_fn_raw(def_id) {
@@ -352,7 +352,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
352352
ecx.goto_block(ret)?; // fully evaluated and done
353353
Ok(None)
354354
} else {
355-
throw_unsup!(MachineError(format!("calling non-const function `{}`", instance)))
355+
throw_unsup_format!("calling non-const function `{}`", instance)
356356
};
357357
}
358358
}

src/librustc_mir/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
199199
},
200200

201201
// Casts to bool are not permitted by rustc, no need to handle them here.
202-
_ => throw_unsup!(Unimplemented(format!("int to {:?} cast", dest_layout.ty))),
202+
_ => bug!("invalid int to {:?} cast", dest_layout.ty),
203203
}
204204
}
205205

src/librustc_mir/interpret/intrinsics.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
9898
let bits = self.read_scalar(args[0])?.to_bits(layout_of.size)?;
9999
let kind = match layout_of.abi {
100100
ty::layout::Abi::Scalar(ref scalar) => scalar.value,
101-
_ => Err(err_unsup!(TypeNotPrimitive(ty)))?,
101+
_ => throw_unsup!(TypeNotPrimitive(ty)),
102102
};
103103
let out_val = if intrinsic_name.ends_with("_nonzero") {
104104
if bits == 0 {
105-
throw_unsup!(Intrinsic(format!("{} called on 0", intrinsic_name)))
105+
throw_ub_format!("`{}` called on 0", intrinsic_name);
106106
}
107107
numeric_intrinsic(intrinsic_name.trim_end_matches("_nonzero"), bits, kind)?
108108
} else {
@@ -187,10 +187,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
187187
let (val, overflowed) = self.binary_op(bin_op, l, r)?;
188188
if overflowed {
189189
let layout = self.layout_of(substs.type_at(0))?;
190-
let r_val = r.to_scalar()?.to_bits(layout.size)?;
191-
throw_unsup!(
192-
Intrinsic(format!("Overflowing shift by {} in {}", r_val, intrinsic_name))
193-
)
190+
let r_val = r.to_scalar()?.to_bits(layout.size)?;
191+
throw_ub_format!("Overflowing shift by {} in `{}`", r_val, intrinsic_name);
194192
}
195193
self.write_scalar(val, dest)?;
196194
}

src/librustc_mir/interpret/memory.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ impl<'tcx, Other> FnVal<'tcx, Other> {
6666
match self {
6767
FnVal::Instance(instance) =>
6868
Ok(instance),
69-
FnVal::Other(_) => throw_unsup!(MachineError(format!(
70-
"Expected instance function pointer, got 'other' pointer"
71-
))),
69+
FnVal::Other(_) => throw_unsup_format!(
70+
"'foreign' function pointers are not supported in this context"
71+
),
7272
}
7373
}
7474
}
@@ -834,9 +834,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
834834
if (src.offset <= dest.offset && src.offset + size > dest.offset) ||
835835
(dest.offset <= src.offset && dest.offset + size > src.offset)
836836
{
837-
throw_unsup!(Intrinsic(
838-
"copy_nonoverlapping called on overlapping ranges".to_string(),
839-
))
837+
throw_ub_format!(
838+
"copy_nonoverlapping called on overlapping ranges"
839+
)
840840
}
841841
}
842842

src/librustc_mir/interpret/operator.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
147147

148148
// For the remaining ops, the types must be the same on both sides
149149
if left_layout.ty != right_layout.ty {
150-
let msg = format!(
151-
"unimplemented asymmetric binary op {:?}: {:?} ({:?}), {:?} ({:?})",
150+
bug!(
151+
"invalid asymmetric binary op {:?}: {:?} ({:?}), {:?} ({:?})",
152152
bin_op,
153-
l,
154-
left_layout.ty,
155-
r,
156-
right_layout.ty
157-
);
158-
throw_unsup!(Unimplemented(msg))
153+
l, left_layout.ty,
154+
r, right_layout.ty,
155+
)
159156
}
160157

161158
// Operations that need special treatment for signed integers
@@ -243,14 +240,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
243240
}
244241

245242
_ => {
246-
let msg = format!(
247-
"unimplemented binary op {:?}: {:?}, {:?} (both {:?})",
243+
bug!(
244+
"invalid binary op {:?}: {:?}, {:?} (both {:?})",
248245
bin_op,
249246
l,
250247
r,
251248
right_layout.ty,
252-
);
253-
throw_unsup!(Unimplemented(msg))
249+
)
254250
}
255251
};
256252

src/librustc_mir/interpret/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
121121
// size of MIR constantly.
122122
Nop => {}
123123

124-
InlineAsm { .. } => throw_unsup!(InlineAsm),
124+
InlineAsm { .. } => throw_unsup_format!("inline assembly is not supported"),
125125
}
126126

127127
self.stack[frame_idx].stmt += 1;

src/librustc_mir/interpret/terminator.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8888
(FnVal::Instance(self.resolve(def_id, substs)?), sig.abi())
8989
},
9090
_ => {
91-
let msg = format!("can't handle callee of type {:?}", func.layout.ty);
92-
throw_unsup!(Unimplemented(msg))
91+
bug!("invalid callee of type {:?}", func.layout.ty)
9392
}
9493
};
9594
let args = self.eval_operands(args)?;

src/librustc_mir/transform/const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
260260
match diagnostic.error {
261261
Exit(_) => bug!("the CTFE program cannot exit"),
262262
Unsupported(_)
263-
| UndefinedBehaviour(_)
263+
| UndefinedBehavior(_)
264264
| InvalidProgram(_)
265265
| ResourceExhaustion(_) => {
266266
// Ignore these errors.

0 commit comments

Comments
 (0)