rustc_const_eval/interpret/
eval_context.rs

1use std::assert_matches::debug_assert_matches;
2
3use either::{Left, Right};
4use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout};
5use rustc_errors::DiagCtxtHandle;
6use rustc_hir::def_id::DefId;
7use rustc_infer::infer::TyCtxtInferExt;
8use rustc_infer::infer::at::ToTrace;
9use rustc_infer::traits::ObligationCause;
10use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
11use rustc_middle::query::TyCtxtAt;
12use rustc_middle::ty::layout::{
13    self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
14};
15use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypingEnv, Variance};
16use rustc_middle::{mir, span_bug};
17use rustc_session::Limit;
18use rustc_span::Span;
19use rustc_target::callconv::FnAbi;
20use rustc_trait_selection::traits::ObligationCtxt;
21use tracing::{debug, instrument, trace};
22
23use super::{
24    Frame, FrameInfo, GlobalId, InterpErrorInfo, InterpErrorKind, InterpResult, MPlaceTy, Machine,
25    MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
26    err_inval, interp_ok, throw_inval, throw_ub, throw_ub_custom,
27};
28use crate::{ReportErrorExt, fluent_generated as fluent, util};
29
30pub struct InterpCx<'tcx, M: Machine<'tcx>> {
31    /// Stores the `Machine` instance.
32    ///
33    /// Note: the stack is provided by the machine.
34    pub machine: M,
35
36    /// The results of the type checker, from rustc.
37    /// The span in this is the "root" of the evaluation, i.e., the const
38    /// we are evaluating (if this is CTFE).
39    pub tcx: TyCtxtAt<'tcx>,
40
41    /// The current context in case we're evaluating in a
42    /// polymorphic context. This always uses `ty::TypingMode::PostAnalysis`.
43    pub(super) typing_env: ty::TypingEnv<'tcx>,
44
45    /// The virtual memory system.
46    pub memory: Memory<'tcx, M>,
47
48    /// The recursion limit (cached from `tcx.recursion_limit(())`)
49    pub recursion_limit: Limit,
50}
51
52impl<'tcx, M: Machine<'tcx>> HasDataLayout for InterpCx<'tcx, M> {
53    #[inline]
54    fn data_layout(&self) -> &TargetDataLayout {
55        &self.tcx.data_layout
56    }
57}
58
59impl<'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'tcx, M>
60where
61    M: Machine<'tcx>,
62{
63    #[inline]
64    fn tcx(&self) -> TyCtxt<'tcx> {
65        *self.tcx
66    }
67}
68
69impl<'tcx, M> layout::HasTypingEnv<'tcx> for InterpCx<'tcx, M>
70where
71    M: Machine<'tcx>,
72{
73    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
74        self.typing_env
75    }
76}
77
78impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
79    type LayoutOfResult = Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>>;
80
81    #[inline]
82    fn layout_tcx_at_span(&self) -> Span {
83        // Using the cheap root span for performance.
84        self.tcx.span
85    }
86
87    #[inline]
88    fn handle_layout_err(
89        &self,
90        err: LayoutError<'tcx>,
91        _: Span,
92        _: Ty<'tcx>,
93    ) -> InterpErrorKind<'tcx> {
94        err_inval!(Layout(err))
95    }
96}
97
98impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
99    type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
100
101    fn handle_fn_abi_err(
102        &self,
103        err: FnAbiError<'tcx>,
104        _span: Span,
105        _fn_abi_request: FnAbiRequest<'tcx>,
106    ) -> InterpErrorKind<'tcx> {
107        match err {
108            FnAbiError::Layout(err) => err_inval!(Layout(err)),
109        }
110    }
111}
112
113/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
114/// This test should be symmetric, as it is primarily about layout compatibility.
115pub(super) fn mir_assign_valid_types<'tcx>(
116    tcx: TyCtxt<'tcx>,
117    typing_env: TypingEnv<'tcx>,
118    src: TyAndLayout<'tcx>,
119    dest: TyAndLayout<'tcx>,
120) -> bool {
121    // Type-changing assignments can happen when subtyping is used. While
122    // all normal lifetimes are erased, higher-ranked types with their
123    // late-bound lifetimes are still around and can lead to type
124    // differences.
125    if util::relate_types(tcx, typing_env, Variance::Covariant, src.ty, dest.ty) {
126        // Make sure the layout is equal, too -- just to be safe. Miri really
127        // needs layout equality. For performance reason we skip this check when
128        // the types are equal. Equal types *can* have different layouts when
129        // enum downcast is involved (as enum variants carry the type of the
130        // enum), but those should never occur in assignments.
131        if cfg!(debug_assertions) || src.ty != dest.ty {
132            assert_eq!(src.layout, dest.layout);
133        }
134        true
135    } else {
136        false
137    }
138}
139
140/// Use the already known layout if given (but sanity check in debug mode),
141/// or compute the layout.
142#[cfg_attr(not(debug_assertions), inline(always))]
143pub(super) fn from_known_layout<'tcx>(
144    tcx: TyCtxtAt<'tcx>,
145    typing_env: TypingEnv<'tcx>,
146    known_layout: Option<TyAndLayout<'tcx>>,
147    compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
148) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
149    match known_layout {
150        None => compute(),
151        Some(known_layout) => {
152            if cfg!(debug_assertions) {
153                let check_layout = compute()?;
154                if !mir_assign_valid_types(tcx.tcx, typing_env, check_layout, known_layout) {
155                    span_bug!(
156                        tcx.span,
157                        "expected type differs from actual type.\nexpected: {}\nactual: {}",
158                        known_layout.ty,
159                        check_layout.ty,
160                    );
161                }
162            }
163            interp_ok(known_layout)
164        }
165    }
166}
167
168/// Turn the given error into a human-readable string. Expects the string to be printed, so if
169/// `RUSTC_CTFE_BACKTRACE` is set this will show a backtrace of the rustc internals that
170/// triggered the error.
171///
172/// This is NOT the preferred way to render an error; use `report` from `const_eval` instead.
173/// However, this is useful when error messages appear in ICEs.
174pub fn format_interp_error<'tcx>(dcx: DiagCtxtHandle<'_>, e: InterpErrorInfo<'tcx>) -> String {
175    let (e, backtrace) = e.into_parts();
176    backtrace.print_backtrace();
177    // FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the
178    // label and arguments from the InterpError.
179    #[allow(rustc::untranslatable_diagnostic)]
180    let mut diag = dcx.struct_allow("");
181    let msg = e.diagnostic_message();
182    e.add_args(&mut diag);
183    let s = dcx.eagerly_translate_to_string(msg, diag.args.iter());
184    diag.cancel();
185    s
186}
187
188impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
189    pub fn new(
190        tcx: TyCtxt<'tcx>,
191        root_span: Span,
192        typing_env: ty::TypingEnv<'tcx>,
193        machine: M,
194    ) -> Self {
195        // Const eval always happens in post analysis mode in order to be able to use the hidden types of
196        // opaque types. This is needed for trivial things like `size_of`, but also for using associated
197        // types that are not specified in the opaque type. We also use MIR bodies whose opaque types have
198        // already been revealed, so we'd be able to at least partially observe the hidden types anyways.
199        debug_assert_matches!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
200        InterpCx {
201            machine,
202            tcx: tcx.at(root_span),
203            typing_env,
204            memory: Memory::new(),
205            recursion_limit: tcx.recursion_limit(),
206        }
207    }
208
209    /// Returns the span of the currently executed statement/terminator.
210    /// This is the span typically used for error reporting.
211    #[inline(always)]
212    pub fn cur_span(&self) -> Span {
213        // This deliberately does *not* honor `requires_caller_location` since it is used for much
214        // more than just panics.
215        self.stack().last().map_or(self.tcx.span, |f| f.current_span())
216    }
217
218    pub(crate) fn stack(&self) -> &[Frame<'tcx, M::Provenance, M::FrameExtra>] {
219        M::stack(self)
220    }
221
222    #[inline(always)]
223    pub(crate) fn stack_mut(&mut self) -> &mut Vec<Frame<'tcx, M::Provenance, M::FrameExtra>> {
224        M::stack_mut(self)
225    }
226
227    #[inline(always)]
228    pub fn frame_idx(&self) -> usize {
229        let stack = self.stack();
230        assert!(!stack.is_empty());
231        stack.len() - 1
232    }
233
234    #[inline(always)]
235    pub fn frame(&self) -> &Frame<'tcx, M::Provenance, M::FrameExtra> {
236        self.stack().last().expect("no call frames exist")
237    }
238
239    #[inline(always)]
240    pub fn frame_mut(&mut self) -> &mut Frame<'tcx, M::Provenance, M::FrameExtra> {
241        self.stack_mut().last_mut().expect("no call frames exist")
242    }
243
244    #[inline(always)]
245    pub fn body(&self) -> &'tcx mir::Body<'tcx> {
246        self.frame().body
247    }
248
249    #[inline]
250    pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
251        ty.is_freeze(*self.tcx, self.typing_env)
252    }
253
254    pub fn load_mir(
255        &self,
256        instance: ty::InstanceKind<'tcx>,
257        promoted: Option<mir::Promoted>,
258    ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
259        trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
260        let body = if let Some(promoted) = promoted {
261            let def = instance.def_id();
262            &self.tcx.promoted_mir(def)[promoted]
263        } else {
264            M::load_mir(self, instance)?
265        };
266        // do not continue if typeck errors occurred (can only occur in local crate)
267        if let Some(err) = body.tainted_by_errors {
268            throw_inval!(AlreadyReported(ReportedErrorInfo::non_const_eval_error(err)));
269        }
270        interp_ok(body)
271    }
272
273    /// Call this on things you got out of the MIR (so it is as generic as the current
274    /// stack frame), to bring it into the proper environment for this interpreter.
275    pub(super) fn instantiate_from_current_frame_and_normalize_erasing_regions<
276        T: TypeFoldable<TyCtxt<'tcx>>,
277    >(
278        &self,
279        value: T,
280    ) -> Result<T, ErrorHandled> {
281        self.instantiate_from_frame_and_normalize_erasing_regions(self.frame(), value)
282    }
283
284    /// Call this on things you got out of the MIR (so it is as generic as the provided
285    /// stack frame), to bring it into the proper environment for this interpreter.
286    pub(super) fn instantiate_from_frame_and_normalize_erasing_regions<
287        T: TypeFoldable<TyCtxt<'tcx>>,
288    >(
289        &self,
290        frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
291        value: T,
292    ) -> Result<T, ErrorHandled> {
293        frame
294            .instance
295            .try_instantiate_mir_and_normalize_erasing_regions(
296                *self.tcx,
297                self.typing_env,
298                ty::EarlyBinder::bind(value),
299            )
300            .map_err(|_| ErrorHandled::TooGeneric(self.cur_span()))
301    }
302
303    /// The `args` are assumed to already be in our interpreter "universe".
304    pub(super) fn resolve(
305        &self,
306        def: DefId,
307        args: GenericArgsRef<'tcx>,
308    ) -> InterpResult<'tcx, ty::Instance<'tcx>> {
309        trace!("resolve: {:?}, {:#?}", def, args);
310        trace!("typing_env: {:#?}", self.typing_env);
311        trace!("args: {:#?}", args);
312        match ty::Instance::try_resolve(*self.tcx, self.typing_env, def, args) {
313            Ok(Some(instance)) => interp_ok(instance),
314            Ok(None) => throw_inval!(TooGeneric),
315
316            // FIXME(eddyb) this could be a bit more specific than `AlreadyReported`.
317            Err(error_guaranteed) => throw_inval!(AlreadyReported(
318                ReportedErrorInfo::non_const_eval_error(error_guaranteed)
319            )),
320        }
321    }
322
323    /// Check if the two things are equal in the current param_env, using an infcx to get proper
324    /// equality checks.
325    #[instrument(level = "trace", skip(self), ret)]
326    pub(super) fn eq_in_param_env<T>(&self, a: T, b: T) -> bool
327    where
328        T: PartialEq + TypeFoldable<TyCtxt<'tcx>> + ToTrace<'tcx>,
329    {
330        // Fast path: compare directly.
331        if a == b {
332            return true;
333        }
334        // Slow path: spin up an inference context to check if these traits are sufficiently equal.
335        let (infcx, param_env) = self.tcx.infer_ctxt().build_with_typing_env(self.typing_env);
336        let ocx = ObligationCtxt::new(&infcx);
337        let cause = ObligationCause::dummy_with_span(self.cur_span());
338        // equate the two trait refs after normalization
339        let a = ocx.normalize(&cause, param_env, a);
340        let b = ocx.normalize(&cause, param_env, b);
341
342        if let Err(terr) = ocx.eq(&cause, param_env, a, b) {
343            trace!(?terr);
344            return false;
345        }
346
347        let errors = ocx.select_all_or_error();
348        if !errors.is_empty() {
349            trace!(?errors);
350            return false;
351        }
352
353        // All good.
354        true
355    }
356
357    /// Walks up the callstack from the intrinsic's callsite, searching for the first callsite in a
358    /// frame which is not `#[track_caller]`. This matches the `caller_location` intrinsic,
359    /// and is primarily intended for the panic machinery.
360    pub(crate) fn find_closest_untracked_caller_location(&self) -> Span {
361        for frame in self.stack().iter().rev() {
362            debug!("find_closest_untracked_caller_location: checking frame {:?}", frame.instance);
363
364            // Assert that the frame we look at is actually executing code currently
365            // (`loc` is `Right` when we are unwinding and the frame does not require cleanup).
366            let loc = frame.loc.left().unwrap();
367
368            // This could be a non-`Call` terminator (such as `Drop`), or not a terminator at all
369            // (such as `box`). Use the normal span by default.
370            let mut source_info = *frame.body.source_info(loc);
371
372            // If this is a `Call` terminator, use the `fn_span` instead.
373            let block = &frame.body.basic_blocks[loc.block];
374            if loc.statement_index == block.statements.len() {
375                debug!(
376                    "find_closest_untracked_caller_location: got terminator {:?} ({:?})",
377                    block.terminator(),
378                    block.terminator().kind,
379                );
380                if let mir::TerminatorKind::Call { fn_span, .. } = block.terminator().kind {
381                    source_info.span = fn_span;
382                }
383            }
384
385            let caller_location = if frame.instance.def.requires_caller_location(*self.tcx) {
386                // We use `Err(())` as indication that we should continue up the call stack since
387                // this is a `#[track_caller]` function.
388                Some(Err(()))
389            } else {
390                None
391            };
392            if let Ok(span) =
393                frame.body.caller_location_span(source_info, caller_location, *self.tcx, Ok)
394            {
395                return span;
396            }
397        }
398
399        span_bug!(self.cur_span(), "no non-`#[track_caller]` frame found")
400    }
401
402    /// Returns the actual dynamic size and alignment of the place at the given type.
403    /// Only the "meta" (metadata) part of the place matters.
404    /// This can fail to provide an answer for extern types.
405    pub(super) fn size_and_align_of(
406        &self,
407        metadata: &MemPlaceMeta<M::Provenance>,
408        layout: &TyAndLayout<'tcx>,
409    ) -> InterpResult<'tcx, Option<(Size, Align)>> {
410        if layout.is_sized() {
411            return interp_ok(Some((layout.size, layout.align.abi)));
412        }
413        match layout.ty.kind() {
414            ty::Adt(..) | ty::Tuple(..) => {
415                // First get the size of all statically known fields.
416                // Don't use type_of::sizing_type_of because that expects t to be sized,
417                // and it also rounds up to alignment, which we want to avoid,
418                // as the unsized field's alignment could be smaller.
419                assert!(!layout.ty.is_simd());
420                assert!(layout.fields.count() > 0);
421                trace!("DST layout: {:?}", layout);
422
423                let unsized_offset_unadjusted = layout.fields.offset(layout.fields.count() - 1);
424                let sized_align = layout.align.abi;
425
426                // Recurse to get the size of the dynamically sized field (must be
427                // the last field). Can't have foreign types here, how would we
428                // adjust alignment and size for them?
429                let field = layout.field(self, layout.fields.count() - 1);
430                let Some((unsized_size, mut unsized_align)) =
431                    self.size_and_align_of(metadata, &field)?
432                else {
433                    // A field with an extern type. We don't know the actual dynamic size
434                    // or the alignment.
435                    return interp_ok(None);
436                };
437
438                // # First compute the dynamic alignment
439
440                // Packed type alignment needs to be capped.
441                if let ty::Adt(def, _) = layout.ty.kind() {
442                    if let Some(packed) = def.repr().pack {
443                        unsized_align = unsized_align.min(packed);
444                    }
445                }
446
447                // Choose max of two known alignments (combined value must
448                // be aligned according to more restrictive of the two).
449                let full_align = sized_align.max(unsized_align);
450
451                // # Then compute the dynamic size
452
453                let unsized_offset_adjusted = unsized_offset_unadjusted.align_to(unsized_align);
454                let full_size = (unsized_offset_adjusted + unsized_size).align_to(full_align);
455
456                // Just for our sanitiy's sake, assert that this is equal to what codegen would compute.
457                assert_eq!(
458                    full_size,
459                    (unsized_offset_unadjusted + unsized_size).align_to(full_align)
460                );
461
462                // Check if this brought us over the size limit.
463                if full_size > self.max_size_of_val() {
464                    throw_ub!(InvalidMeta(InvalidMetaKind::TooBig));
465                }
466                interp_ok(Some((full_size, full_align)))
467            }
468            ty::Dynamic(expected_trait, _, ty::Dyn) => {
469                let vtable = metadata.unwrap_meta().to_pointer(self)?;
470                // Read size and align from vtable (already checks size).
471                interp_ok(Some(self.get_vtable_size_and_align(vtable, Some(expected_trait))?))
472            }
473
474            ty::Slice(_) | ty::Str => {
475                let len = metadata.unwrap_meta().to_target_usize(self)?;
476                let elem = layout.field(self, 0);
477
478                // Make sure the slice is not too big.
479                let size = elem.size.bytes().saturating_mul(len); // we rely on `max_size_of_val` being smaller than `u64::MAX`.
480                let size = Size::from_bytes(size);
481                if size > self.max_size_of_val() {
482                    throw_ub!(InvalidMeta(InvalidMetaKind::SliceTooBig));
483                }
484                interp_ok(Some((size, elem.align.abi)))
485            }
486
487            ty::Foreign(_) => interp_ok(None),
488
489            _ => span_bug!(self.cur_span(), "size_and_align_of::<{}> not supported", layout.ty),
490        }
491    }
492    #[inline]
493    pub fn size_and_align_of_mplace(
494        &self,
495        mplace: &MPlaceTy<'tcx, M::Provenance>,
496    ) -> InterpResult<'tcx, Option<(Size, Align)>> {
497        self.size_and_align_of(&mplace.meta(), &mplace.layout)
498    }
499
500    /// Jump to the given block.
501    #[inline]
502    pub fn go_to_block(&mut self, target: mir::BasicBlock) {
503        self.frame_mut().loc = Left(mir::Location { block: target, statement_index: 0 });
504    }
505
506    /// *Return* to the given `target` basic block.
507    /// Do *not* use for unwinding! Use `unwind_to_block` instead.
508    ///
509    /// If `target` is `None`, that indicates the function cannot return, so we raise UB.
510    pub fn return_to_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
511        if let Some(target) = target {
512            self.go_to_block(target);
513            interp_ok(())
514        } else {
515            throw_ub!(Unreachable)
516        }
517    }
518
519    /// *Unwind* to the given `target` basic block.
520    /// Do *not* use for returning! Use `return_to_block` instead.
521    ///
522    /// If `target` is `UnwindAction::Continue`, that indicates the function does not need cleanup
523    /// during unwinding, and we will just keep propagating that upwards.
524    ///
525    /// If `target` is `UnwindAction::Unreachable`, that indicates the function does not allow
526    /// unwinding, and doing so is UB.
527    #[cold] // usually we have normal returns, not unwinding
528    pub fn unwind_to_block(&mut self, target: mir::UnwindAction) -> InterpResult<'tcx> {
529        self.frame_mut().loc = match target {
530            mir::UnwindAction::Cleanup(block) => Left(mir::Location { block, statement_index: 0 }),
531            mir::UnwindAction::Continue => Right(self.frame_mut().body.span),
532            mir::UnwindAction::Unreachable => {
533                throw_ub_custom!(fluent::const_eval_unreachable_unwind);
534            }
535            mir::UnwindAction::Terminate(reason) => {
536                self.frame_mut().loc = Right(self.frame_mut().body.span);
537                M::unwind_terminate(self, reason)?;
538                // This might have pushed a new stack frame, or it terminated execution.
539                // Either way, `loc` will not be updated.
540                return interp_ok(());
541            }
542        };
543        interp_ok(())
544    }
545
546    /// Call a query that can return `ErrorHandled`. Should be used for statics and other globals.
547    /// (`mir::Const`/`ty::Const` have `eval` methods that can be used directly instead.)
548    pub fn ctfe_query<T>(
549        &self,
550        query: impl FnOnce(TyCtxtAt<'tcx>) -> Result<T, ErrorHandled>,
551    ) -> Result<T, ErrorHandled> {
552        // Use a precise span for better cycle errors.
553        query(self.tcx.at(self.cur_span())).map_err(|err| {
554            err.emit_note(*self.tcx);
555            err
556        })
557    }
558
559    pub fn eval_global(
560        &self,
561        instance: ty::Instance<'tcx>,
562    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
563        let gid = GlobalId { instance, promoted: None };
564        let val = if self.tcx.is_static(gid.instance.def_id()) {
565            let alloc_id = self.tcx.reserve_and_set_static_alloc(gid.instance.def_id());
566
567            let ty = instance.ty(self.tcx.tcx, self.typing_env);
568            mir::ConstAlloc { alloc_id, ty }
569        } else {
570            self.ctfe_query(|tcx| tcx.eval_to_allocation_raw(self.typing_env.as_query_input(gid)))?
571        };
572        self.raw_const_to_mplace(val)
573    }
574
575    pub fn eval_mir_constant(
576        &self,
577        val: &mir::Const<'tcx>,
578        span: Span,
579        layout: Option<TyAndLayout<'tcx>>,
580    ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
581        M::eval_mir_constant(self, *val, span, layout, |ecx, val, span, layout| {
582            let const_val = val.eval(*ecx.tcx, ecx.typing_env, span).map_err(|err| {
583                if M::ALL_CONSTS_ARE_PRECHECKED {
584                    match err {
585                        ErrorHandled::TooGeneric(..) => {},
586                        ErrorHandled::Reported(reported, span) => {
587                            if reported.is_allowed_in_infallible() {
588                                // These errors can just sometimes happen, even when the expression
589                                // is nominally "infallible", e.g. when running out of memory
590                                // or when some layout could not be computed.
591                            } else {
592                                // Looks like the const is not captured by `required_consts`, that's bad.
593                                span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");
594                            }
595                        }
596                    }
597                }
598                err.emit_note(*ecx.tcx);
599                err
600            })?;
601            ecx.const_val_to_op(const_val, val.ty(), layout)
602        })
603    }
604
605    #[must_use]
606    pub fn dump_place(&self, place: &PlaceTy<'tcx, M::Provenance>) -> PlacePrinter<'_, 'tcx, M> {
607        PlacePrinter { ecx: self, place: *place.place() }
608    }
609
610    #[must_use]
611    pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
612        Frame::generate_stacktrace_from_stack(self.stack())
613    }
614
615    pub fn adjust_nan<F1, F2>(&self, f: F2, inputs: &[F1]) -> F2
616    where
617        F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F2>,
618        F2: rustc_apfloat::Float,
619    {
620        if f.is_nan() { M::generate_nan(self, inputs) } else { f }
621    }
622}
623
624#[doc(hidden)]
625/// Helper struct for the `dump_place` function.
626pub struct PlacePrinter<'a, 'tcx, M: Machine<'tcx>> {
627    ecx: &'a InterpCx<'tcx, M>,
628    place: Place<M::Provenance>,
629}
630
631impl<'a, 'tcx, M: Machine<'tcx>> std::fmt::Debug for PlacePrinter<'a, 'tcx, M> {
632    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
633        match self.place {
634            Place::Local { local, offset, locals_addr } => {
635                debug_assert_eq!(locals_addr, self.ecx.frame().locals_addr());
636                let mut allocs = Vec::new();
637                write!(fmt, "{local:?}")?;
638                if let Some(offset) = offset {
639                    write!(fmt, "+{:#x}", offset.bytes())?;
640                }
641                write!(fmt, ":")?;
642
643                self.ecx.frame().locals[local].print(&mut allocs, fmt)?;
644
645                write!(fmt, ": {:?}", self.ecx.dump_allocs(allocs.into_iter().flatten().collect()))
646            }
647            Place::Ptr(mplace) => match mplace.ptr.provenance.and_then(Provenance::get_alloc_id) {
648                Some(alloc_id) => {
649                    write!(fmt, "by ref {:?}: {:?}", mplace.ptr, self.ecx.dump_alloc(alloc_id))
650                }
651                ptr => write!(fmt, " integral by ref: {ptr:?}"),
652            },
653        }
654    }
655}