Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 13a29b8

Browse files
committed
Pull out const error reporting into its own function
1 parent 500dbd4 commit 13a29b8

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ where
117117
/// This will use the `mk` function for creating the error which will get passed labels according to
118118
/// the `InterpError` and the span and a stacktrace of current execution according to
119119
/// `get_span_and_frames`.
120-
pub(super) fn report<'tcx, C, F, E>(
120+
pub(crate) fn report<'tcx, C, F, E>(
121121
tcx: TyCtxt<'tcx>,
122122
error: InterpError<'tcx>,
123123
span: Option<Span>,

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use either::{Left, Right};
44

55
use rustc_hir::def::DefKind;
66
use rustc_middle::mir::interpret::ErrorHandled;
7-
use rustc_middle::mir::pretty::write_allocation_bytes;
87
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
98
use rustc_middle::traits::Reveal;
109
use rustc_middle::ty::layout::LayoutOf;
@@ -19,8 +18,8 @@ use crate::errors;
1918
use crate::errors::ConstEvalError;
2019
use crate::interpret::eval_nullary_intrinsic;
2120
use crate::interpret::{
22-
intern_const_alloc_recursive, GlobalId, Immediate, InternKind, InterpCx, InterpError,
23-
InterpResult, MPlaceTy, MemoryKind, OpTy, StackPopCleanup,
21+
intern_const_alloc_recursive, GlobalId, Immediate, InternKind, InterpCx, InterpResult,
22+
MPlaceTy, MemoryKind, OpTy, StackPopCleanup,
2423
};
2524

2625
// Returns a pointer to where the result lives
@@ -337,36 +336,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
337336

338337
// Validation failed, report an error.
339338
if let Err(error) = validation {
340-
let (error, backtrace) = error.into_parts();
341-
backtrace.print_backtrace();
342-
343-
let ub_note = matches!(error, InterpError::UndefinedBehavior(_)).then(|| {});
344-
345-
let alloc = ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner();
346-
let mut bytes = String::new();
347-
if alloc.size() != abi::Size::ZERO {
348-
bytes = "\n".into();
349-
// FIXME(translation) there might be pieces that are translatable.
350-
write_allocation_bytes(*ecx.tcx, alloc, &mut bytes, " ").unwrap();
351-
}
352-
let raw_bytes = errors::RawBytesNote {
353-
size: alloc.size().bytes(),
354-
align: alloc.align.bytes(),
355-
bytes,
356-
};
357-
358-
Err(super::report(
359-
*ecx.tcx,
360-
error,
361-
None,
362-
|| super::get_span_and_frames(&ecx),
363-
move |span, frames| errors::UndefinedBehavior {
364-
span,
365-
ub_note,
366-
frames,
367-
raw_bytes,
368-
},
369-
))
339+
Err(ecx.const_report_error(error, alloc_id))
370340
} else {
371341
// Convert to raw constant
372342
Ok(ConstAlloc { alloc_id, ty: mplace.layout.ty })

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ use rustc_ast::Mutability;
1313
use rustc_data_structures::fx::FxHashSet;
1414
use rustc_hir as hir;
1515
use rustc_middle::mir::interpret::{
16-
ExpectedKind, InterpError, InvalidMetaKind, Misalignment, PointerKind, ValidationErrorInfo,
17-
ValidationErrorKind, ValidationErrorKind::*,
16+
ErrorHandled, ExpectedKind, InterpError, InterpErrorInfo, InvalidMetaKind, Misalignment,
17+
PointerKind, ValidationErrorInfo, ValidationErrorKind, ValidationErrorKind::*,
1818
};
19+
use rustc_middle::mir::pretty::write_allocation_bytes;
1920
use rustc_middle::ty;
2021
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
2122
use rustc_span::symbol::{sym, Symbol};
2223
use rustc_target::abi::{
23-
Abi, FieldIdx, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange,
24+
self, Abi, FieldIdx, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange,
2425
};
2526

2627
use std::hash::Hash;
2728

29+
use crate::errors;
30+
2831
use super::{
2932
AllocId, CheckInAllocMsg, GlobalAlloc, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy,
3033
Machine, MemPlaceMeta, OpTy, Pointer, Projectable, Scalar, ValueVisitor,
@@ -967,6 +970,36 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
967970
Ok(())
968971
}
969972

973+
#[inline(always)]
974+
pub fn const_report_error(
975+
&self,
976+
error: InterpErrorInfo<'tcx>,
977+
alloc_id: AllocId,
978+
) -> ErrorHandled {
979+
let (error, backtrace) = error.into_parts();
980+
backtrace.print_backtrace();
981+
982+
let ub_note = matches!(error, InterpError::UndefinedBehavior(_)).then(|| {});
983+
984+
let alloc = self.tcx.global_alloc(alloc_id).unwrap_memory().inner();
985+
let mut bytes = String::new();
986+
if alloc.size() != abi::Size::ZERO {
987+
bytes = "\n".into();
988+
// FIXME(translation) there might be pieces that are translatable.
989+
write_allocation_bytes(*self.tcx, alloc, &mut bytes, " ").unwrap();
990+
}
991+
let raw_bytes =
992+
errors::RawBytesNote { size: alloc.size().bytes(), align: alloc.align.bytes(), bytes };
993+
994+
crate::const_eval::report(
995+
*self.tcx,
996+
error,
997+
None,
998+
|| crate::const_eval::get_span_and_frames(self),
999+
move |span, frames| errors::UndefinedBehavior { span, ub_note, frames, raw_bytes },
1000+
)
1001+
}
1002+
9701003
/// This function checks the data at `op` to be runtime-valid.
9711004
/// `op` is assumed to cover valid memory if it is an indirect operand.
9721005
/// It will error if the bits at the destination do not match the ones described by the layout.

0 commit comments

Comments
 (0)