Skip to main content

rustc_middle/ty/
assoc.rs

1use rustc_data_structures::sorted_map::SortedIndexMultiMap;
2use rustc_hir as hir;
3use rustc_hir::def::{DefKind, Namespace};
4use rustc_hir::def_id::DefId;
5use rustc_macros::{Decodable, Encodable, HashStable};
6use rustc_span::{ErrorGuaranteed, Ident, Symbol};
7
8use super::{TyCtxt, Visibility};
9use crate::ty;
10
11#[derive(#[automatically_derived]
impl ::core::clone::Clone for AssocContainer {
    #[inline]
    fn clone(&self) -> AssocContainer {
        let _:
                ::core::clone::AssertParamIsClone<Result<DefId,
                ErrorGuaranteed>>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AssocContainer { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for AssocContainer {
    #[inline]
    fn eq(&self, other: &AssocContainer) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (AssocContainer::TraitImpl(__self_0),
                    AssocContainer::TraitImpl(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for AssocContainer {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Result<DefId, ErrorGuaranteed>>;
    }
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for AssocContainer {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            AssocContainer::Trait =>
                ::core::fmt::Formatter::write_str(f, "Trait"),
            AssocContainer::InherentImpl =>
                ::core::fmt::Formatter::write_str(f, "InherentImpl"),
            AssocContainer::TraitImpl(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "TraitImpl", &__self_0),
        }
    }
}Debug, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_query_system::ich::StableHashingContext<'__ctx>>
            for AssocContainer {
            #[inline]
            fn hash_stable(&self,
                __hcx:
                    &mut ::rustc_query_system::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    AssocContainer::Trait => {}
                    AssocContainer::InherentImpl => {}
                    AssocContainer::TraitImpl(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable, #[automatically_derived]
impl ::core::hash::Hash for AssocContainer {
    #[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 {
            AssocContainer::TraitImpl(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            _ => {}
        }
    }
}Hash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for AssocContainer {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        AssocContainer::Trait => { 0usize }
                        AssocContainer::InherentImpl => { 1usize }
                        AssocContainer::TraitImpl(ref __binding_0) => { 2usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    AssocContainer::Trait => {}
                    AssocContainer::InherentImpl => {}
                    AssocContainer::TraitImpl(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for AssocContainer {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { AssocContainer::Trait }
                    1usize => { AssocContainer::InherentImpl }
                    2usize => {
                        AssocContainer::TraitImpl(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `AssocContainer`, expected 0..3, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable)]
12pub enum AssocContainer {
13    Trait,
14    InherentImpl,
15    /// The `DefId` points to the trait item being implemented.
16    TraitImpl(Result<DefId, ErrorGuaranteed>),
17}
18
19/// Information about an associated item
20#[derive(#[automatically_derived]
impl ::core::marker::Copy for AssocItem { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AssocItem {
    #[inline]
    fn clone(&self) -> AssocItem {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        let _: ::core::clone::AssertParamIsClone<AssocKind>;
        let _: ::core::clone::AssertParamIsClone<AssocContainer>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for AssocItem {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "AssocItem",
            "def_id", &self.def_id, "kind", &self.kind, "container",
            &&self.container)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for AssocItem {
    #[inline]
    fn eq(&self, other: &AssocItem) -> bool {
        self.def_id == other.def_id && self.kind == other.kind &&
            self.container == other.container
    }
}PartialEq, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_query_system::ich::StableHashingContext<'__ctx>>
            for AssocItem {
            #[inline]
            fn hash_stable(&self,
                __hcx:
                    &mut ::rustc_query_system::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    AssocItem {
                        def_id: ref __binding_0,
                        kind: ref __binding_1,
                        container: ref __binding_2 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                        { __binding_2.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable, #[automatically_derived]
impl ::core::cmp::Eq for AssocItem {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<DefId>;
        let _: ::core::cmp::AssertParamIsEq<AssocKind>;
        let _: ::core::cmp::AssertParamIsEq<AssocContainer>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for AssocItem {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.def_id, state);
        ::core::hash::Hash::hash(&self.kind, state);
        ::core::hash::Hash::hash(&self.container, state)
    }
}Hash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for AssocItem {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    AssocItem {
                        def_id: ref __binding_0,
                        kind: ref __binding_1,
                        container: 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, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for AssocItem {
            fn decode(__decoder: &mut __D) -> Self {
                AssocItem {
                    def_id: ::rustc_serialize::Decodable::decode(__decoder),
                    kind: ::rustc_serialize::Decodable::decode(__decoder),
                    container: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable)]
21pub struct AssocItem {
22    pub def_id: DefId,
23    pub kind: AssocKind,
24    pub container: AssocContainer,
25}
26
27impl AssocItem {
28    // Gets the identifier, if it has one.
29    pub fn opt_name(&self) -> Option<Symbol> {
30        match self.kind {
31            ty::AssocKind::Type { data: AssocTypeData::Normal(name) } => Some(name),
32            ty::AssocKind::Type { data: AssocTypeData::Rpitit(_) } => None,
33            ty::AssocKind::Const { name } => Some(name),
34            ty::AssocKind::Fn { name, .. } => Some(name),
35        }
36    }
37
38    // Gets the identifier name. Aborts if it lacks one, i.e. is an RPITIT
39    // associated type.
40    pub fn name(&self) -> Symbol {
41        self.opt_name().expect("name of non-Rpitit assoc item")
42    }
43
44    pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
45        Ident::new(self.name(), tcx.def_ident_span(self.def_id).unwrap())
46    }
47
48    /// Gets the defaultness of the associated item.
49    /// To get the default associated type, use the [`type_of`] query on the
50    /// [`DefId`] of the type.
51    ///
52    /// [`type_of`]: crate::ty::TyCtxt::type_of
53    pub fn defaultness(&self, tcx: TyCtxt<'_>) -> hir::Defaultness {
54        match self.container {
55            AssocContainer::InherentImpl => hir::Defaultness::Final,
56            AssocContainer::Trait | AssocContainer::TraitImpl(_) => tcx.defaultness(self.def_id),
57        }
58    }
59
60    pub fn expect_trait_impl(&self) -> Result<DefId, ErrorGuaranteed> {
61        let AssocContainer::TraitImpl(trait_item_id) = self.container else {
62            crate::util::bug::bug_fmt(format_args!("expected item to be in a trait impl: {0:?}",
        self.def_id));bug!("expected item to be in a trait impl: {:?}", self.def_id);
63        };
64        trait_item_id
65    }
66
67    /// If this is a trait impl item, returns the `DefId` of the trait item this implements.
68    /// Otherwise, returns `DefId` for self. Returns an Err in case the trait item was not
69    /// resolved successfully.
70    pub fn trait_item_or_self(&self) -> Result<DefId, ErrorGuaranteed> {
71        match self.container {
72            AssocContainer::TraitImpl(id) => id,
73            AssocContainer::Trait | AssocContainer::InherentImpl => Ok(self.def_id),
74        }
75    }
76
77    pub fn trait_item_def_id(&self) -> Option<DefId> {
78        match self.container {
79            AssocContainer::TraitImpl(Ok(id)) => Some(id),
80            _ => None,
81        }
82    }
83
84    #[inline]
85    pub fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility<DefId> {
86        tcx.visibility(self.def_id)
87    }
88
89    #[inline]
90    pub fn container_id(&self, tcx: TyCtxt<'_>) -> DefId {
91        tcx.parent(self.def_id)
92    }
93
94    #[inline]
95    pub fn trait_container(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
96        match self.container {
97            AssocContainer::InherentImpl | AssocContainer::TraitImpl(_) => None,
98            AssocContainer::Trait => Some(tcx.parent(self.def_id)),
99        }
100    }
101
102    #[inline]
103    pub fn impl_container(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
104        match self.container {
105            AssocContainer::InherentImpl | AssocContainer::TraitImpl(_) => {
106                Some(tcx.parent(self.def_id))
107            }
108            AssocContainer::Trait => None,
109        }
110    }
111
112    pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
113        match self.kind {
114            ty::AssocKind::Fn { .. } => {
115                // We skip the binder here because the binder would deanonymize all
116                // late-bound regions, and we don't want method signatures to show up
117                // `as for<'r> fn(&'r MyType)`. Pretty-printing handles late-bound
118                // regions just fine, showing `fn(&MyType)`.
119                tcx.fn_sig(self.def_id).instantiate_identity().skip_binder().to_string()
120            }
121            ty::AssocKind::Type { .. } => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("type {0};", self.name()))
    })format!("type {};", self.name()),
122            ty::AssocKind::Const { name } => {
123                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("const {0}: {1:?};", name,
                tcx.type_of(self.def_id).instantiate_identity()))
    })format!("const {}: {:?};", name, tcx.type_of(self.def_id).instantiate_identity())
124            }
125        }
126    }
127
128    pub fn descr(&self) -> &'static str {
129        self.kind.descr()
130    }
131
132    pub fn namespace(&self) -> Namespace {
133        self.kind.namespace()
134    }
135
136    pub fn as_def_kind(&self) -> DefKind {
137        self.kind.as_def_kind()
138    }
139
140    pub fn is_const(&self) -> bool {
141        #[allow(non_exhaustive_omitted_patterns)] match self.kind {
    ty::AssocKind::Const { .. } => true,
    _ => false,
}matches!(self.kind, ty::AssocKind::Const { .. })
142    }
143
144    pub fn is_fn(&self) -> bool {
145        #[allow(non_exhaustive_omitted_patterns)] match self.kind {
    ty::AssocKind::Fn { .. } => true,
    _ => false,
}matches!(self.kind, ty::AssocKind::Fn { .. })
146    }
147
148    pub fn is_method(&self) -> bool {
149        #[allow(non_exhaustive_omitted_patterns)] match self.kind {
    ty::AssocKind::Fn { has_self: true, .. } => true,
    _ => false,
}matches!(self.kind, ty::AssocKind::Fn { has_self: true, .. })
150    }
151
152    pub fn is_type(&self) -> bool {
153        #[allow(non_exhaustive_omitted_patterns)] match self.kind {
    ty::AssocKind::Type { .. } => true,
    _ => false,
}matches!(self.kind, ty::AssocKind::Type { .. })
154    }
155
156    pub fn tag(&self) -> AssocTag {
157        self.kind.tag()
158    }
159
160    pub fn is_impl_trait_in_trait(&self) -> bool {
161        #[allow(non_exhaustive_omitted_patterns)] match self.kind {
    AssocKind::Type { data: AssocTypeData::Rpitit(_) } => true,
    _ => false,
}matches!(self.kind, AssocKind::Type { data: AssocTypeData::Rpitit(_) })
162    }
163}
164
165#[derive(#[automatically_derived]
impl ::core::marker::Copy for AssocTypeData { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AssocTypeData {
    #[inline]
    fn clone(&self) -> AssocTypeData {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        let _: ::core::clone::AssertParamIsClone<ty::ImplTraitInTraitData>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for AssocTypeData {
    #[inline]
    fn eq(&self, other: &AssocTypeData) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (AssocTypeData::Normal(__self_0),
                    AssocTypeData::Normal(__arg1_0)) => __self_0 == __arg1_0,
                (AssocTypeData::Rpitit(__self_0),
                    AssocTypeData::Rpitit(__arg1_0)) => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for AssocTypeData {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            AssocTypeData::Normal(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Normal",
                    &__self_0),
            AssocTypeData::Rpitit(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Rpitit",
                    &__self_0),
        }
    }
}Debug, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_query_system::ich::StableHashingContext<'__ctx>>
            for AssocTypeData {
            #[inline]
            fn hash_stable(&self,
                __hcx:
                    &mut ::rustc_query_system::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    AssocTypeData::Normal(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                    AssocTypeData::Rpitit(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable, #[automatically_derived]
impl ::core::cmp::Eq for AssocTypeData {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
        let _: ::core::cmp::AssertParamIsEq<ty::ImplTraitInTraitData>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for AssocTypeData {
    #[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 {
            AssocTypeData::Normal(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            AssocTypeData::Rpitit(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
        }
    }
}Hash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for AssocTypeData {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        AssocTypeData::Normal(ref __binding_0) => { 0usize }
                        AssocTypeData::Rpitit(ref __binding_0) => { 1usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    AssocTypeData::Normal(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    AssocTypeData::Rpitit(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for AssocTypeData {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => {
                        AssocTypeData::Normal(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    1usize => {
                        AssocTypeData::Rpitit(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `AssocTypeData`, expected 0..2, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable)]
166pub enum AssocTypeData {
167    Normal(Symbol),
168    /// The associated type comes from an RPITIT. It has no name, and the
169    /// `ImplTraitInTraitData` provides additional information about its
170    /// source.
171    Rpitit(ty::ImplTraitInTraitData),
172}
173
174#[derive(#[automatically_derived]
impl ::core::marker::Copy for AssocKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AssocKind {
    #[inline]
    fn clone(&self) -> AssocKind {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        let _: ::core::clone::AssertParamIsClone<bool>;
        let _: ::core::clone::AssertParamIsClone<AssocTypeData>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for AssocKind {
    #[inline]
    fn eq(&self, other: &AssocKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (AssocKind::Const { name: __self_0 }, AssocKind::Const {
                    name: __arg1_0 }) => __self_0 == __arg1_0,
                (AssocKind::Fn { name: __self_0, has_self: __self_1 },
                    AssocKind::Fn { name: __arg1_0, has_self: __arg1_1 }) =>
                    __self_1 == __arg1_1 && __self_0 == __arg1_0,
                (AssocKind::Type { data: __self_0 }, AssocKind::Type {
                    data: __arg1_0 }) => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for AssocKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            AssocKind::Const { name: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Const",
                    "name", &__self_0),
            AssocKind::Fn { name: __self_0, has_self: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f, "Fn",
                    "name", __self_0, "has_self", &__self_1),
            AssocKind::Type { data: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Type",
                    "data", &__self_0),
        }
    }
}Debug, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_query_system::ich::StableHashingContext<'__ctx>>
            for AssocKind {
            #[inline]
            fn hash_stable(&self,
                __hcx:
                    &mut ::rustc_query_system::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    AssocKind::Const { name: ref __binding_0 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                    AssocKind::Fn {
                        name: ref __binding_0, has_self: ref __binding_1 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                    AssocKind::Type { data: ref __binding_0 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable, #[automatically_derived]
impl ::core::cmp::Eq for AssocKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
        let _: ::core::cmp::AssertParamIsEq<bool>;
        let _: ::core::cmp::AssertParamIsEq<AssocTypeData>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for AssocKind {
    #[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 {
            AssocKind::Const { name: __self_0 } =>
                ::core::hash::Hash::hash(__self_0, state),
            AssocKind::Fn { name: __self_0, has_self: __self_1 } => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state)
            }
            AssocKind::Type { data: __self_0 } =>
                ::core::hash::Hash::hash(__self_0, state),
        }
    }
}Hash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for AssocKind {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        AssocKind::Const { name: ref __binding_0 } => { 0usize }
                        AssocKind::Fn {
                            name: ref __binding_0, has_self: ref __binding_1 } => {
                            1usize
                        }
                        AssocKind::Type { data: ref __binding_0 } => { 2usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    AssocKind::Const { name: ref __binding_0 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    AssocKind::Fn {
                        name: ref __binding_0, has_self: ref __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                    AssocKind::Type { data: ref __binding_0 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for AssocKind {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => {
                        AssocKind::Const {
                            name: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    1usize => {
                        AssocKind::Fn {
                            name: ::rustc_serialize::Decodable::decode(__decoder),
                            has_self: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    2usize => {
                        AssocKind::Type {
                            data: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `AssocKind`, expected 0..3, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable)]
175pub enum AssocKind {
176    Const { name: Symbol },
177    Fn { name: Symbol, has_self: bool },
178    Type { data: AssocTypeData },
179}
180
181impl AssocKind {
182    pub fn namespace(&self) -> Namespace {
183        match self {
184            Self::Type { .. } => Namespace::TypeNS,
185            Self::Const { .. } | Self::Fn { .. } => Namespace::ValueNS,
186        }
187    }
188
189    pub fn tag(&self) -> AssocTag {
190        match self {
191            Self::Const { .. } => AssocTag::Const,
192            Self::Fn { .. } => AssocTag::Fn,
193            Self::Type { .. } => AssocTag::Type,
194        }
195    }
196
197    pub fn as_def_kind(&self) -> DefKind {
198        match self {
199            Self::Const { .. } => DefKind::AssocConst,
200            Self::Fn { .. } => DefKind::AssocFn,
201            Self::Type { .. } => DefKind::AssocTy,
202        }
203    }
204
205    pub fn descr(&self) -> &'static str {
206        match self {
207            Self::Fn { has_self: true, .. } => "method",
208            _ => self.tag().descr(),
209        }
210    }
211}
212
213impl std::fmt::Display for AssocKind {
214    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215        f.write_str(self.descr())
216    }
217}
218
219/// Like [`AssocKind`], but just the tag, no fields. Used in various kinds of matching.
220#[derive(#[automatically_derived]
impl ::core::clone::Clone for AssocTag {
    #[inline]
    fn clone(&self) -> AssocTag { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AssocTag { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for AssocTag {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                AssocTag::Const => "Const",
                AssocTag::Fn => "Fn",
                AssocTag::Type => "Type",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for AssocTag {
    #[inline]
    fn eq(&self, other: &AssocTag) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for AssocTag {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for AssocTag {
    #[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)
    }
}Hash)]
221pub enum AssocTag {
222    Const,
223    Fn,
224    Type,
225}
226
227impl AssocTag {
228    pub fn descr(self) -> &'static str {
229        // This should match `DefKind::descr`.
230        match self {
231            Self::Const => "associated constant",
232            Self::Fn => "associated function",
233            Self::Type => "associated type",
234        }
235    }
236}
237
238/// A list of `ty::AssocItem`s in definition order that allows for efficient lookup by name.
239///
240/// When doing lookup by name, we try to postpone hygienic comparison for as long as possible since
241/// it is relatively expensive. Instead, items are indexed by `Symbol` and hygienic comparison is
242/// done only on items with the same name.
243#[derive(#[automatically_derived]
impl ::core::fmt::Debug for AssocItems {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f, "AssocItems",
            "items", &&self.items)
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for AssocItems {
    #[inline]
    fn clone(&self) -> AssocItems {
        AssocItems { items: ::core::clone::Clone::clone(&self.items) }
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for AssocItems {
    #[inline]
    fn eq(&self, other: &AssocItems) -> bool { self.items == other.items }
}PartialEq, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_query_system::ich::StableHashingContext<'__ctx>>
            for AssocItems {
            #[inline]
            fn hash_stable(&self,
                __hcx:
                    &mut ::rustc_query_system::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    AssocItems { items: ref __binding_0 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable)]
244pub struct AssocItems {
245    items: SortedIndexMultiMap<u32, Option<Symbol>, ty::AssocItem>,
246}
247
248impl AssocItems {
249    /// Constructs an `AssociatedItems` map from a series of `ty::AssocItem`s in definition order.
250    pub fn new(items_in_def_order: impl IntoIterator<Item = ty::AssocItem>) -> Self {
251        let items = items_in_def_order.into_iter().map(|item| (item.opt_name(), item)).collect();
252        AssocItems { items }
253    }
254
255    /// Returns an iterator over associated items in the order they were defined.
256    ///
257    /// New code should avoid relying on definition order. If you need a particular associated item
258    /// for a known trait, make that trait a lang item instead of indexing this array.
259    pub fn in_definition_order(&self) -> impl '_ + Iterator<Item = &ty::AssocItem> {
260        self.items.iter().map(|(_, v)| v)
261    }
262
263    pub fn len(&self) -> usize {
264        self.items.len()
265    }
266
267    /// Returns an iterator over all associated items with the given name, ignoring hygiene.
268    ///
269    /// Panics if `name.is_empty()` returns `true`.
270    pub fn filter_by_name_unhygienic(
271        &self,
272        name: Symbol,
273    ) -> impl '_ + Iterator<Item = &ty::AssocItem> {
274        if !!name.is_empty() {
    ::core::panicking::panic("assertion failed: !name.is_empty()")
};assert!(!name.is_empty());
275        self.items.get_by_key(Some(name))
276    }
277
278    /// Returns the associated item with the given identifier and `AssocKind`, if one exists.
279    /// The identifier is ignoring hygiene. This is meant to be used for lints and diagnostics.
280    pub fn filter_by_name_unhygienic_and_kind(
281        &self,
282        name: Symbol,
283        assoc_tag: AssocTag,
284    ) -> impl '_ + Iterator<Item = &ty::AssocItem> {
285        self.filter_by_name_unhygienic(name).filter(move |item| item.tag() == assoc_tag)
286    }
287
288    /// Returns the associated item with the given identifier and `AssocKind`, if one exists.
289    /// The identifier is matched hygienically.
290    pub fn find_by_ident_and_kind(
291        &self,
292        tcx: TyCtxt<'_>,
293        ident: Ident,
294        assoc_tag: AssocTag,
295        parent_def_id: DefId,
296    ) -> Option<&ty::AssocItem> {
297        self.filter_by_name_unhygienic(ident.name)
298            .filter(|item| item.tag() == assoc_tag)
299            .find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
300    }
301
302    /// Returns the associated item with the given identifier in the given `Namespace`, if one
303    /// exists. The identifier is matched hygienically.
304    pub fn find_by_ident_and_namespace(
305        &self,
306        tcx: TyCtxt<'_>,
307        ident: Ident,
308        ns: Namespace,
309        parent_def_id: DefId,
310    ) -> Option<&ty::AssocItem> {
311        self.filter_by_name_unhygienic(ident.name)
312            .filter(|item| item.namespace() == ns)
313            .find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
314    }
315}
316
317impl<'tcx> TyCtxt<'tcx> {
318    /// Given an `fn_def_id` of a trait or a trait implementation:
319    ///
320    /// if `fn_def_id` is a function defined inside a trait, then it synthesizes
321    /// a new def id corresponding to a new associated type for each return-
322    /// position `impl Trait` in the signature.
323    ///
324    /// if `fn_def_id` is a function inside of an impl, then for each synthetic
325    /// associated type generated for the corresponding trait function described
326    /// above, synthesize a corresponding associated type in the impl.
327    pub fn associated_types_for_impl_traits_in_associated_fn(
328        self,
329        fn_def_id: DefId,
330    ) -> &'tcx [DefId] {
331        let parent_def_id = self.parent(fn_def_id);
332        &self.associated_types_for_impl_traits_in_trait_or_impl(parent_def_id)[&fn_def_id]
333    }
334}