diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 5ebe38b2d7e..3da63740350 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -550,12 +550,12 @@ impl<'tcx, Tag: Copy, Extra> Allocation { /// error which will report the first range of bytes which is uninitialized. fn check_init(&self, ptr: Pointer, 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 - })))) + }))) }) } diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 26ce3c2c3db..1589ab28e40 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -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 { @@ -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>), + InvalidUninitBytes(Option), /// Working with a local that is not currently live. DeadLocal, /// Data size is not equal to target size. @@ -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. @@ -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::(&**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, } } diff --git a/compiler/rustc_mir/src/interpret/intern.rs b/compiler/rustc_mir/src/interpret/intern.rs index 59438661cac..95464da145c 100644 --- a/compiler/rustc_mir/src/interpret/intern.rs +++ b/compiler/rustc_mir/src/interpret/intern.rs @@ -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 - ); } } } diff --git a/compiler/rustc_mir/src/transform/const_prop.rs b/compiler/rustc_mir/src/transform/const_prop.rs index 5384dbcb8a6..3b27b544310 100644 --- a/compiler/rustc_mir/src/transform/const_prop.rs +++ b/compiler/rustc_mir/src/transform/const_prop.rs @@ -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