Skip to main content

rustc_type_ir/ty/
alias.rs

1use derive_where::derive_where;
2#[cfg(feature = "nightly")]
3use rustc_macros::{Decodable_NoContext, Encodable_NoContext, StableHash_NoContext};
4use rustc_type_ir_macros::{
5    GenericTypeVisitable, Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic,
6};
7
8use crate::predicate::AliasTermKind;
9use crate::ty_kind::AliasTyKind;
10use crate::{Interner, UnevaluatedConstKind};
11
12/// Represents an alias of a type, constant, or other term-like item.
13///
14/// * For a projection, this would be `<Ty as Trait<...>>::N<...>`.
15/// * For an inherent projection, this would be `Ty::N<...>`.
16/// * For an opaque type, there is no explicit syntax.
17#[automatically_derived]
impl<I: Interner, K> ::core::fmt::Debug for Alias<I, K> where I: Interner,
    K: ::core::fmt::Debug {
    fn fmt(&self, __f: &mut ::core::fmt::Formatter<'_>)
        -> ::core::fmt::Result {
        match self {
            Alias {
                kind: ref __field_kind,
                args: ref __field_args,
                _use_alias_new_instead: ref __field__use_alias_new_instead }
                => {
                let mut __builder =
                    ::core::fmt::Formatter::debug_struct(__f, "Alias");
                ::core::fmt::DebugStruct::field(&mut __builder, "kind",
                    __field_kind);
                ::core::fmt::DebugStruct::field(&mut __builder, "args",
                    __field_args);
                ::core::fmt::DebugStruct::finish_non_exhaustive(&mut __builder)
            }
        }
    }
}#[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner, K)]
18#[derive(const _: () =
    {
        impl<I: Interner, K> ::rustc_type_ir::TypeVisitable<I> for Alias<I, K>
            where I: Interner, K: ::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 {
                    Alias { kind: 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, K> ::rustc_type_ir::TypeFoldable<I> for Alias<I, K>
            where I: Interner, K: ::rustc_type_ir::TypeFoldable<I>,
            K: ::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 {
                        Alias {
                            kind: __binding_0,
                            args: __binding_1,
                            _use_alias_new_instead: __binding_2 } => {
                            Alias {
                                kind: ::rustc_type_ir::TypeFoldable::try_fold_with(__binding_0,
                                        __folder)?,
                                args: ::rustc_type_ir::TypeFoldable::try_fold_with(__binding_1,
                                        __folder)?,
                                _use_alias_new_instead: __binding_2,
                            }
                        }
                    })
            }
            fn fold_with<__F: ::rustc_type_ir::TypeFolder<I>>(self,
                __folder: &mut __F) -> Self {
                match self {
                    Alias {
                        kind: __binding_0,
                        args: __binding_1,
                        _use_alias_new_instead: __binding_2 } => {
                        Alias {
                            kind: ::rustc_type_ir::TypeFoldable::fold_with(__binding_0,
                                __folder),
                            args: ::rustc_type_ir::TypeFoldable::fold_with(__binding_1,
                                __folder),
                            _use_alias_new_instead: __binding_2,
                        }
                    }
                }
            }
        }
    };TypeFoldable_Generic, const _: () =
    {
        impl<I: Interner, K, J> ::rustc_type_ir::lift::Lift<J> for Alias<I, K>
            where J: Interner, I: ::rustc_type_ir::LiftInto<J>,
            K: ::rustc_type_ir::lift::Lift<J> {
            type Lifted =
                Alias<J, <K as ::rustc_type_ir::lift::Lift<J>>::Lifted>;
            fn lift_to_interner(self, interner: J) -> Self::Lifted {
                match self {
                    Alias {
                        kind: __binding_0,
                        args: __binding_1,
                        _use_alias_new_instead: __binding_2 } => {
                        Alias {
                            kind: __binding_0.lift_to_interner(interner),
                            args: __binding_1.lift_to_interner(interner),
                            _use_alias_new_instead: __binding_2,
                        }
                    }
                }
            }
        }
    };Lift_Generic)]
19#[cfg_attr(
20    feature = "nightly",
21    derive(const _: () =
    {
        impl<I: Interner, K, __D: ::rustc_serialize::Decoder>
            ::rustc_serialize::Decodable<__D> for Alias<I, K> where
            K: ::rustc_serialize::Decodable<__D>,
            I::GenericArgs: ::rustc_serialize::Decodable<__D> {
            fn decode(__decoder: &mut __D) -> Self {
                Alias {
                    kind: ::rustc_serialize::Decodable::decode(__decoder),
                    args: ::rustc_serialize::Decodable::decode(__decoder),
                    _use_alias_new_instead: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable_NoContext, const _: () =
    {
        impl<I: Interner, K, __E: ::rustc_serialize::Encoder>
            ::rustc_serialize::Encodable<__E> for Alias<I, K> where
            K: ::rustc_serialize::Encodable<__E>,
            I::GenericArgs: ::rustc_serialize::Encodable<__E> {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    Alias {
                        kind: ref __binding_0,
                        args: ref __binding_1,
                        _use_alias_new_instead: ref __binding_2 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                    }
                }
            }
        }
    };Encodable_NoContext, const _: () =
    {
        impl<I: Interner, K> ::rustc_data_structures::stable_hash::StableHash
            for Alias<I, K> where
            K: ::rustc_data_structures::stable_hash::StableHash,
            I::GenericArgs: ::rustc_data_structures::stable_hash::StableHash {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    Alias {
                        kind: ref __binding_0,
                        args: ref __binding_1,
                        _use_alias_new_instead: ref __binding_2 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                        { __binding_2.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash_NoContext)
22)]
23pub struct Alias<I: Interner, K> {
24    pub kind: K,
25
26    /// The parameters of the associated, opaque, or constant alias.
27    ///
28    /// For a projection, these are the generic parameters for the trait and the
29    /// GAT parameters, if there are any.
30    ///
31    /// For an inherent projection, they consist of the self type and the GAT parameters,
32    /// if there are any.
33    ///
34    /// For RPIT the generic parameters are for the generics of the function,
35    /// while for TAIT it is used for the generic parameters of the alias.
36    pub args: I::GenericArgs,
37
38    /// This field exists to prevent the creation of `Alias` without using the relevant constructor.
39    #[derive_where(skip(Debug))]
40    #[type_visitable(ignore)]
41    #[type_foldable(identity)]
42    #[lift(identity)]
43    pub(crate) _use_alias_new_instead: (),
44}
45
46impl<I: Interner, K: PartialEq> Eq for Alias<I, K> {}
47
48impl<I: Interner, K: Copy> Alias<I, K> {
49    pub fn kind(self, _interner: I) -> K {
50        self.kind
51    }
52}
53
54pub type AliasTerm<I> = Alias<I, AliasTermKind<I>>;
55pub type AliasTy<I> = Alias<I, AliasTyKind<I>>;
56pub type UnevaluatedConst<I> = Alias<I, UnevaluatedConstKind<I>>;