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