Skip to content

Commit 89d32eb

Browse files
committed
Auto merge of rust-lang#82338 - RalfJung:interp-error-allocs, r=oli-obk
all InterpError allocate now, so adjust alloc-error-check Cc rust-lang#82116 (comment) r? `@oli-obk`
2 parents 63bacf1 + d496bfc commit 89d32eb

File tree

4 files changed

+12
-25
lines changed

4 files changed

+12
-25
lines changed

compiler/rustc_middle/src/mir/interpret/allocation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,12 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
543543
/// error which will report the first range of bytes which is uninitialized.
544544
fn check_init(&self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
545545
self.is_init(ptr, size).or_else(|idx_range| {
546-
throw_ub!(InvalidUninitBytes(Some(Box::new(UninitBytesAccess {
546+
throw_ub!(InvalidUninitBytes(Some(UninitBytesAccess {
547547
access_ptr: ptr.erase_tag(),
548548
access_size: size,
549549
uninit_ptr: Pointer::new(ptr.alloc_id, idx_range.start),
550550
uninit_size: idx_range.end - idx_range.start, // `Size` subtraction
551-
}))))
551+
})))
552552
})
553553
}
554554

compiler/rustc_middle/src/mir/interpret/error.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_macros::HashStable;
99
use rustc_session::CtfeBacktrace;
1010
use rustc_span::def_id::DefId;
1111
use rustc_target::abi::{Align, Size};
12-
use std::{any::Any, backtrace::Backtrace, fmt, mem};
12+
use std::{any::Any, backtrace::Backtrace, fmt};
1313

1414
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
1515
pub enum ErrorHandled {
@@ -263,7 +263,7 @@ pub enum UndefinedBehaviorInfo<'tcx> {
263263
/// Using a string that is not valid UTF-8,
264264
InvalidStr(std::str::Utf8Error),
265265
/// Using uninitialized data where it is not allowed.
266-
InvalidUninitBytes(Option<Box<UninitBytesAccess>>),
266+
InvalidUninitBytes(Option<UninitBytesAccess>),
267267
/// Working with a local that is not currently live.
268268
DeadLocal,
269269
/// Data size is not equal to target size.
@@ -445,7 +445,7 @@ impl dyn MachineStopType {
445445
}
446446

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

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

488488
impl InterpError<'_> {
489-
/// Some errors allocate to be created as they contain free-form strings.
490-
/// And sometimes we want to be sure that did not happen as it is a
491-
/// waste of resources.
492-
pub fn allocates(&self) -> bool {
489+
/// Some errors to string formatting even if the error is never printed.
490+
/// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,
491+
/// so this method lets us detect them and `bug!` on unexpected errors.
492+
pub fn formatted_string(&self) -> bool {
493493
match self {
494-
// Zero-sized boxes do not allocate.
495-
InterpError::MachineStop(b) => mem::size_of_val::<dyn MachineStopType>(&**b) > 0,
496494
InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_))
497495
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationFailure(_))
498-
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
499-
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some(_))) => {
500-
true
501-
}
496+
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_)) => true,
502497
_ => false,
503498
}
504499
}

compiler/rustc_mir/src/interpret/intern.rs

-8
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,6 @@ where
352352
error
353353
),
354354
);
355-
// Some errors shouldn't come up because creating them causes
356-
// an allocation, which we should avoid. When that happens,
357-
// dedicated error variants should be introduced instead.
358-
assert!(
359-
!error.kind().allocates(),
360-
"interning encountered allocating error: {}",
361-
error
362-
);
363355
}
364356
}
365357
}

compiler/rustc_mir/src/transform/const_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
466466
// an allocation, which we should avoid. When that happens,
467467
// dedicated error variants should be introduced instead.
468468
assert!(
469-
!error.kind().allocates(),
470-
"const-prop encountered allocating error: {}",
469+
!error.kind().formatted_string(),
470+
"const-prop encountered formatting error: {}",
471471
error
472472
);
473473
None

0 commit comments

Comments
 (0)