Skip to main content

rustc_middle/mir/interpret/
mod.rs

1//! An interpreter for MIR used in CTFE and by miri.
2
3#[macro_use]
4mod error;
5
6mod allocation;
7mod pointer;
8mod queries;
9mod value;
10
11use std::io::{Read, Write};
12use std::num::NonZero;
13use std::{fmt, io};
14
15use rustc_abi::{AddressSpace, Align, Endian, HasDataLayout, Size};
16use rustc_ast::Mutability;
17use rustc_data_structures::fx::FxHashMap;
18use rustc_data_structures::sharded::ShardedHashMap;
19use rustc_data_structures::sync::{AtomicU64, Lock};
20use rustc_hir::def::DefKind;
21use rustc_hir::def_id::{DefId, LocalDefId};
22use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
23use rustc_serialize::{Decodable, Encodable};
24use tracing::{debug, trace};
25// Also make the error macros available from this module.
26pub use {
27    err_exhaust, err_inval, err_machine_stop, err_ub, err_ub_format, err_unsup, err_unsup_format,
28    throw_exhaust, throw_inval, throw_machine_stop, throw_ub, throw_ub_format, throw_unsup,
29    throw_unsup_format,
30};
31
32pub use self::allocation::{
33    AllocBytes, AllocError, AllocInit, AllocRange, AllocResult, Allocation, ConstAllocation,
34    InitChunk, InitChunkIter, alloc_range,
35};
36pub use self::error::{
37    BadBytesAccess, CheckAlignMsg, CheckInAllocMsg, ErrorHandled, EvalStaticInitializerRawResult,
38    EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, InterpErrorInfo,
39    InterpErrorKind, InterpResult, InvalidMetaKind, InvalidProgramInfo, MachineStopType,
40    Misalignment, ReportedErrorInfo, ResourceExhaustionInfo, ScalarSizeMismatch,
41    UndefinedBehaviorInfo, UnsupportedOpInfo, ValTreeCreationError, interp_ok,
42};
43pub use self::pointer::{CtfeProvenance, Pointer, PointerArithmetic, Provenance};
44pub use self::value::Scalar;
45use crate::mir;
46use crate::ty::codec::{TyDecoder, TyEncoder};
47use crate::ty::print::with_no_trimmed_paths;
48use crate::ty::{self, Instance, Ty, TyCtxt};
49
50/// Uniquely identifies one of the following:
51/// - A constant
52/// - A static
53#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for GlobalId<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for GlobalId<'tcx> {
    #[inline]
    fn clone(&self) -> GlobalId<'tcx> {
        let _: ::core::clone::AssertParamIsClone<ty::Instance<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<Option<mir::Promoted>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for GlobalId<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "GlobalId",
            "instance", &self.instance, "promoted", &&self.promoted)
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for GlobalId<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<ty::Instance<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<Option<mir::Promoted>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for GlobalId<'tcx> {
    #[inline]
    fn eq(&self, other: &GlobalId<'tcx>) -> bool {
        self.instance == other.instance && self.promoted == other.promoted
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for GlobalId<'tcx> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.instance, state);
        ::core::hash::Hash::hash(&self.promoted, state)
    }
}Hash, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for GlobalId<'tcx> {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    GlobalId {
                        instance: ref __binding_0, promoted: ref __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for GlobalId<'tcx> {
            fn decode(__decoder: &mut __D) -> Self {
                GlobalId {
                    instance: ::rustc_serialize::Decodable::decode(__decoder),
                    promoted: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };TyDecodable)]
54#[derive(const _: () =
    {
        impl<'tcx, '__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for GlobalId<'tcx> {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    GlobalId {
                        instance: ref __binding_0, promoted: ref __binding_1 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable, const _: () =
    {
        impl<'tcx>
            ::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
            for GlobalId<'tcx> {
            fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
                __folder: &mut __F) -> Result<Self, __F::Error> {
                Ok(match self {
                        GlobalId { instance: __binding_0, promoted: __binding_1 } =>
                            {
                            GlobalId {
                                instance: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
                                        __folder)?,
                                promoted: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
                                        __folder)?,
                            }
                        }
                    })
            }
            fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
                __folder: &mut __F) -> Self {
                match self {
                    GlobalId { instance: __binding_0, promoted: __binding_1 } =>
                        {
                        GlobalId {
                            instance: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
                                __folder),
                            promoted: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
                                __folder),
                        }
                    }
                }
            }
        }
    };TypeFoldable, const _: () =
    {
        impl<'tcx>
            ::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
            for GlobalId<'tcx> {
            fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
                __visitor: &mut __V) -> __V::Result {
                match *self {
                    GlobalId {
                        instance: ref __binding_0, promoted: ref __binding_1 } => {
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                    }
                }
                <__V::Result as ::rustc_middle::ty::VisitorResult>::output()
            }
        }
    };TypeVisitable)]
