Skip to main content

rustc_transmute/maybe_transmutable/
mod.rs

1use tracing::{debug, instrument, trace};
2
3pub(crate) mod query_context;
4#[cfg(test)]
5mod tests;
6
7use rustc_middle::ty::transmute::{Answer, Condition, Reason};
8
9use crate::Map;
10use crate::layout::{self, Def, Dfa, Reference, Tree, dfa, union};
11use crate::maybe_transmutable::query_context::QueryContext;
12
13pub(crate) struct MaybeTransmutableQuery<L, C>
14where
15    C: QueryContext,
16{
17    src: L,
18    dst: L,
19    assume: crate::Assume,
20    context: C,
21}
22
23impl<L, C> MaybeTransmutableQuery<L, C>
24where
25    C: QueryContext,
26{
27    pub(crate) fn new(src: L, dst: L, assume: crate::Assume, context: C) -> Self {
28        Self { src, dst, assume, context }
29    }
30}
31
32#[cfg(feature = "rustc")]
33mod rustc {
34    use rustc_middle::ty::layout::LayoutCx;
35    use rustc_middle::ty::{Ty, TyCtxt, TypingEnv};
36
37    use super::*;
38    use crate::layout::tree::rustc::Err;
39
40    impl<'tcx> MaybeTransmutableQuery<Ty<'tcx>, TyCtxt<'tcx>> {
41        /// This method begins by converting `src` and `dst` from `Ty`s to `Tree`s,
42        /// then computes an answer using those trees.
43        {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("answer",
                                    "rustc_transmute::maybe_transmutable::rustc",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(43u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable::rustc"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("src")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("src");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("dst")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("dst");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self.src)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self.dst)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Answer<<TyCtxt<'tcx> as QueryContext>::Region,
                    <TyCtxt<'tcx> as QueryContext>::Type> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let Self { src, dst, assume, context } = self;
            let layout_cx =
                LayoutCx::new(context, TypingEnv::fully_monomorphized());
            let src = Tree::from_ty(src, layout_cx);
            let dst = Tree::from_ty(dst, layout_cx);
            match (src, dst) {
                (Err(Err::TypeError(_)), _) | (_, Err(Err::TypeError(_))) => {
                    Answer::No(Reason::TypeError)
                }
                (Err(Err::UnknownLayout), _) =>
                    Answer::No(Reason::SrcLayoutUnknown),
                (_, Err(Err::UnknownLayout)) =>
                    Answer::No(Reason::DstLayoutUnknown),
                (Err(Err::NotYetSupported), _) =>
                    Answer::No(Reason::SrcIsNotYetSupported),
                (_, Err(Err::NotYetSupported)) =>
                    Answer::No(Reason::DstIsNotYetSupported),
                (Err(Err::SizeOverflow), _) =>
                    Answer::No(Reason::SrcSizeOverflow),
                (_, Err(Err::SizeOverflow)) =>
                    Answer::No(Reason::DstSizeOverflow),
                (Ok(src), Ok(dst)) =>
                    MaybeTransmutableQuery {
                            src,
                            dst,
                            assume,
                            context,
                        }.answer(),
            }
        }
    }
}#[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))]
44        pub(crate) fn answer(
45            self,
46        ) -> Answer<<TyCtxt<'tcx> as QueryContext>::Region, <TyCtxt<'tcx> as QueryContext>::Type>
47        {
48            let Self { src, dst, assume, context } = self;
49
50            let layout_cx = LayoutCx::new(context, TypingEnv::fully_monomorphized());
51
52            // Convert `src` and `dst` from their rustc representations, to `Tree`-based
53            // representations.
54            let src = Tree::from_ty(src, layout_cx);
55            let dst = Tree::from_ty(dst, layout_cx);
56
57            match (src, dst) {
58                (Err(Err::TypeError(_)), _) | (_, Err(Err::TypeError(_))) => {
59                    Answer::No(Reason::TypeError)
60                }
61                (Err(Err::UnknownLayout), _) => Answer::No(Reason::SrcLayoutUnknown),
62                (_, Err(Err::UnknownLayout)) => Answer::No(Reason::DstLayoutUnknown),
63                (Err(Err::NotYetSupported), _) => Answer::No(Reason::SrcIsNotYetSupported),
64                (_, Err(Err::NotYetSupported)) => Answer::No(Reason::DstIsNotYetSupported),
65                (Err(Err::SizeOverflow), _) => Answer::No(Reason::SrcSizeOverflow),
66                (_, Err(Err::SizeOverflow)) => Answer::No(Reason::DstSizeOverflow),
67                (Ok(src), Ok(dst)) => MaybeTransmutableQuery { src, dst, assume, context }.answer(),
68            }
69        }
70    }
71}
72
73impl<C>
74    MaybeTransmutableQuery<
75        Tree<<C as QueryContext>::Def, <C as QueryContext>::Region, <C as QueryContext>::Type>,
76        C,
77    >
78where
79    C: QueryContext,
80{
81    /// Answers whether a `Tree` is transmutable into another `Tree`.
82    ///
83    /// Removes definition markers from both trees and, unless safety is assumed,
84    /// prunes destination paths that may carry safety invariants. It then converts
85    /// the remaining layouts to `Dfa`s and compares them.
86    #[inline(always)]
87    {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("answer",
                                    "rustc_transmute::maybe_transmutable",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(87u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("src")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("src");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("dst")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("dst");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self.src)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self.dst)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Answer<<C as QueryContext>::Region,
                    <C as QueryContext>::Type> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let Self { src, dst, assume, context } = self;
            let src = src.prune(&|_def| false);
            if src.is_inhabited() && !dst.is_inhabited() {
                return Answer::No(Reason::DstUninhabited);
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event /rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs:100",
                                    "rustc_transmute::maybe_transmutable",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(100u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                                    ::tracing_core::field::FieldSet::new(&["message",
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("src")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("src");
                                                        NAME.as_str()
                                                    }], ::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};
                            __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("pruned src")
                                                        as &dyn ::tracing::field::Value)),
                                            (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&src)
                                                        as &dyn ::tracing::field::Value))])
                        });
                } else { ; }
            };
            let dst =
                if assume.safety {
                    dst.prune(&|_def| false)
                } else { dst.prune(&|def| def.has_safety_invariants()) };
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event /rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs:110",
                                    "rustc_transmute::maybe_transmutable",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(110u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                                    ::tracing_core::field::FieldSet::new(&["message",
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("dst")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("dst");
                                                        NAME.as_str()
                                                    }], ::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};
                            __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("pruned dst")
                                                        as &dyn ::tracing::field::Value)),
                                            (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&dst)
                                                        as &dyn ::tracing::field::Value))])
                        });
                } else { ; }
            };
            let src =
                match Dfa::from_tree(src) {
                    Ok(src) => src,
                    Err(layout::Uninhabited) => return Answer::Yes,
                };
            let dst =
                match Dfa::from_tree(dst) {
                    Ok(dst) => dst,
                    Err(layout::Uninhabited) =>
                        return Answer::No(Reason::DstMayHaveSafetyInvariants),
                };
            MaybeTransmutableQuery { src, dst, assume, context }.answer()
        }
    }
}#[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))]
88    pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Region, <C as QueryContext>::Type> {
89        let Self { src, dst, assume, context } = self;
90
91        // Keep every source representation while removing its definition markers.
92        // Reference nodes remain intact; mutable destination references also generate
93        // a reverse transmutability obligation for their referents.
94        let src = src.prune(&|_def| false);
95
96        if src.is_inhabited() && !dst.is_inhabited() {
97            return Answer::No(Reason::DstUninhabited);
98        }
99
100        trace!(?src, "pruned src");
101
102        // Remove destination definition markers. Unless the caller assumes safety,
103        // prune paths whose definitions may carry safety invariants.
104        let dst = if assume.safety {
105            dst.prune(&|_def| false)
106        } else {
107            dst.prune(&|def| def.has_safety_invariants())
108        };
109
110        trace!(?dst, "pruned dst");
111
112        // Convert `src` from a tree-based representation to a DFA-based
113        // representation. If the conversion fails because `src` is uninhabited,
114        // conclude that the transmutation is acceptable, because instances of
115        // the `src` type do not exist.
116        let src = match Dfa::from_tree(src) {
117            Ok(src) => src,
118            Err(layout::Uninhabited) => return Answer::Yes,
119        };
120
121        // An inhabited source and an originally uninhabited destination were
122        // rejected above. If the pruned destination is now uninhabited, no path
123        // remains whose definitions are known to be free of safety invariants.
124        let dst = match Dfa::from_tree(dst) {
125            Ok(dst) => dst,
126            Err(layout::Uninhabited) => return Answer::No(Reason::DstMayHaveSafetyInvariants),
127        };
128
129        MaybeTransmutableQuery { src, dst, assume, context }.answer()
130    }
131}
132
133impl<C> MaybeTransmutableQuery<Dfa<<C as QueryContext>::Region, <C as QueryContext>::Type>, C>
134where
135    C: QueryContext,
136{
137    /// Answers whether a `Dfa` is transmutable into another `Dfa`.
138    pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Region, <C as QueryContext>::Type> {
139        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs:139",
                        "rustc_transmute::maybe_transmutable",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(139u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                        ::tracing_core::field::FieldSet::new(&[{
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("src")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("src");
                                            NAME.as_str()
                                        }], ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self.src)
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(src = ?self.src);
140        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs:140",
                        "rustc_transmute::maybe_transmutable",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(140u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                        ::tracing_core::field::FieldSet::new(&[{
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("dst")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("dst");
                                            NAME.as_str()
                                        }], ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self.dst)
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(dst = ?self.dst);
141        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs:141",
                        "rustc_transmute::maybe_transmutable",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(141u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                        ::tracing_core::field::FieldSet::new(&[{
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("src_transitions_len")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("src_transitions_len");
                                            NAME.as_str()
                                        },
                                        {
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("dst_transitions_len")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("dst_transitions_len");
                                            NAME.as_str()
                                        }], ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&self.src.transitions.len()
                                            as &dyn ::tracing::field::Value)),
                                (::tracing::__macro_support::Option::Some(&self.dst.transitions.len()
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
142            src_transitions_len = self.src.transitions.len(),
143            dst_transitions_len = self.dst.transitions.len()
144        );
145        self.answer_memo(&mut Map::default(), self.src.start, self.dst.start)
146    }
147
148    #[inline(always)]
149    {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("answer_memo",
                                    "rustc_transmute::maybe_transmutable",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(149u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("src_state")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("src_state");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("dst_state")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("dst_state");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&src_state)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&dst_state)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Answer<<C as QueryContext>::Region,
                    <C as QueryContext>::Type> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if let Some(answer) = cache.get(&(src_state, dst_state)) {
                answer.clone()
            } else {
                let answer = self.answer_impl(cache, src_state, dst_state);
                if let Some(..) =
                        cache.insert((src_state, dst_state), answer.clone()) {
                    {
                        ::core::panicking::panic_fmt(format_args!("failed to correctly cache transmutability"));
                    }
                }
                answer
            }
        }
    }
}#[instrument(level = "debug", skip(self, cache))]
150    fn answer_memo(
151        &self,
152        cache: &mut Map<
153            (dfa::State, dfa::State),
154            Answer<<C as QueryContext>::Region, <C as QueryContext>::Type>,
155        >,
156        src_state: dfa::State,
157        dst_state: dfa::State,
158    ) -> Answer<<C as QueryContext>::Region, <C as QueryContext>::Type> {
159        if let Some(answer) = cache.get(&(src_state, dst_state)) {
160            answer.clone()
161        } else {
162            let answer = self.answer_impl(cache, src_state, dst_state);
163            if let Some(..) = cache.insert((src_state, dst_state), answer.clone()) {
164                panic!("failed to correctly cache transmutability")
165            }
166            answer
167        }
168    }
169
170    fn answer_impl(
171        &self,
172        cache: &mut Map<
173            (dfa::State, dfa::State),
174            Answer<<C as QueryContext>::Region, <C as QueryContext>::Type>,
175        >,
176        src_state: dfa::State,
177        dst_state: dfa::State,
178    ) -> Answer<<C as QueryContext>::Region, <C as QueryContext>::Type> {
179        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs:179",
                        "rustc_transmute::maybe_transmutable",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(179u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                        ::tracing_core::field::FieldSet::new(&[{
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("src_state")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("src_state");
                                            NAME.as_str()
                                        },
                                        {
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("dst_state")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("dst_state");
                                            NAME.as_str()
                                        }], ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&src_state)
                                            as &dyn ::tracing::field::Value)),
                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&dst_state)
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(?src_state, ?dst_state);
180        if dst_state == self.dst.accept {
181            // The destination needs no more input. Union transmutation permits
182            // truncating the remaining source bytes: for example, `u8` to `()`.
183            // Compatibility of the consumed prefix is checked by the preceding
184            // transitions, including any conditions they generate for references.
185            Answer::Yes
186        } else if src_state == self.src.accept {
187            // extension: `size_of(Src) <= size_of(Dst)`
188            if let Some(dst_state_prime) = self.dst.get_uninit_edge_dst(dst_state) {
189                self.answer_memo(cache, src_state, dst_state_prime)
190            } else {
191                Answer::No(Reason::DstIsTooBig)
192            }
193        } else {
194            let src_quantifier = if self.assume.validity {
195                // The caller checks validity (for example, `src <= 1u8` for a
196                // `u8`-to-`bool` transmutation), so at least one source transition
197                // must admit a compatible continuation.
198                Quantifier::ThereExists
199            } else {
200                // Every source transition must admit a compatible continuation
201                // in the destination when the caller does not assume validity.
202                Quantifier::ForAll
203            };
204
205            let bytes_answer = src_quantifier.apply(
206                union(self.src.bytes_from(src_state), self.dst.bytes_from(dst_state)).filter_map(
207                    |(_range, (src_state_prime, dst_state_prime))| {
208                        match (src_state_prime, dst_state_prime) {
209                            // No matching transitions in `src`. Skip.
210                            (None, _) => None,
211                            // No matching transitions in `dst`. Fail.
212                            (Some(_), None) => Some(Answer::No(Reason::DstIsBitIncompatible)),
213                            // Matching transitions. Continue with successor states.
214                            (Some(src_state_prime), Some(dst_state_prime)) => {
215                                Some(self.answer_memo(cache, src_state_prime, dst_state_prime))
216                            }
217                        }
218                    },
219                ),
220            );
221
222            // The below early returns reflect how this code would behave:
223            //   if self.assume.validity {
224            //       or(bytes_answer, refs_answer)
225            //   } else {
226            //       and(bytes_answer, refs_answer)
227            //   }
228            // ...if `refs_answer` was computed lazily. The below early
229            // returns can be deleted without impacting the correctness of
230            // the algorithm; only its performance.
231            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs:231",
                        "rustc_transmute::maybe_transmutable",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_transmute/src/maybe_transmutable/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(231u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_transmute::maybe_transmutable"),
                        ::tracing_core::field::FieldSet::new(&[{
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("bytes_answer")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("bytes_answer");
                                            NAME.as_str()
                                        }], ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&bytes_answer)
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(?bytes_answer);
232            match bytes_answer {
233                Answer::No(_) if !self.assume.validity => return bytes_answer,
234                Answer::Yes if self.assume.validity => return bytes_answer,
235                _ => {}
236            };
237
238            let refs_answer = src_quantifier.apply(
239                // for each reference transition out of `src_state`...
240                self.src.refs_from(src_state).map(|(src_ref, src_state_prime)| {
241                    // ...there exists a reference transition out of `dst_state`...
242                    Quantifier::ThereExists.apply(self.dst.refs_from(dst_state).map(
243                        |(dst_ref, dst_state_prime)| {
244                            if !src_ref.is_mut && dst_ref.is_mut {
245                                Answer::No(Reason::DstIsMoreUnique)
246                            } else if !self.assume.alignment
247                                && src_ref.referent_align < dst_ref.referent_align
248                            {
249                                Answer::No(Reason::DstHasStricterAlignment {
250                                    src_min_align: src_ref.referent_align,
251                                    dst_min_align: dst_ref.referent_align,
252                                })
253                            } else if dst_ref.referent_size > src_ref.referent_size {
254                                Answer::No(Reason::DstRefIsTooBig {
255                                    src: src_ref.referent,
256                                    src_size: src_ref.referent_size,
257                                    dst: dst_ref.referent,
258                                    dst_size: dst_ref.referent_size,
259                                })
260                            } else {
261                                let mut conditions = Vec::with_capacity(4);
262                                let mut is_transmutable =
263                                    |src: Reference<_, _>, dst: Reference<_, _>| {
264                                        conditions.push(Condition::Transmutable {
265                                            src: src.referent,
266                                            dst: dst.referent,
267                                        });
268                                        if !self.assume.lifetimes {
269                                            conditions.push(Condition::Outlives {
270                                                long: src.region,
271                                                short: dst.region,
272                                            });
273                                        }
274                                    };
275
276                                is_transmutable(src_ref, dst_ref);
277
278                                if dst_ref.is_mut {
279                                    is_transmutable(dst_ref, src_ref);
280                                } else {
281                                    conditions.push(Condition::Immutable { ty: dst_ref.referent });
282                                }
283
284                                Answer::If(Condition::IfAll(conditions)).and(self.answer_memo(
285                                    cache,
286                                    src_state_prime,
287                                    dst_state_prime,
288                                ))
289                            }
290                        },
291                    ))
292                }),
293            );
294
295            if self.assume.validity {
296                bytes_answer.or(refs_answer)
297            } else {
298                bytes_answer.and(refs_answer)
299            }
300        }
301    }
302}
303
304enum Quantifier {
305    ThereExists,
306    ForAll,
307}
308
309impl Quantifier {
310    /// Folds answers with `or` or `and`, stopping on `Yes` or `No`, respectively.
311    /// An empty iterator yields bit incompatibility for `ThereExists` and `Yes`
312    /// for `ForAll`.
313    fn apply<R, T, I>(&self, iter: I) -> Answer<R, T>
314    where
315        R: layout::Region,
316        T: layout::Type,
317        I: IntoIterator<Item = Answer<R, T>>,
318    {
319        use std::ops::ControlFlow::{Break, Continue};
320
321        let (init, try_fold_f): (_, fn(_, _) -> _) = match self {
322            Self::ThereExists => {
323                (Answer::No(Reason::DstIsBitIncompatible), |accum: Answer<R, T>, next| match accum
324                    .or(next)
325                {
326                    Answer::Yes => Break(Answer::Yes),
327                    maybe => Continue(maybe),
328                })
329            }
330            Self::ForAll => (Answer::Yes, |accum: Answer<R, T>, next| {
331                let answer = accum.and(next);
332                match answer {
333                    Answer::No(_) => Break(answer),
334                    maybe => Continue(maybe),
335                }
336            }),
337        };
338
339        let (Continue(result) | Break(result)) = iter.into_iter().try_fold(init, try_fold_f);
340        result
341    }
342}