Skip to main content

rustc_transmute/
lib.rs

1// tidy-alphabetical-start
2#![cfg_attr(test, feature(test))]
3#![feature(never_type)]
4#![feature(option_into_flat_iter)]
5// tidy-alphabetical-end
6
7pub(crate) use rustc_data_structures::fx::{FxIndexMap as Map, FxIndexSet as Set};
8
9pub mod layout;
10mod maybe_transmutable;
11
12#[derive(#[automatically_derived]
impl ::core::marker::Copy for Assume { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Assume {
    #[inline]
    fn clone(&self) -> Assume {
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Assume {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f, "Assume",
            "alignment", &self.alignment, "lifetimes", &self.lifetimes,
            "safety", &self.safety, "validity", &&self.validity)
    }
}Debug, #[automatically_derived]
impl ::core::default::Default for Assume {
    #[inline]
    fn default() -> Assume {
        Assume {
            alignment: ::core::default::Default::default(),
            lifetimes: ::core::default::Default::default(),
            safety: ::core::default::Default::default(),
            validity: ::core::default::Default::default(),
        }
    }
}Default)]
13pub struct Assume {
14    pub alignment: bool,
15    pub lifetimes: bool,
16    pub safety: bool,
17    pub validity: bool,
18}
19
20/// Either transmutation is allowed, we have an error, or we have an optional
21/// Condition that must hold.
22#[derive(#[automatically_derived]
impl<R: ::core::fmt::Debug, T: ::core::fmt::Debug> ::core::fmt::Debug for
    Answer<R, T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Answer::Yes => ::core::fmt::Formatter::write_str(f, "Yes"),
            Answer::No(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "No",
                    &__self_0),
            Answer::If(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "If",
                    &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl<R: ::core::hash::Hash, T: ::core::hash::Hash> ::core::hash::Hash for
    Answer<R, T> {
    #[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 {
            Answer::No(__self_0) => ::core::hash::Hash::hash(__self_0, state),
            Answer::If(__self_0) => ::core::hash::Hash::hash(__self_0, state),
            _ => {}
        }
    }
}Hash, #[automatically_derived]
impl<R: ::core::cmp::Eq, T: ::core::cmp::Eq> ::core::cmp::Eq for Answer<R, T>
    {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Reason<T>>;
        let _: ::core::cmp::AssertParamIsEq<Condition<R, T>>;
    }
}Eq, #[automatically_derived]
impl<R: ::core::cmp::PartialEq, T: ::core::cmp::PartialEq>
    ::core::cmp::PartialEq for Answer<R, T> {
    #[inline]
    fn eq(&self, other: &Answer<R, T>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Answer::No(__self_0), Answer::No(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (Answer::If(__self_0), Answer::If(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl<R: ::core::clone::Clone, T: ::core::clone::Clone> ::core::clone::Clone
    for Answer<R, T> {
    #[inline]
    fn clone(&self) -> Answer<R, T> {
        match self {
            Answer::Yes => Answer::Yes,
            Answer::No(__self_0) =>
                Answer::No(::core::clone::Clone::clone(__self_0)),
            Answer::If(__self_0) =>
                Answer::If(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone)]
23pub enum Answer<R, T> {
24    Yes,
25    No(Reason<T>),
26    If(Condition<R, T>),
27}
28
29/// A condition which must hold for safe transmutation to be possible.
30#[derive(#[automatically_derived]
impl<R: ::core::fmt::Debug, T: ::core::fmt::Debug> ::core::fmt::Debug for
    Condition<R, T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Condition::Transmutable { src: __self_0, dst: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "Transmutable", "src", __self_0, "dst", &__self_1),
            Condition::Outlives { long: __self_0, short: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "Outlives", "long", __self_0, "short", &__self_1),
            Condition::Immutable { ty: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Immutable", "ty", &__self_0),
            Condition::IfAll(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "IfAll",
                    &__self_0),
            Condition::IfAny(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "IfAny",
                    &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl<R: ::core::hash::Hash, T: ::core::hash::Hash> ::core::hash::Hash for
    Condition<R, T> {
    #[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 {
            Condition::Transmutable { src: __self_0, dst: __self_1 } => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state)
            }
            Condition::Outlives { long: __self_0, short: __self_1 } => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state)
            }
            Condition::Immutable { ty: __self_0 } =>
                ::core::hash::Hash::hash(__self_0, state),
            Condition::IfAll(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            Condition::IfAny(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
        }
    }
}Hash, #[automatically_derived]
impl<R: ::core::cmp::Eq, T: ::core::cmp::Eq> ::core::cmp::Eq for
    Condition<R, T> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<T>;
        let _: ::core::cmp::AssertParamIsEq<R>;
        let _: ::core::cmp::AssertParamIsEq<Vec<Condition<R, T>>>;
        let _: ::core::cmp::AssertParamIsEq<Vec<Condition<R, T>>>;
    }
}Eq, #[automatically_derived]
impl<R: ::core::cmp::PartialEq, T: ::core::cmp::PartialEq>
    ::core::cmp::PartialEq for Condition<R, T> {
    #[inline]
    fn eq(&self, other: &Condition<R, T>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Condition::Transmutable { src: __self_0, dst: __self_1 },
                    Condition::Transmutable { src: __arg1_0, dst: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (Condition::Outlives { long: __self_0, short: __self_1 },
                    Condition::Outlives { long: __arg1_0, short: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (Condition::Immutable { ty: __self_0 }, Condition::Immutable {
                    ty: __arg1_0 }) => __self_0 == __arg1_0,
                (Condition::IfAll(__self_0), Condition::IfAll(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (Condition::IfAny(__self_0), Condition::IfAny(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<R: ::core::clone::Clone, T: ::core::clone::Clone> ::core::clone::Clone
    for Condition<R, T> {
    #[inline]
    fn clone(&self) -> Condition<R, T> {
        match self {
            Condition::Transmutable { src: __self_0, dst: __self_1 } =>
                Condition::Transmutable {
                    src: ::core::clone::Clone::clone(__self_0),
                    dst: ::core::clone::Clone::clone(__self_1),
                },
            Condition::Outlives { long: __self_0, short: __self_1 } =>
                Condition::Outlives {
                    long: ::core::clone::Clone::clone(__self_0),
                    short: ::core::clone::Clone::clone(__self_1),
                },
            Condition::Immutable { ty: __self_0 } =>
                Condition::Immutable {
                    ty: ::core::clone::Clone::clone(__self_0),
                },
            Condition::IfAll(__self_0) =>
                Condition::IfAll(::core::clone::Clone::clone(__self_0)),
            Condition::IfAny(__self_0) =>
                Condition::IfAny(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone)]
31pub enum Condition<R, T> {
32    /// `Src` is transmutable into `Dst`, if `src` is transmutable into `dst`.
33    Transmutable { src: T, dst: T },
34
35    /// The region `long` must outlive `short`.
36    Outlives { long: R, short: R },
37
38    /// The `ty` is immutable.
39    Immutable { ty: T },
40
41    /// `Src` is transmutable into `Dst`, if all of the enclosed requirements are met.
42    IfAll(Vec<Condition<R, T>>),
43
44    /// `Src` is transmutable into `Dst` if any of the enclosed requirements are met.
45    IfAny(Vec<Condition<R, T>>),
46}
47
48/// Answers "why wasn't the source type transmutable into the destination type?"
49#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for Reason<T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Reason::SrcIsNotYetSupported =>
                ::core::fmt::Formatter::write_str(f, "SrcIsNotYetSupported"),
            Reason::DstIsNotYetSupported =>
                ::core::fmt::Formatter::write_str(f, "DstIsNotYetSupported"),
            Reason::DstIsBitIncompatible =>
                ::core::fmt::Formatter::write_str(f, "DstIsBitIncompatible"),
            Reason::DstUninhabited =>
                ::core::fmt::Formatter::write_str(f, "DstUninhabited"),
            Reason::DstMayHaveSafetyInvariants =>
                ::core::fmt::Formatter::write_str(f,
                    "DstMayHaveSafetyInvariants"),
            Reason::DstIsTooBig =>
                ::core::fmt::Formatter::write_str(f, "DstIsTooBig"),
            Reason::DstRefIsTooBig {
                src: __self_0,
                src_size: __self_1,
                dst: __self_2,
                dst_size: __self_3 } =>
                ::core::fmt::Formatter::debug_struct_field4_finish(f,
                    "DstRefIsTooBig", "src", __self_0, "src_size", __self_1,
                    "dst", __self_2, "dst_size", &__self_3),
            Reason::DstHasStricterAlignment {
                src_min_align: __self_0, dst_min_align: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "DstHasStricterAlignment", "src_min_align", __self_0,
                    "dst_min_align", &__self_1),
            Reason::DstIsMoreUnique =>
                ::core::fmt::Formatter::write_str(f, "DstIsMoreUnique"),
            Reason::TypeError =>
                ::core::fmt::Formatter::write_str(f, "TypeError"),
            Reason::SrcLayoutUnknown =>
                ::core::fmt::Formatter::write_str(f, "SrcLayoutUnknown"),
            Reason::DstLayoutUnknown =>
                ::core::fmt::Formatter::write_str(f, "DstLayoutUnknown"),
            Reason::SrcSizeOverflow =>
                ::core::fmt::Formatter::write_str(f, "SrcSizeOverflow"),
            Reason::DstSizeOverflow =>
                ::core::fmt::Formatter::write_str(f, "DstSizeOverflow"),
        }
    }
}Debug, #[automatically_derived]
impl<T: ::core::hash::Hash> ::core::hash::Hash for Reason<T> {
    #[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 {
            Reason::DstRefIsTooBig {
                src: __self_0,
                src_size: __self_1,
                dst: __self_2,
                dst_size: __self_3 } => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state);
                ::core::hash::Hash::hash(__self_2, state);
                ::core::hash::Hash::hash(__self_3, state)
            }
            Reason::DstHasStricterAlignment {
                src_min_align: __self_0, dst_min_align: __self_1 } => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state)
            }
            _ => {}
        }
    }
}Hash, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for Reason<T> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<T>;
        let _: ::core::cmp::AssertParamIsEq<usize>;
    }
}Eq, #[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for Reason<T> {
    #[inline]
    fn eq(&self, other: &Reason<T>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Reason::DstRefIsTooBig {
                    src: __self_0,
                    src_size: __self_1,
                    dst: __self_2,
                    dst_size: __self_3 }, Reason::DstRefIsTooBig {
                    src: __arg1_0,
                    src_size: __arg1_1,
                    dst: __arg1_2,
                    dst_size: __arg1_3 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                            __self_2 == __arg1_2 && __self_3 == __arg1_3,
                (Reason::DstHasStricterAlignment {
                    src_min_align: __self_0, dst_min_align: __self_1 },
                    Reason::DstHasStricterAlignment {
                    src_min_align: __arg1_0, dst_min_align: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl<T: ::core::cmp::PartialOrd> ::core::cmp::PartialOrd for Reason<T> {
    #[inline]
    fn partial_cmp(&self, other: &Reason<T>)
        -> ::core::option::Option<::core::cmp::Ordering> {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        match (self, other) {
            (Reason::DstRefIsTooBig {
                src: __self_0,
                src_size: __self_1,
                dst: __self_2,
                dst_size: __self_3 }, Reason::DstRefIsTooBig {
                src: __arg1_0,
                src_size: __arg1_1,
                dst: __arg1_2,
                dst_size: __arg1_3 }) =>
                match ::core::cmp::PartialOrd::partial_cmp(__self_0, __arg1_0)
                    {
                    ::core::option::Option::Some(::core::cmp::Ordering::Equal)
                        =>
                        match ::core::cmp::PartialOrd::partial_cmp(__self_1,
                                __arg1_1) {
                            ::core::option::Option::Some(::core::cmp::Ordering::Equal)
                                =>
                                match ::core::cmp::PartialOrd::partial_cmp(__self_2,
                                        __arg1_2) {
                                    ::core::option::Option::Some(::core::cmp::Ordering::Equal)
                                        => ::core::cmp::PartialOrd::partial_cmp(__self_3, __arg1_3),
                                    cmp => cmp,
                                },
                            cmp => cmp,
                        },
                    cmp => cmp,
                },
            (Reason::DstHasStricterAlignment {
                src_min_align: __self_0, dst_min_align: __self_1 },
                Reason::DstHasStricterAlignment {
                src_min_align: __arg1_0, dst_min_align: __arg1_1 }) =>
                match ::core::cmp::PartialOrd::partial_cmp(__self_0, __arg1_0)
                    {
                    ::core::option::Option::Some(::core::cmp::Ordering::Equal)
                        => ::core::cmp::PartialOrd::partial_cmp(__self_1, __arg1_1),
                    cmp => cmp,
                },
            _ =>
                ::core::cmp::PartialOrd::partial_cmp(&__self_discr,
                    &__arg1_discr),
        }
    }
}PartialOrd, #[automatically_derived]
impl<T: ::core::cmp::Ord> ::core::cmp::Ord for Reason<T> {
    #[inline]
    fn cmp(&self, other: &Reason<T>) -> ::core::cmp::Ordering {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        match ::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr) {
            ::core::cmp::Ordering::Equal =>
                match (self, other) {
                    (Reason::DstRefIsTooBig {
                        src: __self_0,
                        src_size: __self_1,
                        dst: __self_2,
                        dst_size: __self_3 }, Reason::DstRefIsTooBig {
                        src: __arg1_0,
                        src_size: __arg1_1,
                        dst: __arg1_2,
                        dst_size: __arg1_3 }) =>
                        match ::core::cmp::Ord::cmp(__self_0, __arg1_0) {
                            ::core::cmp::Ordering::Equal =>
                                match ::core::cmp::Ord::cmp(__self_1, __arg1_1) {
                                    ::core::cmp::Ordering::Equal =>
                                        match ::core::cmp::Ord::cmp(__self_2, __arg1_2) {
                                            ::core::cmp::Ordering::Equal =>
                                                ::core::cmp::Ord::cmp(__self_3, __arg1_3),
                                            cmp => cmp,
                                        },
                                    cmp => cmp,
                                },
                            cmp => cmp,
                        },
                    (Reason::DstHasStricterAlignment {
                        src_min_align: __self_0, dst_min_align: __self_1 },
                        Reason::DstHasStricterAlignment {
                        src_min_align: __arg1_0, dst_min_align: __arg1_1 }) =>
                        match ::core::cmp::Ord::cmp(__self_0, __arg1_0) {
                            ::core::cmp::Ordering::Equal =>
                                ::core::cmp::Ord::cmp(__self_1, __arg1_1),
                            cmp => cmp,
                        },
                    _ => ::core::cmp::Ordering::Equal,
                },
            cmp => cmp,
        }
    }
}Ord, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for Reason<T> {
    #[inline]
    fn clone(&self) -> Reason<T> {
        match self {
            Reason::SrcIsNotYetSupported => Reason::SrcIsNotYetSupported,
            Reason::DstIsNotYetSupported => Reason::DstIsNotYetSupported,
            Reason::DstIsBitIncompatible => Reason::DstIsBitIncompatible,
            Reason::DstUninhabited => Reason::DstUninhabited,
            Reason::DstMayHaveSafetyInvariants =>
                Reason::DstMayHaveSafetyInvariants,
            Reason::DstIsTooBig => Reason::DstIsTooBig,
            Reason::DstRefIsTooBig {
                src: __self_0,
                src_size: __self_1,
                dst: __self_2,
                dst_size: __self_3 } =>
                Reason::DstRefIsTooBig {
                    src: ::core::clone::Clone::clone(__self_0),
                    src_size: ::core::clone::Clone::clone(__self_1),
                    dst: ::core::clone::Clone::clone(__self_2),
                    dst_size: ::core::clone::Clone::clone(__self_3),
                },
            Reason::DstHasStricterAlignment {
                src_min_align: __self_0, dst_min_align: __self_1 } =>
                Reason::DstHasStricterAlignment {
                    src_min_align: ::core::clone::Clone::clone(__self_0),
                    dst_min_align: ::core::clone::Clone::clone(__self_1),
                },
            Reason::DstIsMoreUnique => Reason::DstIsMoreUnique,
            Reason::TypeError => Reason::TypeError,
            Reason::SrcLayoutUnknown => Reason::SrcLayoutUnknown,
            Reason::DstLayoutUnknown => Reason::DstLayoutUnknown,
            Reason::SrcSizeOverflow => Reason::SrcSizeOverflow,
            Reason::DstSizeOverflow => Reason::DstSizeOverflow,
        }
    }
}Clone)]
50pub enum Reason<T> {
51    /// The layout of the source type is not yet supported.
52    SrcIsNotYetSupported,
53    /// The layout of the destination type is not yet supported.
54    DstIsNotYetSupported,
55    /// The layout of the destination type is bit-incompatible with the source type.
56    DstIsBitIncompatible,
57    /// The destination type is uninhabited.
58    DstUninhabited,
59    /// The destination type may carry safety invariants.
60    DstMayHaveSafetyInvariants,
61    /// `Dst` is larger than `Src`, and the excess bytes were not exclusively uninitialized.
62    DstIsTooBig,
63    /// `Dst` is larger `Src`.
64    DstRefIsTooBig {
65        /// The referent of the source type.
66        src: T,
67        /// The size of the source type's referent.
68        src_size: usize,
69        /// The too-large referent of the destination type.
70        dst: T,
71        /// The size of the destination type's referent.
72        dst_size: usize,
73    },
74    /// Src should have a stricter alignment than Dst, but it does not.
75    DstHasStricterAlignment { src_min_align: usize, dst_min_align: usize },
76    /// Can't go from shared pointer to unique pointer
77    DstIsMoreUnique,
78    /// Encountered a type error
79    TypeError,
80    /// The layout of src is unknown
81    SrcLayoutUnknown,
82    /// The layout of dst is unknown
83    DstLayoutUnknown,
84    /// The size of src is overflow
85    SrcSizeOverflow,
86    /// The size of dst is overflow
87    DstSizeOverflow,
88}
89
90#[cfg(feature = "rustc")]
91mod rustc {
92    use rustc_hir::lang_items::LangItem;
93    use rustc_middle::ty::{Const, Region, Ty, TyCtxt};
94
95    use super::*;
96
97    pub struct TransmuteTypeEnv<'tcx> {
98        tcx: TyCtxt<'tcx>,
99    }
100
101    impl<'tcx> TransmuteTypeEnv<'tcx> {
102        pub fn new(tcx: TyCtxt<'tcx>) -> Self {
103            Self { tcx }
104        }
105
106        pub fn is_transmutable(
107            &mut self,
108            src: Ty<'tcx>,
109            dst: Ty<'tcx>,
110            assume: crate::Assume,
111        ) -> crate::Answer<Region<'tcx>, Ty<'tcx>> {
112            crate::maybe_transmutable::MaybeTransmutableQuery::new(src, dst, assume, self.tcx)
113                .answer()
114        }
115    }
116
117    impl Assume {
118        /// Constructs an `Assume` from a given const-`Assume`.
119        pub fn from_const<'tcx>(tcx: TyCtxt<'tcx>, ct: Const<'tcx>) -> Option<Self> {
120            use rustc_middle::ty::ScalarInt;
121            use rustc_span::sym;
122
123            let cv = ct.try_to_value()?;
124            let adt_def = cv.ty.ty_adt_def()?;
125
126            if !tcx.is_lang_item(adt_def.did(), LangItem::TransmuteOpts) {
127                tcx.dcx().delayed_bug(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("The given `const` was not marked with the `{0}` lang item.",
                LangItem::TransmuteOpts.name()))
    })format!(
128                    "The given `const` was not marked with the `{}` lang item.",
129                    LangItem::TransmuteOpts.name()
130                ));
131                return Some(Self {
132                    alignment: true,
133                    lifetimes: true,
134                    safety: true,
135                    validity: true,
136                });
137            }
138
139            let variant = adt_def.non_enum_variant();
140            let fields = cv.to_branch();
141
142            let get_field = |name| {
143                let (field_idx, _) = variant
144                    .fields
145                    .iter()
146                    .enumerate()
147                    .find(|(_, field_def)| name == field_def.name)
148                    .unwrap_or_else(|| {
    ::core::panicking::panic_fmt(format_args!("There were no fields named `{0}`.",
            name));
}panic!("There were no fields named `{name}`."));
149                fields[field_idx].try_to_leaf().map(|leaf| leaf == ScalarInt::TRUE)
150            };
151
152            Some(Self {
153                alignment: get_field(sym::alignment)?,
154                lifetimes: get_field(sym::lifetimes)?,
155                safety: get_field(sym::safety)?,
156                validity: get_field(sym::validity)?,
157            })
158        }
159    }
160}
161
162#[cfg(feature = "rustc")]
163pub use rustc::*;