55pub struct GlobalId<'tcx> {
56    /// For a constant or static, the `Instance` of the item itself.
57    /// For a promoted global, the `Instance` of the function they belong to.
58    pub instance: ty::Instance<'tcx>,
59
60    /// The index for promoted globals within their function's `mir::Body`.
61    pub promoted: Option<mir::Promoted>,
62}
63
64impl<'tcx> GlobalId<'tcx> {
65    pub fn display(self, tcx: TyCtxt<'tcx>) -> String {
66        let instance_name = {
    let _guard = NoTrimmedGuard::new();
    tcx.def_path_str(self.instance.def.def_id())
}with_no_trimmed_paths!(tcx.def_path_str(self.instance.def.def_id()));
67        if let Some(promoted) = self.promoted {
68            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}::{1:?}", instance_name,
                promoted))
    })format!("{instance_name}::{promoted:?}")
69        } else {
70            instance_name
71        }
72    }
73}
74
75#[derive(#[automatically_derived]
impl ::core::marker::Copy for AllocId { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AllocId {
    #[inline]
    fn clone(&self) -> AllocId {
        let _: ::core::clone::AssertParamIsClone<NonZero<u64>>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::Eq for AllocId {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<NonZero<u64>>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for AllocId {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}Hash, #[automatically_derived]
impl ::core::cmp::Ord for AllocId {
    #[inline]
    fn cmp(&self, other: &AllocId) -> ::core::cmp::Ordering {
        ::core::cmp::Ord::cmp(&self.0, &other.0)
    }
}Ord, #[automatically_derived]
impl ::core::cmp::PartialEq for AllocId {
    #[inline]
    fn eq(&self, other: &AllocId) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::PartialOrd for AllocId {
    #[inline]
    fn partial_cmp(&self, other: &AllocId)
        -> ::core::option::Option<::core::cmp::Ordering> {
        ::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
    }
}PartialOrd)]
76pub struct AllocId(pub NonZero<u64>);
77
78// AllocId show up in const-eval error messages
79impl fmt::Display for AllocId {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        if f.alternate() { f.write_fmt(format_args!("a{0}", self.0))write!(f, "a{}", self.0) } else { f.write_fmt(format_args!("alloc{0}", self.0))write!(f, "alloc{}", self.0) }
82    }
83}
84
85// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
86// all the Miri types.
87impl fmt::Debug for AllocId {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        // Dispatch to `Display` impl.
90        fmt::Display::fmt(self, f)
91    }
92}
93
94#[derive(const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for AllocDiscriminant {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { AllocDiscriminant::Alloc }
                    1usize => { AllocDiscriminant::Fn }
                    2usize => { AllocDiscriminant::VTable }
                    3usize => { AllocDiscriminant::Static }
                    4usize => { AllocDiscriminant::Type }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `AllocDiscriminant`, expected 0..5, actual {0}",
                                n));
                    }
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for AllocDiscriminant {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        AllocDiscriminant::Alloc => { 0usize }
                        AllocDiscriminant::Fn => { 1usize }
                        AllocDiscriminant::VTable => { 2usize }
                        AllocDiscriminant::Static => { 3usize }
                        AllocDiscriminant::Type => { 4usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    AllocDiscriminant::Alloc => {}
                    AllocDiscriminant::Fn => {}
                    AllocDiscriminant::VTable => {}
                    AllocDiscriminant::Static => {}
                    AllocDiscriminant::Type => {}
                }
            }
        }
    };TyEncodable)]
