Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6999720

Browse files
authoredOct 19, 2024
Rollup merge of #131916 - RalfJung:interpret-err, r=jieyouxu
small interpreter error cleanup - Add `InterpretResult::map_err_kind` for the common case of swapping out the error kind (while preserving the backtrace pointing to the original error source) - Rename `InterpError` -> `InterpErrorKind` to be consistent with the `kind` field name, and make it more clear that this is not the final error type
2 parents 0ca10bb + eea74be commit 6999720

File tree

17 files changed

+108
-95
lines changed

17 files changed

+108
-95
lines changed
 

‎compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use rustc_span::{Span, Symbol};
1111
use super::CompileTimeMachine;
1212
use crate::errors::{self, FrameNote, ReportErrorExt};
1313
use crate::interpret::{
14-
ErrorHandled, Frame, InterpError, InterpErrorInfo, MachineStopType, err_inval, err_machine_stop,
14+
ErrorHandled, Frame, InterpErrorInfo, InterpErrorKind, MachineStopType, err_inval,
15+
err_machine_stop,
1516
};
1617

1718
/// The CTFE machine has some custom error kinds.
@@ -57,7 +58,7 @@ impl MachineStopType for ConstEvalErrKind {
5758
}
5859
}
5960

60-
/// The errors become [`InterpError::MachineStop`] when being raised.
61+
/// The errors become [`InterpErrorKind::MachineStop`] when being raised.
6162
impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalErrKind {
6263
fn into(self) -> InterpErrorInfo<'tcx> {
6364
err_machine_stop!(self).into()
@@ -124,7 +125,7 @@ pub fn get_span_and_frames<'tcx>(
124125
/// `get_span_and_frames`.
125126
pub(super) fn report<'tcx, C, F, E>(
126127
tcx: TyCtxt<'tcx>,
127-
error: InterpError<'tcx>,
128+
error: InterpErrorKind<'tcx>,
128129
span: Span,
129130
get_span_and_frames: C,
130131
mk: F,

‎compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use tracing::{debug, instrument, trace};
1818
use super::{CanAccessMutGlobal, CompileTimeInterpCx, CompileTimeMachine};
1919
use crate::const_eval::CheckAlignment;
2020
use crate::interpret::{
21-
CtfeValidationMode, GlobalId, Immediate, InternKind, InternResult, InterpCx, InterpError,
21+
CtfeValidationMode, GlobalId, Immediate, InternKind, InternResult, InterpCx, InterpErrorKind,
2222
InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, StackPopCleanup, create_static_alloc,
2323
eval_nullary_intrinsic, intern_const_alloc_recursive, interp_ok, throw_exhaust,
2424
};
@@ -463,7 +463,7 @@ fn report_validation_error<'tcx>(
463463
error: InterpErrorInfo<'tcx>,
464464
alloc_id: AllocId,
465465
) -> ErrorHandled {
466-
if !matches!(error.kind(), InterpError::UndefinedBehavior(_)) {
466+
if !matches!(error.kind(), InterpErrorKind::UndefinedBehavior(_)) {
467467
// Some other error happened during validation, e.g. an unsupported operation.
468468
return report_eval_error(ecx, cid, error);
469469
}

‎compiler/rustc_const_eval/src/errors.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::{
99
use rustc_hir::ConstContext;
1010
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1111
use rustc_middle::mir::interpret::{
12-
CheckInAllocMsg, CtfeProvenance, ExpectedKind, InterpError, InvalidMetaKind,
12+
CheckInAllocMsg, CtfeProvenance, ExpectedKind, InterpErrorKind, InvalidMetaKind,
1313
InvalidProgramInfo, Misalignment, Pointer, PointerKind, ResourceExhaustionInfo,
1414
UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
1515
};
@@ -835,23 +835,23 @@ impl ReportErrorExt for UnsupportedOpInfo {
835835
}
836836
}
837837

838-
impl<'tcx> ReportErrorExt for InterpError<'tcx> {
838+
impl<'tcx> ReportErrorExt for InterpErrorKind<'tcx> {
839839
fn diagnostic_message(&self) -> DiagMessage {
840840
match self {
841-
InterpError::UndefinedBehavior(ub) => ub.diagnostic_message(),
842-
InterpError::Unsupported(e) => e.diagnostic_message(),
843-
InterpError::InvalidProgram(e) => e.diagnostic_message(),
844-
InterpError::ResourceExhaustion(e) => e.diagnostic_message(),
845-
InterpError::MachineStop(e) => e.diagnostic_message(),
841+
InterpErrorKind::UndefinedBehavior(ub) => ub.diagnostic_message(),
842+
InterpErrorKind::Unsupported(e) => e.diagnostic_message(),
843+
InterpErrorKind::InvalidProgram(e) => e.diagnostic_message(),
844+
InterpErrorKind::ResourceExhaustion(e) => e.diagnostic_message(),
845+
InterpErrorKind::MachineStop(e) => e.diagnostic_message(),
846846
}
847847
}
848848
fn add_args<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
849849
match self {
850-
InterpError::UndefinedBehavior(ub) => ub.add_args(diag),
851-
InterpError::Unsupported(e) => e.add_args(diag),
852-
InterpError::InvalidProgram(e) => e.add_args(diag),
853-
InterpError::ResourceExhaustion(e) => e.add_args(diag),
854-
InterpError::MachineStop(e) => e.add_args(&mut |name, value| {
850+
InterpErrorKind::UndefinedBehavior(ub) => ub.add_args(diag),
851+
InterpErrorKind::Unsupported(e) => e.add_args(diag),
852+
InterpErrorKind::InvalidProgram(e) => e.add_args(diag),
853+
InterpErrorKind::ResourceExhaustion(e) => e.add_args(diag),
854+
InterpErrorKind::MachineStop(e) => e.add_args(&mut |name, value| {
855855
diag.arg(name, value);
856856
}),
857857
}

‎compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
471471
// Don't forget to mark "initially live" locals as live.
472472
self.storage_live_for_always_live_locals()?;
473473
};
474-
res.inspect_err(|_| {
474+
res.inspect_err_kind(|_| {
475475
// Don't show the incomplete stack frame in the error stacktrace.
476476
self.stack_mut().pop();
477477
})

‎compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_trait_selection::traits::ObligationCtxt;
1919
use tracing::{debug, instrument, trace};
2020

2121
use super::{
22-
Frame, FrameInfo, GlobalId, InterpError, InterpErrorInfo, InterpResult, MPlaceTy, Machine,
22+
Frame, FrameInfo, GlobalId, InterpErrorInfo, InterpErrorKind, InterpResult, MPlaceTy, Machine,
2323
MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
2424
err_inval, interp_ok, throw_inval, throw_ub, throw_ub_custom,
2525
};
@@ -73,7 +73,7 @@ where
7373
}
7474

7575
impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
76-
type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpError<'tcx>>;
76+
type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>>;
7777

7878
#[inline]
7979
fn layout_tcx_at_span(&self) -> Span {
@@ -82,20 +82,25 @@ impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
8282
}
8383

8484
#[inline]
85-
fn handle_layout_err(&self, err: LayoutError<'tcx>, _: Span, _: Ty<'tcx>) -> InterpError<'tcx> {
85+
fn handle_layout_err(
86+
&self,
87+
err: LayoutError<'tcx>,
88+
_: Span,
89+
_: Ty<'tcx>,
90+
) -> InterpErrorKind<'tcx> {
8691
err_inval!(Layout(err))
8792
}
8893
}
8994

9095
impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
91-
type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpError<'tcx>>;
96+
type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
9297

9398
fn handle_fn_abi_err(
9499
&self,
95100
err: FnAbiError<'tcx>,
96101
_span: Span,
97102
_fn_abi_request: FnAbiRequest<'tcx>,
98-
) -> InterpError<'tcx> {
103+
) -> InterpErrorKind<'tcx> {
99104
match err {
100105
FnAbiError::Layout(err) => err_inval!(Layout(err)),
101106
FnAbiError::AdjustForForeignAbi(err) => {

‎compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
324324
dist.checked_neg().unwrap(), // i64::MIN is impossible as no allocation can be that large
325325
CheckInAllocMsg::OffsetFromTest,
326326
)
327-
.map_err(|_| {
327+
.map_err_kind(|_| {
328328
// Make the error more specific.
329329
err_ub_custom!(
330330
fluent::const_eval_offset_from_different_allocations,
331331
name = intrinsic_name,
332332
)
333-
.into()
334333
})?;
335334

336335
// Perform division by size to compute return value.

‎compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use rustc_hir as hir;
1717
use rustc_middle::bug;
1818
use rustc_middle::mir::interpret::ValidationErrorKind::{self, *};
1919
use rustc_middle::mir::interpret::{
20-
ExpectedKind, InterpError, InterpErrorInfo, InvalidMetaKind, Misalignment, PointerKind,
21-
Provenance, UnsupportedOpInfo, ValidationErrorInfo, alloc_range, interp_ok,
20+
ExpectedKind, InterpErrorKind, InvalidMetaKind, Misalignment, PointerKind, Provenance,
21+
UnsupportedOpInfo, ValidationErrorInfo, alloc_range, interp_ok,
2222
};
2323
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
2424
use rustc_middle::ty::{self, Ty};
@@ -37,8 +37,8 @@ use super::{
3737

3838
// for the validation errors
3939
#[rustfmt::skip]
40-
use super::InterpError::UndefinedBehavior as Ub;
41-
use super::InterpError::Unsupported as Unsup;
40+
use super::InterpErrorKind::UndefinedBehavior as Ub;
41+
use super::InterpErrorKind::Unsupported as Unsup;
4242
use super::UndefinedBehaviorInfo::*;
4343
use super::UnsupportedOpInfo::*;
4444

@@ -97,20 +97,19 @@ macro_rules! try_validation {
9797
($e:expr, $where:expr,
9898
$( $( $p:pat_param )|+ => $kind: expr ),+ $(,)?
9999
) => {{
100-
$e.map_err(|e| {
100+
$e.map_err_kind(|e| {
101101
// We catch the error and turn it into a validation failure. We are okay with
102102
// allocation here as this can only slow down builds that fail anyway.
103-
let (kind, backtrace) = e.into_parts();
104-
match kind {
103+
match e {
105104
$(
106105
$($p)|+ => {
107106
err_validation_failure!(
108107
$where,
109108
$kind
110-
).into()
109+
)
111110
}
112111
),+,
113-
_ => InterpErrorInfo::from_parts(kind, backtrace),
112+
e => e,
114113
}
115114
})?
116115
}};
@@ -1230,11 +1229,10 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12301229
// No need for an alignment check here, this is not an actual memory access.
12311230
let alloc = self.ecx.get_ptr_alloc(mplace.ptr(), size)?.expect("we already excluded size 0");
12321231

1233-
alloc.get_bytes_strip_provenance().map_err(|err| {
1232+
alloc.get_bytes_strip_provenance().map_err_kind(|kind| {
12341233
// Some error happened, try to provide a more detailed description.
12351234
// For some errors we might be able to provide extra information.
12361235
// (This custom logic does not fit the `try_validation!` macro.)
1237-
let (kind, backtrace) = err.into_parts();
12381236
match kind {
12391237
Ub(InvalidUninitBytes(Some((_alloc_id, access)))) | Unsup(ReadPointerAsInt(Some((_alloc_id, access)))) => {
12401238
// Some byte was uninitialized, determine which
@@ -1247,14 +1245,14 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12471245
self.path.push(PathElem::ArrayElem(i));
12481246

12491247
if matches!(kind, Ub(InvalidUninitBytes(_))) {
1250-
err_validation_failure!(self.path, Uninit { expected }).into()
1248+
err_validation_failure!(self.path, Uninit { expected })
12511249
} else {
1252-
err_validation_failure!(self.path, PointerAsInt { expected }).into()
1250+
err_validation_failure!(self.path, PointerAsInt { expected })
12531251
}
12541252
}
12551253

12561254
// Propagate upwards (that will also check for unexpected errors).
1257-
_ => return InterpErrorInfo::from_parts(kind, backtrace),
1255+
err => err,
12581256
}
12591257
})?;
12601258

@@ -1368,12 +1366,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
13681366
v.reset_padding(val)?;
13691367
interp_ok(())
13701368
})
1371-
.map_err(|err| {
1369+
.map_err_info(|err| {
13721370
if !matches!(
13731371
err.kind(),
13741372
err_ub!(ValidationError { .. })
1375-
| InterpError::InvalidProgram(_)
1376-
| InterpError::Unsupported(UnsupportedOpInfo::ExternTypeField)
1373+
| InterpErrorKind::InvalidProgram(_)
1374+
| InterpErrorKind::Unsupported(UnsupportedOpInfo::ExternTypeField)
13771375
) {
13781376
bug!(
13791377
"Unexpected error during validation: {}",

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
1818
use rustc_target::abi::{Align, HasDataLayout, Size};
1919

2020
use super::{
21-
AllocId, BadBytesAccess, CtfeProvenance, InterpError, InterpResult, Pointer, PointerArithmetic,
22-
Provenance, ResourceExhaustionInfo, Scalar, ScalarSizeMismatch, UndefinedBehaviorInfo,
23-
UnsupportedOpInfo, interp_ok, read_target_uint, write_target_uint,
21+
AllocId, BadBytesAccess, CtfeProvenance, InterpErrorKind, InterpResult, Pointer,
22+
PointerArithmetic, Provenance, ResourceExhaustionInfo, Scalar, ScalarSizeMismatch,
23+
UndefinedBehaviorInfo, UnsupportedOpInfo, interp_ok, read_target_uint, write_target_uint,
2424
};
2525
use crate::ty;
2626

@@ -199,22 +199,22 @@ impl From<ScalarSizeMismatch> for AllocError {
199199
}
200200

201201
impl AllocError {
202-
pub fn to_interp_error<'tcx>(self, alloc_id: AllocId) -> InterpError<'tcx> {
202+
pub fn to_interp_error<'tcx>(self, alloc_id: AllocId) -> InterpErrorKind<'tcx> {
203203
use AllocError::*;
204204
match self {
205205
ScalarSizeMismatch(s) => {
206-
InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ScalarSizeMismatch(s))
206+
InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::ScalarSizeMismatch(s))
207207
}
208-
ReadPointerAsInt(info) => InterpError::Unsupported(
208+
ReadPointerAsInt(info) => InterpErrorKind::Unsupported(
209209
UnsupportedOpInfo::ReadPointerAsInt(info.map(|b| (alloc_id, b))),
210210
),
211-
OverwritePartialPointer(offset) => InterpError::Unsupported(
211+
OverwritePartialPointer(offset) => InterpErrorKind::Unsupported(
212212
UnsupportedOpInfo::OverwritePartialPointer(Pointer::new(alloc_id, offset)),
213213
),
214-
ReadPartialPointer(offset) => InterpError::Unsupported(
214+
ReadPartialPointer(offset) => InterpErrorKind::Unsupported(
215215
UnsupportedOpInfo::ReadPartialPointer(Pointer::new(alloc_id, offset)),
216216
),
217-
InvalidUninitBytes(info) => InterpError::UndefinedBehavior(
217+
InvalidUninitBytes(info) => InterpErrorKind::UndefinedBehavior(
218218
UndefinedBehaviorInfo::InvalidUninitBytes(info.map(|b| (alloc_id, b))),
219219
),
220220
}
@@ -318,7 +318,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
318318
pub fn try_uninit<'tcx>(size: Size, align: Align) -> InterpResult<'tcx, Self> {
319319
Self::uninit_inner(size, align, || {
320320
ty::tls::with(|tcx| tcx.dcx().delayed_bug("exhausted memory during interpretation"));
321-
InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
321+
InterpErrorKind::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
322322
})
323323
.into()
324324
}

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

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub struct InterpErrorInfo<'tcx>(Box<InterpErrorInfoInner<'tcx>>);
113113

114114
#[derive(Debug)]
115115
struct InterpErrorInfoInner<'tcx> {
116-
kind: InterpError<'tcx>,
116+
kind: InterpErrorKind<'tcx>,
117117
backtrace: InterpErrorBacktrace,
118118
}
119119

@@ -154,21 +154,21 @@ impl InterpErrorBacktrace {
154154
}
155155

156156
impl<'tcx> InterpErrorInfo<'tcx> {
157-
pub fn into_parts(self) -> (InterpError<'tcx>, InterpErrorBacktrace) {
157+
pub fn into_parts(self) -> (InterpErrorKind<'tcx>, InterpErrorBacktrace) {
158158
let InterpErrorInfo(box InterpErrorInfoInner { kind, backtrace }) = self;
159159
(kind, backtrace)
160160
}
161161

162-
pub fn into_kind(self) -> InterpError<'tcx> {
162+
pub fn into_kind(self) -> InterpErrorKind<'tcx> {
163163
self.0.kind
164164
}
165165

166-
pub fn from_parts(kind: InterpError<'tcx>, backtrace: InterpErrorBacktrace) -> Self {
166+
pub fn from_parts(kind: InterpErrorKind<'tcx>, backtrace: InterpErrorBacktrace) -> Self {
167167
Self(Box::new(InterpErrorInfoInner { kind, backtrace }))
168168
}
169169

170170
#[inline]
171-
pub fn kind(&self) -> &InterpError<'tcx> {
171+
pub fn kind(&self) -> &InterpErrorKind<'tcx> {
172172
&self.0.kind
173173
}
174174
}
@@ -179,22 +179,22 @@ fn print_backtrace(backtrace: &Backtrace) {
179179

180180
impl From<ErrorGuaranteed> for InterpErrorInfo<'_> {
181181
fn from(err: ErrorGuaranteed) -> Self {
182-
InterpError::InvalidProgram(InvalidProgramInfo::AlreadyReported(err.into())).into()
182+
InterpErrorKind::InvalidProgram(InvalidProgramInfo::AlreadyReported(err.into())).into()
183183
}
184184
}
185185

186186
impl From<ErrorHandled> for InterpErrorInfo<'_> {
187187
fn from(err: ErrorHandled) -> Self {
188-
InterpError::InvalidProgram(match err {
188+
InterpErrorKind::InvalidProgram(match err {
189189
ErrorHandled::Reported(r, _span) => InvalidProgramInfo::AlreadyReported(r),
190190
ErrorHandled::TooGeneric(_span) => InvalidProgramInfo::TooGeneric,
191191
})
192192
.into()
193193
}
194194
}
195195

196-
impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
197-
fn from(kind: InterpError<'tcx>) -> Self {
196+
impl<'tcx> From<InterpErrorKind<'tcx>> for InterpErrorInfo<'tcx> {
197+
fn from(kind: InterpErrorKind<'tcx>) -> Self {
198198
InterpErrorInfo(Box::new(InterpErrorInfoInner {
199199
kind,
200200
backtrace: InterpErrorBacktrace::new(),
@@ -590,7 +590,7 @@ impl dyn MachineStopType {
590590
}
591591

592592
#[derive(Debug)]
593-
pub enum InterpError<'tcx> {
593+
pub enum InterpErrorKind<'tcx> {
594594
/// The program caused undefined behavior.
595595
UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
596596
/// The program did something the interpreter does not support (some of these *might* be UB
@@ -606,25 +606,25 @@ pub enum InterpError<'tcx> {
606606
MachineStop(Box<dyn MachineStopType>),
607607
}
608608

609-
impl InterpError<'_> {
609+
impl InterpErrorKind<'_> {
610610
/// Some errors do string formatting even if the error is never printed.
611611
/// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,
612612
/// so this method lets us detect them and `bug!` on unexpected errors.
613613
pub fn formatted_string(&self) -> bool {
614614
matches!(
615615
self,
616-
InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_))
617-
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationError { .. })
618-
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
616+
InterpErrorKind::Unsupported(UnsupportedOpInfo::Unsupported(_))
617+
| InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::ValidationError { .. })
618+
| InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
619619
)
620620
}
621621
}
622622

623-
// Macros for constructing / throwing `InterpError`
623+
// Macros for constructing / throwing `InterpErrorKind`
624624
#[macro_export]
625625
macro_rules! err_unsup {
626626
($($tt:tt)*) => {
627-
$crate::mir::interpret::InterpError::Unsupported(
627+
$crate::mir::interpret::InterpErrorKind::Unsupported(
628628
$crate::mir::interpret::UnsupportedOpInfo::$($tt)*
629629
)
630630
};
@@ -638,7 +638,7 @@ macro_rules! err_unsup_format {
638638
#[macro_export]
639639
macro_rules! err_inval {
640640
($($tt:tt)*) => {
641-
$crate::mir::interpret::InterpError::InvalidProgram(
641+
$crate::mir::interpret::InterpErrorKind::InvalidProgram(
642642
$crate::mir::interpret::InvalidProgramInfo::$($tt)*
643643
)
644644
};
@@ -647,7 +647,7 @@ macro_rules! err_inval {
647647
#[macro_export]
648648
macro_rules! err_ub {
649649
($($tt:tt)*) => {
650-
$crate::mir::interpret::InterpError::UndefinedBehavior(
650+
$crate::mir::interpret::InterpErrorKind::UndefinedBehavior(
651651
$crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
652652
)
653653
};
@@ -680,7 +680,7 @@ macro_rules! err_ub_custom {
680680
#[macro_export]
681681
macro_rules! err_exhaust {
682682
($($tt:tt)*) => {
683-
$crate::mir::interpret::InterpError::ResourceExhaustion(
683+
$crate::mir::interpret::InterpErrorKind::ResourceExhaustion(
684684
$crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
685685
)
686686
};
@@ -689,7 +689,7 @@ macro_rules! err_exhaust {
689689
#[macro_export]
690690
macro_rules! err_machine_stop {
691691
($($tt:tt)*) => {
692-
$crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*))
692+
$crate::mir::interpret::InterpErrorKind::MachineStop(Box::new($($tt)*))
693693
};
694694
}
695695

@@ -792,9 +792,9 @@ impl<'tcx, T> ops::FromResidual for InterpResult_<'tcx, T> {
792792
}
793793

794794
// Allow `yeet`ing `InterpError` in functions returning `InterpResult_`.
795-
impl<'tcx, T> ops::FromResidual<ops::Yeet<InterpError<'tcx>>> for InterpResult_<'tcx, T> {
795+
impl<'tcx, T> ops::FromResidual<ops::Yeet<InterpErrorKind<'tcx>>> for InterpResult_<'tcx, T> {
796796
#[inline]
797-
fn from_residual(ops::Yeet(e): ops::Yeet<InterpError<'tcx>>) -> Self {
797+
fn from_residual(ops::Yeet(e): ops::Yeet<InterpErrorKind<'tcx>>) -> Self {
798798
Self::new(Err(e.into()))
799799
}
800800
}
@@ -856,16 +856,27 @@ impl<'tcx, T> InterpResult_<'tcx, T> {
856856
}
857857

858858
#[inline]
859-
pub fn map_err(
859+
pub fn map_err_info(
860860
self,
861861
f: impl FnOnce(InterpErrorInfo<'tcx>) -> InterpErrorInfo<'tcx>,
862862
) -> InterpResult<'tcx, T> {
863863
InterpResult_::new(self.disarm().map_err(f))
864864
}
865865

866866
#[inline]
867-
pub fn inspect_err(self, f: impl FnOnce(&InterpErrorInfo<'tcx>)) -> InterpResult<'tcx, T> {
868-
InterpResult_::new(self.disarm().inspect_err(f))
867+
pub fn map_err_kind(
868+
self,
869+
f: impl FnOnce(InterpErrorKind<'tcx>) -> InterpErrorKind<'tcx>,
870+
) -> InterpResult<'tcx, T> {
871+
InterpResult_::new(self.disarm().map_err(|mut e| {
872+
e.0.kind = f(e.0.kind);
873+
e
874+
}))
875+
}
876+
877+
#[inline]
878+
pub fn inspect_err_kind(self, f: impl FnOnce(&InterpErrorKind<'tcx>)) -> InterpResult<'tcx, T> {
879+
InterpResult_::new(self.disarm().inspect_err(|e| f(&e.0.kind)))
869880
}
870881

871882
#[inline]

‎compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub use self::allocation::{
3636
pub use self::error::{
3737
BadBytesAccess, CheckAlignMsg, CheckInAllocMsg, ErrorHandled, EvalStaticInitializerRawResult,
3838
EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, ExpectedKind,
39-
InterpError, InterpErrorInfo, InterpResult, InvalidMetaKind, InvalidProgramInfo,
39+
InterpErrorInfo, InterpErrorKind, InterpResult, InvalidMetaKind, InvalidProgramInfo,
4040
MachineStopType, Misalignment, PointerKind, ReportedErrorInfo, ResourceExhaustionInfo,
4141
ScalarSizeMismatch, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
4242
ValidationErrorKind, interp_ok,

‎compiler/rustc_mir_transform/src/known_panics_lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
232232
F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
233233
{
234234
f(self)
235-
.map_err(|err| {
235+
.map_err_info(|err| {
236236
trace!("InterpCx operation failed: {:?}", err);
237237
// Some errors shouldn't come up because creating them causes
238238
// an allocation, which we should avoid. When that happens,

‎src/tools/miri/src/borrow_tracker/stacked_borrows/diagnostics.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn err_sb_ub<'tcx>(
1313
msg: String,
1414
help: Vec<String>,
1515
history: Option<TagHistory>,
16-
) -> InterpError<'tcx> {
16+
) -> InterpErrorKind<'tcx> {
1717
err_machine_stop!(TerminationInfo::StackedBorrowsUb { msg, help, history })
1818
}
1919

@@ -376,7 +376,7 @@ impl<'history, 'ecx, 'tcx> DiagnosticCx<'history, 'ecx, 'tcx> {
376376

377377
/// Report a descriptive error when `new` could not be granted from `derived_from`.
378378
#[inline(never)] // This is only called on fatal code paths
379-
pub(super) fn grant_error(&self, stack: &Stack) -> InterpError<'tcx> {
379+
pub(super) fn grant_error(&self, stack: &Stack) -> InterpErrorKind<'tcx> {
380380
let Operation::Retag(op) = &self.operation else {
381381
unreachable!("grant_error should only be called during a retag")
382382
};
@@ -402,7 +402,7 @@ impl<'history, 'ecx, 'tcx> DiagnosticCx<'history, 'ecx, 'tcx> {
402402

403403
/// Report a descriptive error when `access` is not permitted based on `tag`.
404404
#[inline(never)] // This is only called on fatal code paths
405-
pub(super) fn access_error(&self, stack: &Stack) -> InterpError<'tcx> {
405+
pub(super) fn access_error(&self, stack: &Stack) -> InterpErrorKind<'tcx> {
406406
// Deallocation and retagging also do an access as part of their thing, so handle that here, too.
407407
let op = match &self.operation {
408408
Operation::Access(op) => op,
@@ -424,7 +424,7 @@ impl<'history, 'ecx, 'tcx> DiagnosticCx<'history, 'ecx, 'tcx> {
424424
}
425425

426426
#[inline(never)] // This is only called on fatal code paths
427-
pub(super) fn protector_error(&self, item: &Item, kind: ProtectorKind) -> InterpError<'tcx> {
427+
pub(super) fn protector_error(&self, item: &Item, kind: ProtectorKind) -> InterpErrorKind<'tcx> {
428428
let protected = match kind {
429429
ProtectorKind::WeakProtector => "weakly protected",
430430
ProtectorKind::StrongProtector => "strongly protected",
@@ -445,7 +445,7 @@ impl<'history, 'ecx, 'tcx> DiagnosticCx<'history, 'ecx, 'tcx> {
445445
}
446446

447447
#[inline(never)] // This is only called on fatal code paths
448-
pub fn dealloc_error(&self, stack: &Stack) -> InterpError<'tcx> {
448+
pub fn dealloc_error(&self, stack: &Stack) -> InterpErrorKind<'tcx> {
449449
let Operation::Dealloc(op) = &self.operation else {
450450
unreachable!("dealloc_error should only be called during a deallocation")
451451
};

‎src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ pub(super) struct TbError<'node> {
298298

299299
impl TbError<'_> {
300300
/// Produce a UB error.
301-
pub fn build<'tcx>(self) -> InterpError<'tcx> {
301+
pub fn build<'tcx>(self) -> InterpErrorKind<'tcx> {
302302
use TransitionError::*;
303303
let cause = self.access_cause;
304304
let accessed = self.accessed_info;

‎src/tools/miri/src/borrow_tracker/tree_borrows/tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ impl<'tcx> Tree {
674674
Ok(())
675675
}
676676
},
677-
|args: ErrHandlerArgs<'_, TransitionError>| -> InterpError<'tcx> {
677+
|args: ErrHandlerArgs<'_, TransitionError>| -> InterpErrorKind<'tcx> {
678678
let ErrHandlerArgs { error_kind, conflicting_info, accessed_info } = args;
679679
TbError {
680680
conflicting_info,
@@ -772,7 +772,7 @@ impl<'tcx> Tree {
772772
let err_handler = |perms_range: Range<u64>,
773773
access_cause: diagnostics::AccessCause,
774774
args: ErrHandlerArgs<'_, TransitionError>|
775-
-> InterpError<'tcx> {
775+
-> InterpErrorKind<'tcx> {
776776
let ErrHandlerArgs { error_kind, conflicting_info, accessed_info } = args;
777777
TbError {
778778
conflicting_info,

‎src/tools/miri/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ pub fn report_error<'tcx>(
214214
ecx: &InterpCx<'tcx, MiriMachine<'tcx>>,
215215
e: InterpErrorInfo<'tcx>,
216216
) -> Option<(i64, bool)> {
217-
use InterpError::*;
217+
use InterpErrorKind::*;
218218
use UndefinedBehaviorInfo::*;
219219

220220
let mut msg = vec![];

‎src/tools/miri/src/intrinsics/simd.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
245245
let val = match which {
246246
Op::MirOp(mir_op) => {
247247
// This does NaN adjustments.
248-
let val = this.binary_op(mir_op, &left, &right).map_err(|err| {
249-
match err.kind() {
250-
&InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ShiftOverflow { shift_amount, .. }) => {
248+
let val = this.binary_op(mir_op, &left, &right).map_err_kind(|kind| {
249+
match kind {
250+
InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::ShiftOverflow { shift_amount, .. }) => {
251251
// This resets the interpreter backtrace, but it's not worth avoiding that.
252252
let shift_amount = match shift_amount {
253253
Either::Left(v) => v.to_string(),
254254
Either::Right(v) => v.to_string(),
255255
};
256-
err_ub_format!("overflowing shift by {shift_amount} in `simd_{intrinsic_name}` in lane {i}").into()
256+
err_ub_format!("overflowing shift by {shift_amount} in `simd_{intrinsic_name}` in lane {i}")
257257
}
258-
_ => err
258+
kind => kind
259259
}
260260
})?;
261261
if matches!(mir_op, BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge) {

‎src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,10 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
289289
"miri_get_alloc_id" => {
290290
let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
291291
let ptr = this.read_pointer(ptr)?;
292-
let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr, 0).map_err(|_e| {
292+
let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr, 0).map_err_kind(|_e| {
293293
err_machine_stop!(TerminationInfo::Abort(format!(
294294
"pointer passed to `miri_get_alloc_id` must not be dangling, got {ptr:?}"
295295
)))
296-
.into()
297296
})?;
298297
this.write_scalar(Scalar::from_u64(alloc_id.0.get()), dest)?;
299298
}

0 commit comments

Comments
 (0)
Please sign in to comment.