Skip to main content

rustc_type_ir/
opaque_ty.rs

1use derive_where::derive_where;
2#[cfg(feature = "nightly")]
3use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
4use rustc_type_ir_macros::{GenericTypeVisitable, TypeFoldable_Generic, TypeVisitable_Generic};
5
6use crate::inherent::*;
7use crate::{self as ty, Interner};
8
9#[automatically_derived]
impl<I: Interner> ::core::fmt::Debug for OpaqueTypeKey<I> where I: Interner {
    fn fmt(&self, __f: &mut ::core::fmt::Formatter<'_>)
        -> ::core::fmt::Result {
        match self {
            OpaqueTypeKey { def_id: ref __field_def_id, args: ref __field_args
                } => {
                let mut __builder =
                    ::core::fmt::Formatter::debug_struct(__f, "OpaqueTypeKey");
                ::core::fmt::DebugStruct::field(&mut __builder, "def_id",
                    __field_def_id);
                ::core::fmt::DebugStruct::field(&mut __builder, "args",
                    __field_args);
                ::core::fmt::DebugStruct::finish(&mut __builder)
            }
        }
    }
}#[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner)]
10#[derive(const _: () =
    {
        impl<I: Interner> ::rustc_type_ir::TypeVisitable<I> for
            OpaqueTypeKey<I> where I: Interner,
            I::LocalDefId: ::rustc_type_ir::TypeVisitable<I>,
            I::GenericArgs: ::rustc_type_ir::TypeVisitable<I> {
            fn visit_with<__V: ::rustc_type_ir::TypeVisitor<I>>(&self,
                __visitor: &mut __V) -> __V::Result {
                match *self {
                    OpaqueTypeKey {
                        def_id: ref __binding_0, args: ref __binding_1 } => {
                        {
                            match ::rustc_type_ir::VisitorResult::branch(::rustc_type_ir::TypeVisitable::visit_with(__binding_0,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_type_ir::VisitorResult::from_residual(r);
                                }
                            }
                        }
                        {
                            match ::rustc_type_ir::VisitorResult::branch(::rustc_type_ir::TypeVisitable::visit_with(__binding_1,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_type_ir::VisitorResult::from_residual(r);
                                }
                            }
                        }
                    }
                }
                <__V::Result as ::rustc_type_ir::VisitorResult>::output()
            }
        }
    };TypeVisitable_Generic, GenericTypeVisitable, const _: () =
    {
        impl<I: Interner> ::rustc_type_ir::TypeFoldable<I> for
            OpaqueTypeKey<I> where I: Interner,
            I::LocalDefId: ::rustc_type_ir::TypeFoldable<I>,
            I::GenericArgs: ::rustc_type_ir::TypeFoldable<I> {
            fn try_fold_with<__F: ::rustc_type_ir::FallibleTypeFolder<I>>(self,
                __folder: &mut __F) -> Result<Self, __F::Error> {
                Ok(match self {
                        OpaqueTypeKey { def_id: __binding_0, args: __binding_1 } =>
                            {
                            OpaqueTypeKey {
                                def_id: ::rustc_type_ir::TypeFoldable::try_fold_with(__binding_0,
                                        __folder)?,
                                args: ::rustc_type_ir::TypeFoldable::try_fold_with(__binding_1,
                                        __folder)?,
                            }
                        }
                    })
            }
            fn fold_with<__F: ::rustc_type_ir::TypeFolder<I>>(self,
                __folder: &mut __F) -> Self {
                match self {
                    OpaqueTypeKey { def_id: __binding_0, args: __binding_1 } =>
                        {
                        OpaqueTypeKey {
                            def_id: ::rustc_type_ir::TypeFoldable::fold_with(__binding_0,
                                __folder),
                            args: ::rustc_type_ir::TypeFoldable::fold_with(__binding_1,
                                __folder),
                        }
                    }
                }
            }
        }
    };TypeFoldable_Generic)]
11#[cfg_attr(
12    feature = "nightly",
13    derive(const _: () =
    {
        impl<I: Interner, __E: ::rustc_serialize::Encoder>
            ::rustc_serialize::Encodable<__E> for OpaqueTypeKey<I> where
            I::LocalDefId: ::rustc_serialize::Encodable<__E>,
            I::GenericArgs: ::rustc_serialize::Encodable<__E> {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    OpaqueTypeKey {
                        def_id: ref __binding_0, args: ref __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };Encodable_NoContext, const _: () =
    {
        impl<I: Interner, __D: ::rustc_serialize::Decoder>
            ::rustc_serialize::Decodable<__D> for OpaqueTypeKey<I> where
            I::LocalDefId: ::rustc_serialize::Decodable<__D>,
            I::GenericArgs: ::rustc_serialize::Decodable<__D> {
            fn decode(__decoder: &mut __D) -> Self {
                OpaqueTypeKey {
                    def_id: ::rustc_serialize::Decodable::decode(__decoder),
                    args: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable_NoContext, const _: () =
    {
        impl<I: Interner, __CTX>
            ::rustc_data_structures::stable_hasher::HashStable<__CTX> for
            OpaqueTypeKey<I> where
            I::LocalDefId: ::rustc_data_structures::stable_hasher::HashStable<__CTX>,
            I::GenericArgs: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
            {
            #[inline]
            fn hash_stable(&self, __hcx: &mut __CTX,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    OpaqueTypeKey {
                        def_id: ref __binding_0, args: ref __binding_1 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable_NoContext)
14)]
15pub struct OpaqueTypeKey<I: Interner> {
16    pub def_id: I::LocalDefId,
17    pub args: I::GenericArgs,
18}
19
20impl<I: Interner> Eq for OpaqueTypeKey<I> {}
21
22impl<I: Interner> OpaqueTypeKey<I> {
23    pub fn iter_captured_args(self, cx: I) -> impl Iterator<Item = (usize, I::GenericArg)> {
24        let variances = cx.variances_of(self.def_id.into());
25        std::iter::zip(self.args.iter(), variances.iter()).enumerate().filter_map(
26            |(i, (arg, v))| match (arg.kind(), v) {
27                (_, ty::Invariant) => Some((i, arg)),
28                (ty::GenericArgKind::Lifetime(_), ty::Bivariant) => None,
29                _ => {
    ::core::panicking::panic_fmt(format_args!("unexpected opaque type arg variance"));
}panic!("unexpected opaque type arg variance"),
30            },
31        )
32    }
33
34    pub fn fold_captured_lifetime_args(
35        self,
36        cx: I,
37        mut f: impl FnMut(I::Region) -> I::Region,
38    ) -> Self {
39        let Self { def_id, args } = self;
40        let variances = cx.variances_of(def_id.into());
41        let args =
42            std::iter::zip(args.iter(), variances.iter()).map(|(arg, v)| match (arg.kind(), v) {
43                (ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
44                (ty::GenericArgKind::Lifetime(lt), _) => f(lt).into(),
45                _ => arg,
46            });
47        let args = cx.mk_args_from_iter(args);
48        Self { def_id, args }
49    }
50}