Skip to main content

rustc_const_eval/interpret/
eval_context.rs

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