Skip to content

Commit

Permalink
Auto merge of #82338 - RalfJung:interp-error-allocs, r=oli-obk
Browse files Browse the repository at this point in the history
all InterpError allocate now, so adjust alloc-error-check

Cc #82116 (comment)
r? `@oli-obk`
  • Loading branch information
bors committed Feb 25, 2021
2 parents 63bacf1 + d496bfc commit 89d32eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/mir/interpret/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,12 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
/// 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
}))))
})))
})
}

Expand Down
21 changes: 8 additions & 13 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_macros::HashStable;
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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -445,7 +445,7 @@ impl dyn MachineStopType {
}

#[cfg(target_arch = "x86_64")]
static_assert_size!(InterpError<'_>, 40);
static_assert_size!(InterpError<'_>, 72);

pub enum InterpError<'tcx> {
/// The program caused undefined behavior.
Expand Down Expand Up @@ -486,19 +486,14 @@ impl fmt::Debug for InterpError<'_> {
}

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,
}
}
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_mir/src/interpret/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,6 @@ where
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
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir/src/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
// 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
Expand Down

0 comments on commit 89d32eb

Please sign in to comment.