Skip to content

Commit 0468929

Browse files
committed
move error allocation test to error.rs
1 parent 564758c commit 0468929

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

src/librustc/mir/interpret/error.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ pub enum UnsupportedOpInfo<'tcx> {
354354
Unsupported(String),
355355

356356
/// When const-prop encounters a situation it does not support, it raises this error.
357-
/// This must not allocate for performance reasons.
357+
/// This must not allocate for performance reasons (hence `str`, not `String`).
358358
ConstPropUnsupported(&'tcx str),
359359

360360
// -- Everything below is not categorized yet --
@@ -612,3 +612,19 @@ impl fmt::Debug for InterpError<'_> {
612612
}
613613
}
614614
}
615+
616+
impl InterpError<'_> {
617+
/// Some errors allocate to be created as they contain free-from strings.
618+
/// And sometiems we want to be sure that did not happen as it is a
619+
/// waste of resources.
620+
pub fn allocates(&self) -> bool {
621+
match self {
622+
InterpError::MachineStop(_)
623+
| InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_))
624+
| InterpError::Unsupported(UnsupportedOpInfo::ValidationFailure(_))
625+
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
626+
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::UbExperimental(_)) => true,
627+
_ => false,
628+
}
629+
}
630+
}

src/librustc_mir/transform/const_prop.rs

+9-26
Original file line numberDiff line numberDiff line change
@@ -404,32 +404,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
404404
let r = match f(self) {
405405
Ok(val) => Some(val),
406406
Err(error) => {
407-
use rustc::mir::interpret::{
408-
InterpError::*, UndefinedBehaviorInfo, UnsupportedOpInfo,
409-
};
410-
match error.kind {
411-
MachineStop(_) => bug!("ConstProp does not stop"),
412-
413-
// Some error shouldn't come up because creating them causes
414-
// an allocation, which we should avoid. When that happens,
415-
// dedicated error variants should be introduced instead.
416-
// Only test this in debug builds though to avoid disruptions.
417-
Unsupported(UnsupportedOpInfo::Unsupported(_))
418-
| Unsupported(UnsupportedOpInfo::ValidationFailure(_))
419-
| UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
420-
| UndefinedBehavior(UndefinedBehaviorInfo::UbExperimental(_))
421-
if cfg!(debug_assertions) =>
422-
{
423-
bug!("const-prop encountered allocating error: {:?}", error.kind);
424-
}
425-
426-
Unsupported(_)
427-
| UndefinedBehavior(_)
428-
| InvalidProgram(_)
429-
| ResourceExhaustion(_) => {
430-
// Ignore these errors.
431-
}
432-
}
407+
// Some errors shouldn't come up because creating them causes
408+
// an allocation, which we should avoid. When that happens,
409+
// dedicated error variants should be introduced instead.
410+
// Only test this in debug builds though to avoid disruptions.
411+
debug_assert!(
412+
!error.kind.allocates(),
413+
"const-prop encountered allocating error: {}",
414+
error
415+
);
433416
None
434417
}
435418
};

0 commit comments

Comments
 (0)