95enum AllocDiscriminant {
96    Alloc,
97    Fn,
98    VTable,
99    Static,
100    Type,
101}
102
103pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<'tcx>>(
104    encoder: &mut E,
105    tcx: TyCtxt<'tcx>,
106    alloc_id: AllocId,
107) {
108    match tcx.global_alloc(alloc_id) {
109        GlobalAlloc::Memory(alloc) => {
110            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:110",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(110u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("encoding {0:?} with {1:#?}",
                                                    alloc_id, alloc) as &dyn Value))])
            });
    } else { ; }
};trace!("encoding {:?} with {:#?}", alloc_id, alloc);
111            AllocDiscriminant::Alloc.encode(encoder);
112            alloc.encode(encoder);
113        }
114        GlobalAlloc::Function { instance } => {
115            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:115",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(115u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("encoding {0:?} with {1:#?}",
                                                    alloc_id, instance) as &dyn Value))])
            });
    } else { ; }
};trace!("encoding {:?} with {:#?}", alloc_id, instance);
116            AllocDiscriminant::Fn.encode(encoder);
117            instance.encode(encoder);
118        }
119        GlobalAlloc::VTable(ty, poly_trait_ref) => {
120            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:120",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(120u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("encoding {0:?} with {1:#?}, {2:#?}",
                                                    alloc_id, ty, poly_trait_ref) as &dyn Value))])
            });
    } else { ; }
};trace!("encoding {:?} with {ty:#?}, {poly_trait_ref:#?}", alloc_id);
121            AllocDiscriminant::VTable.encode(encoder);
122            ty.encode(encoder);
123            poly_trait_ref.encode(encoder);
124        }
125        GlobalAlloc::TypeId { ty } => {
126            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:126",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(126u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("encoding {0:?} with {1:#?}",
                                                    alloc_id, ty) as &dyn Value))])
            });
    } else { ; }
};trace!("encoding {alloc_id:?} with {ty:#?}");
127            AllocDiscriminant::Type.encode(encoder);
128            ty.encode(encoder);
129        }
130        GlobalAlloc::Static(did) => {
131            if !!tcx.is_thread_local_static(did) {
    ::core::panicking::panic("assertion failed: !tcx.is_thread_local_static(did)")
};assert!(!tcx.is_thread_local_static(did));
132            // References to statics doesn't need to know about their allocations,
133            // just about its `DefId`.
134            AllocDiscriminant::Static.encode(encoder);
135            // Cannot use `did.encode(encoder)` because of a bug around
136            // specializations and method calls.
137            Encodable::<E>::encode(&did, encoder);
138        }
139    }
140}
141
142#[derive(#[automatically_derived]
impl ::core::clone::Clone for State {
    #[inline]
    fn clone(&self) -> State {
        match self {
            State::Empty => State::Empty,
            State::Done(__self_0) =>
                State::Done(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone)]
143enum State {
144    Empty,
145    Done(AllocId),
146}
147
148pub struct AllocDecodingState {
149    // For each `AllocId`, we keep track of which decoding state it's currently in.
150    decoding_state: Vec<Lock<State>>,
151    // The offsets of each allocation in the data stream.
152    data_offsets: Vec<u64>,
153}
154
155impl AllocDecodingState {
156    #[inline]
157    pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
158        AllocDecodingSession { state: self }
159    }
160
161    pub fn new(data_offsets: Vec<u64>) -> Self {
162        let decoding_state =
163            std::iter::repeat_with(|| Lock::new(State::Empty)).take(data_offsets.len()).collect();
164
165        Self { decoding_state, data_offsets }
166    }
167}
168
169#[derive(#[automatically_derived]
impl<'s> ::core::marker::Copy for AllocDecodingSession<'s> { }Copy, #[automatically_derived]
impl<'s> ::core::clone::Clone for AllocDecodingSession<'s> {
    #[inline]
    fn clone(&self) -> AllocDecodingSession<'s> {
        let _: ::core::clone::AssertParamIsClone<&'s AllocDecodingState>;
        *self
    }
}Clone)]
170pub struct AllocDecodingSession<'s> {
171    state: &'s AllocDecodingState,
172}
173
174impl<'s> AllocDecodingSession<'s> {
175    /// Decodes an `AllocId` in a thread-safe way.
176    pub fn decode_alloc_id<'tcx, D>(&self, decoder: &mut D) -> AllocId
177    where
178        D: TyDecoder<'tcx>,
179    {
180        // Read the index of the allocation.
181        let idx = usize::try_from(decoder.read_u32()).unwrap();
182        let pos = usize::try_from(self.state.data_offsets[idx]).unwrap();
183
184        // Decode the `AllocDiscriminant` now so that we know if we have to reserve an
185        // `AllocId`.
186        let (alloc_kind, pos) = decoder.with_position(pos, |decoder| {
187            let alloc_kind = AllocDiscriminant::decode(decoder);
188            (alloc_kind, decoder.position())
189        });
190
191        // We are going to hold this lock during the entire decoding of this allocation, which may
192        // require that we decode other allocations. This cannot deadlock for two reasons:
193        //
194        // At the time of writing, it is only possible to create an allocation that contains a pointer
195        // to itself using the const_allocate intrinsic (which is for testing only), and even attempting
196        // to evaluate such consts blows the stack. If we ever grow a mechanism for producing
197        // cyclic allocations, we will need a new strategy for decoding that doesn't bring back
198        // https://github.com/rust-lang/rust/issues/126741.
199        //
200        // It is also impossible to create two allocations (call them A and B) where A is a pointer to B, and B
201        // is a pointer to A, because attempting to evaluate either of those consts will produce a
202        // query cycle, failing compilation.
203        let mut entry = self.state.decoding_state[idx].lock();
204        // Check the decoding state to see if it's already decoded or if we should
205        // decode it here.
206        if let State::Done(alloc_id) = *entry {
207            return alloc_id;
208        }
209
210        // Now decode the actual data.
211        let alloc_id = decoder.with_position(pos, |decoder| match alloc_kind {
212            AllocDiscriminant::Alloc => {
213                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:213",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(213u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("creating memory alloc ID")
                                            as &dyn Value))])
            });
    } else { ; }
};trace!("creating memory alloc ID");
214                let alloc = <ConstAllocation<'tcx> as Decodable<_>>::decode(decoder);
215                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:215",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(215u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("decoded alloc {0:?}",
                                                    alloc) as &dyn Value))])
            });
    } else { ; }
};trace!("decoded alloc {:?}", alloc);
216                decoder.interner().reserve_and_set_memory_alloc(alloc)
217            }
218            AllocDiscriminant::Fn => {
219                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:219",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(219u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("creating fn alloc ID")
                                            as &dyn Value))])
            });
    } else { ; }
};trace!("creating fn alloc ID");
220                let instance = ty::Instance::decode(decoder);
221                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:221",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(221u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("decoded fn alloc instance: {0:?}",
                                                    instance) as &dyn Value))])
            });
    } else { ; }
};trace!("decoded fn alloc instance: {:?}", instance);
222                decoder.interner().reserve_and_set_fn_alloc(instance, CTFE_ALLOC_SALT)
223            }
224            AllocDiscriminant::VTable => {
225                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:225",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(225u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("creating vtable alloc ID")
                                            as &dyn Value))])
            });
    } else { ; }
};trace!("creating vtable alloc ID");
226                let ty = Decodable::decode(decoder);
227                let poly_trait_ref = Decodable::decode(decoder);
228                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:228",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(228u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("decoded vtable alloc instance: {0:?}, {1:?}",
                                                    ty, poly_trait_ref) as &dyn Value))])
            });
    } else { ; }
};trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
229                decoder.interner().reserve_and_set_vtable_alloc(ty, poly_trait_ref, CTFE_ALLOC_SALT)
230            }
231            AllocDiscriminant::Type => {
232                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:232",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(232u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("creating typeid alloc ID")
                                            as &dyn Value))])
            });
    } else { ; }
};trace!("creating typeid alloc ID");
233                let ty = Decodable::decode(decoder);
234                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:234",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(234u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("decoded typid: {0:?}",
                                                    ty) as &dyn Value))])
            });
    } else { ; }
};trace!("decoded typid: {ty:?}");
235                decoder.interner().reserve_and_set_type_id_alloc(ty)
236            }
237            AllocDiscriminant::Static => {
238                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:238",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(238u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("creating extern static alloc ID")
                                            as &dyn Value))])
            });
    } else { ; }
};trace!("creating extern static alloc ID");
239                let did = <DefId as Decodable<D>>::decode(decoder);
240                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:240",
                        "rustc_middle::mir::interpret", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(240u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("decoded static def-ID: {0:?}",
                                                    did) as &dyn Value))])
            });
    } else { ; }
};trace!("decoded static def-ID: {:?}", did);
241                decoder.interner().reserve_and_set_static_alloc(did)
242            }
243        });
244
245        *entry = State::Done(alloc_id);
246
247        alloc_id
248    }
249}
250
251/// An allocation in the global (tcx-managed) memory can be either a function pointer,
252/// a static, or a "real" allocation with some data in it.
253#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for GlobalAlloc<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            GlobalAlloc::Function { instance: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Function", "instance", &__self_0),
            GlobalAlloc::VTable(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f, "VTable",
                    __self_0, &__self_1),
            GlobalAlloc::Static(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Static",
                    &__self_0),
            GlobalAlloc::Memory(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Memory",
                    &__self_0),
            GlobalAlloc::TypeId { ty: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "TypeId", "ty", &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for GlobalAlloc<'tcx> {
    #[inline]
    fn clone(&self) -> GlobalAlloc<'tcx> {
        match self {
            GlobalAlloc::Function { instance: __self_0 } =>
                GlobalAlloc::Function {
                    instance: ::core::clone::Clone::clone(__self_0),
                },
            GlobalAlloc::VTable(__self_0, __self_1) =>
                GlobalAlloc::VTable(::core::clone::Clone::clone(__self_0),
                    ::core::clone::Clone::clone(__self_1)),
            GlobalAlloc::Static(__self_0) =>
                GlobalAlloc::Static(::core::clone::Clone::clone(__self_0)),
            GlobalAlloc::Memory(__self_0) =>
                GlobalAlloc::Memory(::core::clone::Clone::clone(__self_0)),
            GlobalAlloc::TypeId { ty: __self_0 } =>
                GlobalAlloc::TypeId {
                    ty: ::core::clone::Clone::clone(__self_0),
                },
        }
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for GlobalAlloc<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Instance<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _:
                ::core::cmp::AssertParamIsEq<&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>>;
        let _: ::core::cmp::AssertParamIsEq<DefId>;
        let _: ::core::cmp::AssertParamIsEq<ConstAllocation<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for GlobalAlloc<'tcx> {
    #[inline]
    fn eq(&self, other: &GlobalAlloc<'tcx>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (GlobalAlloc::Function { instance: __self_0 },
                    GlobalAlloc::Function { instance: __arg1_0 }) =>
                    __self_0 == __arg1_0,
                (GlobalAlloc::VTable(__self_0, __self_1),
                    GlobalAlloc::VTable(__arg1_0, __arg1_1)) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (GlobalAlloc::Static(__self_0), GlobalAlloc::Static(__arg1_0))
                    => __self_0 == __arg1_0,
                (GlobalAlloc::Memory(__self_0), GlobalAlloc::Memory(__arg1_0))
                    => __self_0 == __arg1_0,
                (GlobalAlloc::TypeId { ty: __self_0 }, GlobalAlloc::TypeId {
                    ty: __arg1_0 }) => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for GlobalAlloc<'tcx> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state);
        match self {
            GlobalAlloc::Function { instance: __self_0 } =>
                ::core::hash::Hash::hash(__self_0, state),
            GlobalAlloc::VTable(__self_0, __self_1) => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state)
            }
            GlobalAlloc::Static(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            GlobalAlloc::Memory(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            GlobalAlloc::TypeId { ty: __self_0 } =>
                ::core::hash::Hash::hash(__self_0, state),
        }
    }
}Hash, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for GlobalAlloc<'tcx> {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => {
                        GlobalAlloc::Function {
                            instance: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    1usize => {
                        GlobalAlloc::VTable(::rustc_serialize::Decodable::decode(__decoder),
                            ::rustc_middle::ty::codec::RefDecodable::decode(__decoder))
                    }
                    2usize => {
                        GlobalAlloc::Static(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    3usize => {
                        GlobalAlloc::Memory(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    4usize => {
                        GlobalAlloc::TypeId {
                            ty: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `GlobalAlloc`, expected 0..5, actual {0}",
                                n));
                    }
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for GlobalAlloc<'tcx> {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        GlobalAlloc::Function { instance: ref __binding_0 } => {
                            0usize
                        }
                        GlobalAlloc::VTable(ref __binding_0, __binding_1) => {
                            1usize
                        }
                        GlobalAlloc::Static(ref __binding_0) => { 2usize }
                        GlobalAlloc::Memory(ref __binding_0) => { 3usize }
                        GlobalAlloc::TypeId { ty: ref __binding_0 } => { 4usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    GlobalAlloc::Function { instance: ref __binding_0 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    GlobalAlloc::VTable(ref __binding_0, __binding_1) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                    GlobalAlloc::Static(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    GlobalAlloc::Memory(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    GlobalAlloc::TypeId { ty: ref __binding_0 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, '__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for GlobalAlloc<'tcx> {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    GlobalAlloc::Function { instance: ref __binding_0 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                    GlobalAlloc::VTable(ref __binding_0, ref __binding_1) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                    GlobalAlloc::Static(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                    GlobalAlloc::Memory(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                    GlobalAlloc::TypeId { ty: ref __binding_0 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable)]
254pub enum GlobalAlloc<'tcx> {
255    /// The alloc ID is used as a function pointer.
256    Function { instance: Instance<'tcx> },
257    /// This alloc ID points to a symbolic (not-reified) vtable.
258    /// We remember the full dyn type, not just the principal trait, so that
259    /// const-eval and Miri can detect UB due to invalid transmutes of
260    /// `dyn Trait` types.
261    VTable(Ty<'tcx>, &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>),
262    /// The alloc ID points to a "lazy" static variable that did not get computed (yet).
263    /// This is also used to break the cycle in recursive statics.
264    Static(DefId),
265    /// The alloc ID points to memory.
266    Memory(ConstAllocation<'tcx>),
267    /// The first pointer-sized segment of a type id. On 64 bit systems, the 128 bit type id
268    /// is split into two segments, on 32 bit systems there are 4 segments, and so on.
269    TypeId { ty: Ty<'tcx> },
270}
271
272impl<'tcx> GlobalAlloc<'tcx> {
273    /// Panics if the `GlobalAlloc` does not refer to an `GlobalAlloc::Memory`
274    #[track_caller]
275    #[inline]
276    pub fn unwrap_memory(&self) -> ConstAllocation<'tcx> {
277        match *self {
278            GlobalAlloc::Memory(mem) => mem,
279            _ => crate::util::bug::bug_fmt(format_args!("expected memory, got {0:?}", self))bug!("expected memory, got {:?}", self),
280        }
281    }
282
283    /// Panics if the `GlobalAlloc` is not `GlobalAlloc::Function`
284    #[track_caller]
285    #[inline]
286    pub fn unwrap_fn(&self) -> Instance<'tcx> {
287        match *self {
288            GlobalAlloc::Function { instance, .. } => instance,
289            _ => crate::util::bug::bug_fmt(format_args!("expected function, got {0:?}", self))bug!("expected function, got {:?}", self),
290        }
291    }
292
293    /// Panics if the `GlobalAlloc` is not `GlobalAlloc::VTable`
294    #[track_caller]
295    #[inline]
296    pub fn unwrap_vtable(&self) -> (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
297        match *self {
298            GlobalAlloc::VTable(ty, dyn_ty) => (ty, dyn_ty.principal()),
299            _ => crate::util::bug::bug_fmt(format_args!("expected vtable, got {0:?}", self))bug!("expected vtable, got {:?}", self),
300        }
301    }
302
303    /// The address space that this `GlobalAlloc` should be placed in.
304    #[inline]
305    pub fn address_space(&self, cx: &impl HasDataLayout) -> AddressSpace {
306        match self {
307            GlobalAlloc::Function { .. } => cx.data_layout().instruction_address_space,
308            GlobalAlloc::TypeId { .. }
309            | GlobalAlloc::Static(..)
310            | GlobalAlloc::Memory(..)
311            | GlobalAlloc::VTable(..) => AddressSpace::ZERO,
312        }
313    }
314
315    pub fn mutability(&self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Mutability {
316        // Let's see what kind of memory we are.
317        match *self {
318            GlobalAlloc::Static(did) => {
319                let DefKind::Static { safety: _, mutability, nested } = tcx.def_kind(did) else {
320                    crate::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
321                };
322                if nested {
323                    // Nested statics in a `static` are never interior mutable,
324                    // so just use the declared mutability.
325                    if truecfg!(debug_assertions) {
326                        let alloc = tcx.eval_static_initializer(did).unwrap();
327                        match (&alloc.0.mutability, &mutability) {
    (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!(alloc.0.mutability, mutability);
328                    }
329                    mutability
330                } else {
331                    let mutability = match mutability {
332                        Mutability::Not
333                            if !tcx
334                                .type_of(did)
335                                .no_bound_vars()
336                                .expect("statics should not have generic parameters")
337                                .is_freeze(tcx, typing_env) =>
338                        {
339                            Mutability::Mut
340                        }
341                        _ => mutability,
342                    };
343                    mutability
344                }
345            }
346            GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
347            GlobalAlloc::TypeId { .. } | GlobalAlloc::Function { .. } | GlobalAlloc::VTable(..) => {
348                // These are immutable.
349                Mutability::Not
350            }
351        }
352    }
353
354    pub fn size_and_align(
355        &self,
356        tcx: TyCtxt<'tcx>,
357        typing_env: ty::TypingEnv<'tcx>,
358    ) -> (Size, Align) {
359        match *self {
360            GlobalAlloc::Static(def_id) => {
361                let DefKind::Static { nested, .. } = tcx.def_kind(def_id) else {
362                    crate::util::bug::bug_fmt(format_args!("GlobalAlloc::Static is not a static"))bug!("GlobalAlloc::Static is not a static")
363                };
364
365                if nested {
366                    // Nested anonymous statics are untyped, so let's get their
367                    // size and alignment from the allocation itself. This always
368                    // succeeds, as the query is fed at DefId creation time, so no
369                    // evaluation actually occurs.
370                    let alloc = tcx.eval_static_initializer(def_id).unwrap();
371                    (alloc.0.size(), alloc.0.align)
372                } else {
373                    // Use size and align of the type for everything else. We need
374                    // to do that to
375                    // * avoid cycle errors in case of self-referential statics,
376                    // * be able to get information on extern statics.
377                    let ty = tcx
378                        .type_of(def_id)
379                        .no_bound_vars()
380                        .expect("statics should not have generic parameters");
381                    let layout = tcx.layout_of(typing_env.as_query_input(ty)).unwrap();
382                    if !layout.is_sized() {
    ::core::panicking::panic("assertion failed: layout.is_sized()")
};assert!(layout.is_sized());
383
384                    // Take over-alignment from attributes into account.
385                    let align = match tcx.codegen_fn_attrs(def_id).alignment {
386                        Some(align_from_attribute) => {
387                            Ord::max(align_from_attribute, layout.align.abi)
388                        }
389                        None => layout.align.abi,
390                    };
391
392                    (layout.size, align)
393                }
394            }
395            GlobalAlloc::Memory(alloc) => {
396                let alloc = alloc.inner();
397                (alloc.size(), alloc.align)
398            }
399            GlobalAlloc::Function { .. } => (Size::ZERO, Align::ONE),
400            GlobalAlloc::VTable(..) => {
401                // No data to be accessed here. But vtables are pointer-aligned.
402                (Size::ZERO, tcx.data_layout.pointer_align().abi)
403            }
404            // Fake allocation, there's nothing to access here.
405            GlobalAlloc::TypeId { .. } => (Size::ZERO, Align::ONE),
406        }
407    }
408}
409
410pub const CTFE_ALLOC_SALT: usize = 0;
411
412pub(crate) struct AllocMap<'tcx> {
413    /// Maps `AllocId`s to their corresponding allocations.
414    // Note that this map on rustc workloads seems to be rather dense, but in miri workloads should
415    // be pretty sparse. In #136105 we considered replacing it with a (dense) Vec-based map, but
416    // since there are workloads where it can be sparse we decided to go with sharding for now. At
417    // least up to 32 cores the one workload tested didn't exhibit much difference between the two.
418    //
419    // Should be locked *after* locking dedup if locking both to avoid deadlocks.
420    to_alloc: ShardedHashMap<AllocId, GlobalAlloc<'tcx>>,
421
422    /// Used to deduplicate global allocations: functions, vtables, string literals, ...
423    ///
424    /// The `usize` is a "salt" used by Miri to make deduplication imperfect, thus better emulating
425    /// the actual guarantees.
426    dedup: Lock<FxHashMap<(GlobalAlloc<'tcx>, usize), AllocId>>,
427
428    /// The `AllocId` to assign to the next requested ID.
429    /// Always incremented; never gets smaller.
430    next_id: AtomicU64,
431}
432
433impl<'tcx> AllocMap<'tcx> {
434    pub(crate) fn new() -> Self {
435        AllocMap {
436            to_alloc: Default::default(),
437            dedup: Default::default(),
438            next_id: AtomicU64::new(1),
439        }
440    }
441    fn reserve(&self) -> AllocId {
442        // Technically there is a window here where we overflow and then another thread
443        // increments `next_id` *again* and uses it before we panic and tear down the entire session.
444        // We consider this fine since such overflows cannot realistically occur.
445        let next_id = self.next_id.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
446        AllocId(NonZero::new(next_id).unwrap())
447    }
448}
449
450impl<'tcx> TyCtxt<'tcx> {
451    /// Obtains a new allocation ID that can be referenced but does not
452    /// yet have an allocation backing it.
453    ///
454    /// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
455    /// an `AllocId` from a query.
456    pub fn reserve_alloc_id(self) -> AllocId {
457        self.alloc_map.reserve()
458    }
459
460    /// Reserves a new ID *if* this allocation has not been dedup-reserved before.
461    /// Should not be used for mutable memory.
462    fn reserve_and_set_dedup(self, alloc: GlobalAlloc<'tcx>, salt: usize) -> AllocId {
463        if let GlobalAlloc::Memory(mem) = alloc {
464            if mem.inner().mutability.is_mut() {
465                crate::util::bug::bug_fmt(format_args!("trying to dedup-reserve mutable memory"));bug!("trying to dedup-reserve mutable memory");
466            }
467        }
468        let alloc_salt = (alloc, salt);
469        // Locking this *before* `to_alloc` also to ensure correct lock order.
470        let mut dedup = self.alloc_map.dedup.lock();
471        if let Some(&alloc_id) = dedup.get(&alloc_salt) {
472            return alloc_id;
473        }
474        let id = self.alloc_map.reserve();
475        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/mir/interpret/mod.rs:475",
                        "rustc_middle::mir::interpret", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/mir/interpret/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(475u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::mir::interpret"),
                        ::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!("creating alloc {0:?} with id {1:?}",
                                                    alloc_salt.0, id) as &dyn Value))])
            });
    } else { ; }
};debug!("creating alloc {:?} with id {id:?}", alloc_salt.0);
476        let had_previous = self.alloc_map.to_alloc.insert(id, alloc_salt.0.clone()).is_some();
477        // We just reserved, so should always be unique.
478        if !!had_previous {
    ::core::panicking::panic("assertion failed: !had_previous")
};assert!(!had_previous);
479        dedup.insert(alloc_salt, id);
480        id
481    }
482
483    /// Generates an `AllocId` for a memory allocation. If the exact same memory has been
484    /// allocated before, this will return the same `AllocId`.
485    pub fn reserve_and_set_memory_dedup(self, mem: ConstAllocation<'tcx>, salt: usize) -> AllocId {
486        self.reserve_and_set_dedup(GlobalAlloc::Memory(mem), salt)
487    }
488
489    /// Generates an `AllocId` for a static or return a cached one in case this function has been
490    /// called on the same static before.
491    pub fn reserve_and_set_static_alloc(self, static_id: DefId) -> AllocId {
492        let salt = 0; // Statics have a guaranteed unique address, no salt added.
493        self.reserve_and_set_dedup(GlobalAlloc::Static(static_id), salt)
494    }
495
496    /// Generates an `AllocId` for a function. Will get deduplicated.
497    pub fn reserve_and_set_fn_alloc(self, instance: Instance<'tcx>, salt: usize) -> AllocId {
498        self.reserve_and_set_dedup(GlobalAlloc::Function { instance }, salt)
499    }
500
501    /// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated.
502    pub fn reserve_and_set_vtable_alloc(
503        self,
504        ty: Ty<'tcx>,
505        dyn_ty: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
506        salt: usize,
507    ) -> AllocId {
508        self.reserve_and_set_dedup(GlobalAlloc::VTable(ty, dyn_ty), salt)
509    }
510
511    /// Generates an [AllocId] for a [core::any::TypeId]. Will get deduplicated.
512    pub fn reserve_and_set_type_id_alloc(self, ty: Ty<'tcx>) -> AllocId {
513        self.reserve_and_set_dedup(GlobalAlloc::TypeId { ty }, 0)
514    }
515
516    /// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
517    /// `Allocation` with a different `AllocId`.
518    /// Statics with identical content will still point to the same `Allocation`, i.e.,
519    /// their data will be deduplicated through `Allocation` interning -- but they
520    /// are different places in memory and as such need different IDs.
521    pub fn reserve_and_set_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
522        let id = self.reserve_alloc_id();
523        self.set_alloc_id_memory(id, mem);
524        id
525    }
526
527    /// Returns `None` in case the `AllocId` is dangling. An `InterpretCx` can still have a
528    /// local `Allocation` for that `AllocId`, but having such an `AllocId` in a constant is
529    /// illegal and will likely ICE.
530    /// This function exists to allow const eval to detect the difference between evaluation-
531    /// local dangling pointers and allocations in constants/statics.
532    #[inline]
533    pub fn try_get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
534        self.alloc_map.to_alloc.get(&id)
535    }
536
537    #[inline]
538    #[track_caller]
539    /// Panics in case the `AllocId` is dangling. Since that is impossible for `AllocId`s in
540    /// constants (as all constants must pass interning and validation that check for dangling
541    /// ids), this function is frequently used throughout rustc, but should not be used within
542    /// the interpreter.
543    pub fn global_alloc(self, id: AllocId) -> GlobalAlloc<'tcx> {
544        match self.try_get_global_alloc(id) {
545            Some(alloc) => alloc,
546            None => crate::util::bug::bug_fmt(format_args!("could not find allocation for {0:?}",
        id))bug!("could not find allocation for {id:?}"),
547        }
548    }
549
550    /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
551    /// call this function twice, even with the same `Allocation` will ICE the compiler.
552    pub fn set_alloc_id_memory(self, id: AllocId, mem: ConstAllocation<'tcx>) {
553        if let Some(old) = self.alloc_map.to_alloc.insert(id, GlobalAlloc::Memory(mem)) {
554            crate::util::bug::bug_fmt(format_args!("tried to set allocation ID {0:?}, but it was already existing as {1:#?}",
        id, old));bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
555        }
556    }
557
558    /// Freezes an `AllocId` created with `reserve` by pointing it at a static item. Trying to
559    /// call this function twice, even with the same `DefId` will ICE the compiler.
560    pub fn set_nested_alloc_id_static(self, id: AllocId, def_id: LocalDefId) {
561        if let Some(old) =
562            self.alloc_map.to_alloc.insert(id, GlobalAlloc::Static(def_id.to_def_id()))
563        {
564            crate::util::bug::bug_fmt(format_args!("tried to set allocation ID {0:?}, but it was already existing as {1:#?}",
        id, old));bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
565        }
566    }
567}
568
569////////////////////////////////////////////////////////////////////////////////
570// Methods to access integers in the target endianness
571////////////////////////////////////////////////////////////////////////////////
572
573#[inline]
574pub fn write_target_uint(
575    endianness: Endian,
576    mut target: &mut [u8],
577    data: u128,
578) -> Result<(), io::Error> {
579    // This u128 holds an "any-size uint" (since smaller uints can fits in it)
580    // So we do not write all bytes of the u128, just the "payload".
581    match endianness {
582        Endian::Little => target.write(&data.to_le_bytes())?,
583        Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
584    };
585    if true {
    if !(target.len() == 0) {
        ::core::panicking::panic("assertion failed: target.len() == 0")
    };
};debug_assert!(target.len() == 0); // We should have filled the target buffer.
586    Ok(())
587}
588
589#[inline]
590pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
591    // This u128 holds an "any-size uint" (since smaller uints can fits in it)
592    let mut buf = [0u8; size_of::<u128>()];
593    // So we do not read exactly 16 bytes into the u128, just the "payload".
594    let uint = match endianness {
595        Endian::Little => {
596            source.read_exact(&mut buf[..source.len()])?;
597            Ok(u128::from_le_bytes(buf))
598        }
599        Endian::Big => {
600            source.read_exact(&mut buf[16 - source.len()..])?;
601            Ok(u128::from_be_bytes(buf))
602        }
603    };
604    if true {
    if !(source.len() == 0) {
        ::core::panicking::panic("assertion failed: source.len() == 0")
    };
};debug_assert!(source.len() == 0); // We should have consumed the source buffer.
605    uint
606}