Auto merge of #82338 - RalfJung:interp-error-allocs, r=oli-obk

all InterpError allocate now, so adjust alloc-error-check

Cc https://github.com/rust-lang/rust/pull/82116#discussion_r578310770
r? `@oli-obk`
This commit is contained in:
bors 2021-02-25 08:27:09 +00:00
commit 89d32eb1ea
4 changed files with 12 additions and 25 deletions

View File

@ -543,12 +543,12 @@ fn is_init(&self, ptr: Pointer<Tag>, size: Size) -> Result<(), Range<Size>> {
/// error which will report the first range of bytes which is uninitialized.
fn check_init(&self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
self.is_init(ptr, size).or_else(|idx_range| {
throw_ub!(InvalidUninitBytes(Some(Box::new(UninitBytesAccess {
throw_ub!(InvalidUninitBytes(Some(UninitBytesAccess {
access_ptr: ptr.erase_tag(),
access_size: size,
uninit_ptr: Pointer::new(ptr.alloc_id, idx_range.start),
uninit_size: idx_range.end - idx_range.start, // `Size` subtraction
}))))
})))
})
}

View File

@ -9,7 +9,7 @@
use rustc_session::CtfeBacktrace;
use rustc_span::def_id::DefId;
use rustc_target::abi::{Align, Size};
use std::{any::Any, backtrace::Backtrace, fmt, mem};
use std::{any::Any, backtrace::Backtrace, fmt};
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
pub enum ErrorHandled {
@ -263,7 +263,7 @@ pub enum UndefinedBehaviorInfo<'tcx> {
/// Using a string that is not valid UTF-8,
InvalidStr(std::str::Utf8Error),
/// Using uninitialized data where it is not allowed.
InvalidUninitBytes(Option<Box<UninitBytesAccess>>),
InvalidUninitBytes(Option<UninitBytesAccess>),
/// Working with a local that is not currently live.
DeadLocal,
/// Data size is not equal to target size.
@ -445,7 +445,7 @@ pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
}
#[cfg(target_arch = "x86_64")]
static_assert_size!(InterpError<'_>, 40);
static_assert_size!(InterpError<'_>, 72);
pub enum InterpError<'tcx> {
/// The program caused undefined behavior.
@ -486,19 +486,14 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
impl InterpError<'_> {
/// Some errors allocate to be created as they contain free-form strings.
/// And sometimes we want to be sure that did not happen as it is a
/// waste of resources.
pub fn allocates(&self) -> bool {
/// Some errors to string formatting even if the error is never printed.
/// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,
/// so this method lets us detect them and `bug!` on unexpected errors.
pub fn formatted_string(&self) -> bool {
match self {
// Zero-sized boxes do not allocate.
InterpError::MachineStop(b) => mem::size_of_val::<dyn MachineStopType>(&**b) > 0,
InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_))
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationFailure(_))
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some(_))) => {
true
}
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_)) => true,
_ => false,
}
}

View File

@ -352,14 +352,6 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx, const_eval
error
),
);
// Some errors shouldn't come up because creating them causes
// an allocation, which we should avoid. When that happens,
// dedicated error variants should be introduced instead.
assert!(
!error.kind().allocates(),
"interning encountered allocating error: {}",
error
);
}
}
}

View File

@ -466,8 +466,8 @@ fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
// an allocation, which we should avoid. When that happens,
// dedicated error variants should be introduced instead.
assert!(
!error.kind().allocates(),
"const-prop encountered allocating error: {}",
!error.kind().formatted_string(),
"const-prop encountered formatting error: {}",
error
);
None