Skip to main content

rustc_middle/ty/
generics.rs

1use rustc_ast as ast;
2use rustc_data_structures::fx::FxHashMap;
3use rustc_hir::def_id::DefId;
4use rustc_macros::{StableHash, TyDecodable, TyEncodable};
5use rustc_span::{Span, Symbol, kw};
6use tracing::instrument;
7
8use super::{Clause, InstantiatedPredicates, ParamConst, ParamTy, Ty, TyCtxt, Unnormalized};
9use crate::ty;
10use crate::ty::{EarlyBinder, GenericArgsRef};
11
12#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericParamDefKind {
    #[inline]
    fn clone(&self) -> GenericParamDefKind {
        match self {
            GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,
            GenericParamDefKind::Type {
                has_default: __self_0, synthetic: __self_1 } =>
                GenericParamDefKind::Type {
                    has_default: ::core::clone::Clone::clone(__self_0),
                    synthetic: ::core::clone::Clone::clone(__self_1),
                },
            GenericParamDefKind::Const { has_default: __self_0 } =>
                GenericParamDefKind::Const {
                    has_default: ::core::clone::Clone::clone(__self_0),
                },
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericParamDefKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            GenericParamDefKind::Lifetime =>
                ::core::fmt::Formatter::write_str(f, "Lifetime"),
            GenericParamDefKind::Type {
                has_default: __self_0, synthetic: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f, "Type",
                    "has_default", __self_0, "synthetic", &__self_1),
            GenericParamDefKind::Const { has_default: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Const",
                    "has_default", &__self_0),
        }
    }
}Debug, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for GenericParamDefKind {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        GenericParamDefKind::Lifetime => { 0usize }
                        GenericParamDefKind::Type {
                            has_default: ref __binding_0, synthetic: ref __binding_1 }
                            => {
                            1usize
                        }
                        GenericParamDefKind::Const { has_default: ref __binding_0 }
                            => {
                            2usize
                        }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    GenericParamDefKind::Lifetime => {}
                    GenericParamDefKind::Type {
                        has_default: ref __binding_0, synthetic: ref __binding_1 }
                        => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                    GenericParamDefKind::Const { has_default: ref __binding_0 }
                        => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for GenericParamDefKind {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { GenericParamDefKind::Lifetime }
                    1usize => {
                        GenericParamDefKind::Type {
                            has_default: ::rustc_serialize::Decodable::decode(__decoder),
                            synthetic: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    2usize => {
                        GenericParamDefKind::Const {
                            has_default: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `GenericParamDefKind`, expected 0..3, actual {0}",
                                n));
                    }
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            GenericParamDefKind {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    GenericParamDefKind::Lifetime => {}
                    GenericParamDefKind::Type {
                        has_default: ref __binding_0, synthetic: ref __binding_1 }
                        => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                    }
                    GenericParamDefKind::Const { has_default: ref __binding_0 }
                        => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
13pub enum GenericParamDefKind {
14    Lifetime,
15    Type { has_default: bool, synthetic: bool },
16    Const { has_default: bool },
17}
18
19impl GenericParamDefKind {
20    pub fn descr(&self) -> &'static str {
21        match self {
22            GenericParamDefKind::Lifetime => "lifetime",
23            GenericParamDefKind::Type { .. } => "type",
24            GenericParamDefKind::Const { .. } => "constant",
25        }
26    }
27    pub fn to_ord(&self) -> ast::ParamKindOrd {
28        match self {
29            GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
30            GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
31                ast::ParamKindOrd::TypeOrConst
32            }
33        }
34    }
35
36    pub fn is_ty_or_const(&self) -> bool {
37        match self {
38            GenericParamDefKind::Lifetime => false,
39            GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true,
40        }
41    }
42
43    pub fn is_synthetic(&self) -> bool {
44        match self {
45            GenericParamDefKind::Type { synthetic, .. } => *synthetic,
46            _ => false,
47        }
48    }
49}
50
51#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericParamDef {
    #[inline]
    fn clone(&self) -> GenericParamDef {
        GenericParamDef {
            name: ::core::clone::Clone::clone(&self.name),
            def_id: ::core::clone::Clone::clone(&self.def_id),
            index: ::core::clone::Clone::clone(&self.index),
            pure_wrt_drop: ::core::clone::Clone::clone(&self.pure_wrt_drop),
            kind: ::core::clone::Clone::clone(&self.kind),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericParamDef {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field5_finish(f,
            "GenericParamDef", "name", &self.name, "def_id", &self.def_id,
            "index", &self.index, "pure_wrt_drop", &self.pure_wrt_drop,
            "kind", &&self.kind)
    }
}Debug, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for GenericParamDef {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    GenericParamDef {
                        name: ref __binding_0,
                        def_id: ref __binding_1,
                        index: ref __binding_2,
                        pure_wrt_drop: ref __binding_3,
                        kind: ref __binding_4 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_3,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_4,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for GenericParamDef {
            fn decode(__decoder: &mut __D) -> Self {
                GenericParamDef {
                    name: ::rustc_serialize::Decodable::decode(__decoder),
                    def_id: ::rustc_serialize::Decodable::decode(__decoder),
                    index: ::rustc_serialize::Decodable::decode(__decoder),
                    pure_wrt_drop: ::rustc_serialize::Decodable::decode(__decoder),
                    kind: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            GenericParamDef {
            #[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 {
                    GenericParamDef {
                        name: ref __binding_0,
                        def_id: ref __binding_1,
                        index: ref __binding_2,
                        pure_wrt_drop: ref __binding_3,
                        kind: ref __binding_4 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                        { __binding_2.stable_hash(__hcx, __hasher); }
                        { __binding_3.stable_hash(__hcx, __hasher); }
                        { __binding_4.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
52pub struct GenericParamDef {
53    pub name: Symbol,
54    pub def_id: DefId,
55    pub index: u32,
56
57    /// `pure_wrt_drop`, set by the (unsafe) `#[may_dangle]` attribute
58    /// on generic parameter `'a`/`T`, asserts data behind the parameter
59    /// `'a`/`T` won't be accessed during the parent type's `Drop` impl.
60    pub pure_wrt_drop: bool,
61
62    pub kind: GenericParamDefKind,
63}
64
65impl GenericParamDef {
66    pub fn to_early_bound_region_data(&self) -> ty::EarlyParamRegion {
67        if let GenericParamDefKind::Lifetime = self.kind {
68            ty::EarlyParamRegion { index: self.index, name: self.name }
69        } else {
70            crate::util::bug::bug_fmt(format_args!("cannot convert a non-lifetime parameter def to an early bound region"))bug!("cannot convert a non-lifetime parameter def to an early bound region")
71        }
72    }
73
74    pub fn is_anonymous_lifetime(&self) -> bool {
75        match self.kind {
76            GenericParamDefKind::Lifetime => self.name == kw::UnderscoreLifetime,
77            _ => false,
78        }
79    }
80
81    pub fn default_value<'tcx>(
82        &self,
83        tcx: TyCtxt<'tcx>,
84    ) -> Option<EarlyBinder<'tcx, ty::GenericArg<'tcx>>> {
85        match self.kind {
86            GenericParamDefKind::Type { has_default: true, .. } => {
87                Some(tcx.type_of(self.def_id).map_bound(|t| t.into()))
88            }
89            GenericParamDefKind::Const { has_default: true, .. } => {
90                Some(tcx.const_param_default(self.def_id).map_bound(|c| c.into()))
91            }
92            _ => None,
93        }
94    }
95
96    pub fn to_error<'tcx>(&self, tcx: TyCtxt<'tcx>) -> ty::GenericArg<'tcx> {
97        match &self.kind {
98            ty::GenericParamDefKind::Lifetime => ty::Region::new_error_misc(tcx).into(),
99            ty::GenericParamDefKind::Type { .. } => Ty::new_misc_error(tcx).into(),
100            ty::GenericParamDefKind::Const { .. } => ty::Const::new_misc_error(tcx).into(),
101        }
102    }
103}
104
105#[derive(#[automatically_derived]
impl ::core::default::Default for GenericParamCount {
    #[inline]
    fn default() -> GenericParamCount {
        GenericParamCount {
            lifetimes: ::core::default::Default::default(),
            types: ::core::default::Default::default(),
            consts: ::core::default::Default::default(),
        }
    }
}Default)]
106pub struct GenericParamCount {
107    pub lifetimes: usize,
108    pub types: usize,
109    pub consts: usize,
110}
111
112/// Information about the formal type/lifetime parameters associated
113/// with an item or method. Analogous to `hir::Generics`.
114///
115/// The ordering of parameters is the same as in [`ty::GenericArg`] (excluding child generics):
116/// `Self` (optionally), `Lifetime` params..., `Type` params...
117#[derive(#[automatically_derived]
impl ::core::clone::Clone for Generics {
    #[inline]
    fn clone(&self) -> Generics {
        Generics {
            parent: ::core::clone::Clone::clone(&self.parent),
            parent_count: ::core::clone::Clone::clone(&self.parent_count),
            own_params: ::core::clone::Clone::clone(&self.own_params),
            param_def_id_to_index: ::core::clone::Clone::clone(&self.param_def_id_to_index),
            has_self: ::core::clone::Clone::clone(&self.has_self),
            has_late_bound_regions: ::core::clone::Clone::clone(&self.has_late_bound_regions),
        }
    }
}Clone, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for Generics {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    Generics {
                        parent: ref __binding_0,
                        parent_count: ref __binding_1,
                        own_params: ref __binding_2,
                        param_def_id_to_index: ref __binding_3,
                        has_self: ref __binding_4,
                        has_late_bound_regions: ref __binding_5 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_3,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_4,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_5,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for Generics {
            fn decode(__decoder: &mut __D) -> Self {
                Generics {
                    parent: ::rustc_serialize::Decodable::decode(__decoder),
                    parent_count: ::rustc_serialize::Decodable::decode(__decoder),
                    own_params: ::rustc_serialize::Decodable::decode(__decoder),
                    param_def_id_to_index: ::rustc_serialize::Decodable::decode(__decoder),
                    has_self: ::rustc_serialize::Decodable::decode(__decoder),
                    has_late_bound_regions: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for Generics {
            #[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 {
                    Generics {
                        parent: ref __binding_0,
                        parent_count: ref __binding_1,
                        own_params: ref __binding_2,
                        param_def_id_to_index: ref __binding_3,
                        has_self: ref __binding_4,
                        has_late_bound_regions: ref __binding_5 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                        { __binding_2.stable_hash(__hcx, __hasher); }
                        {}
                        { __binding_4.stable_hash(__hcx, __hasher); }
                        { __binding_5.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
118pub struct Generics {
119    pub parent: Option<DefId>,
120    pub parent_count: usize,
121    pub own_params: Vec<GenericParamDef>,
122
123    /// Reverse map to the `index` field of each `GenericParamDef`.
124    #[stable_hash(ignore)]
125    pub param_def_id_to_index: FxHashMap<DefId, u32>,
126
127    pub has_self: bool,
128    pub has_late_bound_regions: Option<Span>,
129}
130
131impl std::fmt::Debug for Generics {
132    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
133        // ironically, we get this warning because of what we're trying to fix.
134        #[expect(rustc::potential_query_instability)]
135        let mut stabilized_hashmap = self.param_def_id_to_index.iter().collect::<Vec<_>>();
136        stabilized_hashmap.sort_by_key(|(_, v)| **v);
137        f.debug_struct("Generics")
138            .field("parent", &self.parent)
139            .field("parent_count", &self.parent_count)
140            .field("own_params", &self.own_params)
141            .field("param_def_id_to_index", &stabilized_hashmap)
142            .field("has_self", &self.has_self)
143            .field("has_late_bound_regions", &self.has_late_bound_regions)
144            .finish()
145    }
146}
147
148impl<'tcx> rustc_type_ir::inherent::GenericsOf<TyCtxt<'tcx>> for &'tcx Generics {
149    fn count(&self) -> usize {
150        self.parent_count + self.own_params.len()
151    }
152}
153
154impl<'tcx> Generics {
155    /// Looks through the generics and all parents to find the index of the
156    /// given param def-id. This is in comparison to the `param_def_id_to_index`
157    /// struct member, which only stores information about this item's own
158    /// generics.
159    pub fn param_def_id_to_index(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<u32> {
160        if let Some(idx) = self.param_def_id_to_index.get(&def_id) {
161            Some(*idx)
162        } else if let Some(parent) = self.parent {
163            let parent = tcx.generics_of(parent);
164            parent.param_def_id_to_index(tcx, def_id)
165        } else {
166            None
167        }
168    }
169
170    #[inline]
171    pub fn count(&self) -> usize {
172        self.parent_count + self.own_params.len()
173    }
174
175    pub fn own_counts(&self) -> GenericParamCount {
176        // We could cache this as a property of `GenericParamCount`, but
177        // the aim is to refactor this away entirely eventually and the
178        // presence of this method will be a constant reminder.
179        let mut own_counts = GenericParamCount::default();
180
181        for param in &self.own_params {
182            match param.kind {
183                GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
184                GenericParamDefKind::Type { .. } => own_counts.types += 1,
185                GenericParamDefKind::Const { .. } => own_counts.consts += 1,
186            }
187        }
188
189        own_counts
190    }
191
192    pub fn own_defaults(&self) -> GenericParamCount {
193        let mut own_defaults = GenericParamCount::default();
194
195        for param in &self.own_params {
196            match param.kind {
197                GenericParamDefKind::Lifetime => (),
198                GenericParamDefKind::Type { has_default, .. } => {
199                    own_defaults.types += has_default as usize;
200                }
201                GenericParamDefKind::Const { has_default, .. } => {
202                    own_defaults.consts += has_default as usize;
203                }
204            }
205        }
206
207        own_defaults
208    }
209
210    pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool {
211        if self.own_requires_monomorphization() {
212            return true;
213        }
214
215        if let Some(parent_def_id) = self.parent {
216            let parent = tcx.generics_of(parent_def_id);
217            parent.requires_monomorphization(tcx)
218        } else {
219            false
220        }
221    }
222
223    pub fn own_requires_monomorphization(&self) -> bool {
224        for param in &self.own_params {
225            match param.kind {
226                GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
227                    return true;
228                }
229                GenericParamDefKind::Lifetime => {}
230            }
231        }
232        false
233    }
234
235    /// Returns the `GenericParamDef` with the given index.
236    pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
237        if let Some(index) = param_index.checked_sub(self.parent_count) {
238            &self.own_params[index]
239        } else {
240            tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
241                .param_at(param_index, tcx)
242        }
243    }
244
245    pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
246        if let Some(index) = param_index.checked_sub(self.parent_count) {
247            &self.own_params[..index]
248        } else {
249            tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
250                .params_to(param_index, tcx)
251        }
252    }
253
254    /// Returns the `GenericParamDef` associated with this `EarlyParamRegion`.
255    pub fn region_param(
256        &'tcx self,
257        param: ty::EarlyParamRegion,
258        tcx: TyCtxt<'tcx>,
259    ) -> &'tcx GenericParamDef {
260        let param = self.param_at(param.index as usize, tcx);
261        match param.kind {
262            GenericParamDefKind::Lifetime => param,
263            _ => {
264                crate::util::bug::bug_fmt(format_args!("expected lifetime parameter, but found another generic parameter: {0:#?}",
        param))bug!("expected lifetime parameter, but found another generic parameter: {param:#?}")
265            }
266        }
267    }
268
269    /// Returns the `GenericParamDef` associated with this `ParamTy`.
270    pub fn type_param(&'tcx self, param: ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
271        let param = self.param_at(param.index as usize, tcx);
272        match param.kind {
273            GenericParamDefKind::Type { .. } => param,
274            _ => crate::util::bug::bug_fmt(format_args!("expected type parameter, but found another generic parameter: {0:#?}",
        param))bug!("expected type parameter, but found another generic parameter: {param:#?}"),
275        }
276    }
277
278    /// Returns the `GenericParamDef` associated with this `ParamConst`.
279    pub fn const_param(&'tcx self, param: ParamConst, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
280        let param = self.param_at(param.index as usize, tcx);
281        match param.kind {
282            GenericParamDefKind::Const { .. } => param,
283            _ => crate::util::bug::bug_fmt(format_args!("expected const parameter, but found another generic parameter: {0:#?}",
        param))bug!("expected const parameter, but found another generic parameter: {param:#?}"),
284        }
285    }
286
287    /// Returns `true` if `params` has `impl Trait`.
288    pub fn has_impl_trait(&'tcx self) -> bool {
289        self.own_params.iter().any(|param| {
290            #[allow(non_exhaustive_omitted_patterns)] match param.kind {
    ty::GenericParamDefKind::Type { synthetic: true, .. } => true,
    _ => false,
}matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. })
291        })
292    }
293
294    pub fn own_synthetic_params_count(&'tcx self) -> usize {
295        self.own_params.iter().filter(|p| p.kind.is_synthetic()).count()
296    }
297
298    /// Returns the args corresponding to the generic parameters
299    /// of this item, excluding `Self`.
300    ///
301    /// **This should only be used for diagnostics purposes.**
302    pub fn own_args_no_defaults<'a>(
303        &'tcx self,
304        tcx: TyCtxt<'tcx>,
305        args: &'a [ty::GenericArg<'tcx>],
306    ) -> &'a [ty::GenericArg<'tcx>] {
307        let mut own_params = self.parent_count..self.count();
308        if self.has_own_self() {
309            own_params.start = 1;
310        }
311
312        // Filter the default arguments.
313        //
314        // This currently uses structural equality instead
315        // of semantic equivalence. While not ideal, that's
316        // good enough for now as this should only be used
317        // for diagnostics anyways.
318        own_params.end -= self
319            .own_params
320            .iter()
321            .rev()
322            .take_while(|param| {
323                param.default_value(tcx).is_some_and(|default| {
324                    default.instantiate(tcx, args).skip_norm_wip() == args[param.index as usize]
325                })
326            })
327            .count();
328
329        &args[own_params]
330    }
331
332    /// Returns the args corresponding to the generic parameters of this item, excluding `Self`.
333    ///
334    /// **This should only be used for diagnostics purposes.**
335    pub fn own_args(
336        &'tcx self,
337        args: &'tcx [ty::GenericArg<'tcx>],
338    ) -> &'tcx [ty::GenericArg<'tcx>] {
339        let own = &args[self.parent_count..][..self.own_params.len()];
340        if self.has_own_self() { &own[1..] } else { own }
341    }
342
343    /// Returns true if a concrete type is specified after a default type.
344    /// For example, consider `struct T<W = usize, X = Vec<W>>(W, X)`
345    /// `T<usize, String>` will return true
346    /// `T<usize>` will return false
347    pub fn check_concrete_type_after_default(
348        &'tcx self,
349        tcx: TyCtxt<'tcx>,
350        args: &'tcx [ty::GenericArg<'tcx>],
351    ) -> bool {
352        let mut default_param_seen = false;
353        for param in self.own_params.iter() {
354            if let Some(inst) = param
355                .default_value(tcx)
356                .map(|default| default.instantiate(tcx, args).skip_norm_wip())
357            {
358                if inst == args[param.index as usize] {
359                    default_param_seen = true;
360                } else if default_param_seen {
361                    return true;
362                }
363            }
364        }
365        false
366    }
367
368    pub fn is_empty(&'tcx self) -> bool {
369        self.count() == 0
370    }
371
372    pub fn is_own_empty(&'tcx self) -> bool {
373        self.own_params.is_empty()
374    }
375
376    pub fn has_own_self(&'tcx self) -> bool {
377        self.has_self && self.parent.is_none()
378    }
379}
380
381/// Bounds on generics.
382#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for GenericPredicates<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for GenericPredicates<'tcx> {
    #[inline]
    fn clone(&self) -> GenericPredicates<'tcx> {
        let _: ::core::clone::AssertParamIsClone<Option<DefId>>;
        let _:
                ::core::clone::AssertParamIsClone<&'tcx [(Clause<'tcx>,
                Span)]>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::default::Default for GenericPredicates<'tcx> {
    #[inline]
    fn default() -> GenericPredicates<'tcx> {
        GenericPredicates {
            parent: ::core::default::Default::default(),
            predicates: ::core::default::Default::default(),
        }
    }
}Default, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for GenericPredicates<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "GenericPredicates", "parent", &self.parent, "predicates",
            &&self.predicates)
    }
}Debug, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for GenericPredicates<'tcx> {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    GenericPredicates {
                        parent: ref __binding_0, predicates: __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for GenericPredicates<'tcx> {
            fn decode(__decoder: &mut __D) -> Self {
                GenericPredicates {
                    parent: ::rustc_serialize::Decodable::decode(__decoder),
                    predicates: ::rustc_middle::ty::codec::RefDecodable::decode(__decoder),
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
            GenericPredicates<'tcx> {
            #[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 {
                    GenericPredicates {
                        parent: ref __binding_0, predicates: ref __binding_1 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
383pub struct GenericPredicates<'tcx> {
384    pub parent: Option<DefId>,
385    pub predicates: &'tcx [(Clause<'tcx>, Span)],
386}
387
388impl<'tcx> GenericPredicates<'tcx> {
389    pub fn instantiate(
390        self,
391        tcx: TyCtxt<'tcx>,
392        args: GenericArgsRef<'tcx>,
393    ) -> InstantiatedPredicates<'tcx> {
394        let mut instantiated = InstantiatedPredicates::empty();
395        self.instantiate_into(tcx, &mut instantiated, args);
396        instantiated
397    }
398
399    pub fn instantiate_own(
400        self,
401        tcx: TyCtxt<'tcx>,
402        args: GenericArgsRef<'tcx>,
403    ) -> impl Iterator<Item = (Unnormalized<'tcx, Clause<'tcx>>, Span)>
404    + DoubleEndedIterator
405    + ExactSizeIterator {
406        EarlyBinder::bind(self.predicates).iter_instantiated_copied(tcx, args).map(|u| {
407            let (clause, span) = u.unzip();
408            (clause, span.skip_normalization())
409        })
410    }
411
412    pub fn instantiate_own_identity(
413        self,
414    ) -> impl Iterator<Item = (Unnormalized<'tcx, Clause<'tcx>>, Span)>
415    + DoubleEndedIterator
416    + ExactSizeIterator {
417        EarlyBinder::bind(self.predicates).iter_identity_copied().map(|u| {
418            let (clause, span) = u.unzip();
419            (clause, span.skip_normalization())
420        })
421    }
422
423    #[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("instantiate_into",
                                    "rustc_middle::ty::generics", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/ty/generics.rs"),
                                    ::tracing_core::__macro_support::Option::Some(423u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_middle::ty::generics"),
                                    ::tracing_core::field::FieldSet::new(&["instantiated",
                                                    "args"], ::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};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&instantiated)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&args)
                                                            as &dyn 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: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if let Some(def_id) = self.parent {
                tcx.predicates_of(def_id).instantiate_into(tcx, instantiated,
                    args);
            }
            instantiated.predicates.extend(self.predicates.iter().map(|(p, _)|
                        EarlyBinder::bind(*p).instantiate(tcx, args)));
            instantiated.spans.extend(self.predicates.iter().map(|(_, sp)|
                        *sp));
        }
    }
}#[instrument(level = "debug", skip(self, tcx))]
424    fn instantiate_into(
425        self,
426        tcx: TyCtxt<'tcx>,
427        instantiated: &mut InstantiatedPredicates<'tcx>,
428        args: GenericArgsRef<'tcx>,
429    ) {
430        if let Some(def_id) = self.parent {
431            tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, args);
432        }
433        instantiated.predicates.extend(
434            self.predicates.iter().map(|(p, _)| EarlyBinder::bind(*p).instantiate(tcx, args)),
435        );
436        instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp));
437    }
438
439    pub fn instantiate_identity(self, tcx: TyCtxt<'tcx>) -> InstantiatedPredicates<'tcx> {
440        let mut instantiated = InstantiatedPredicates::empty();
441        self.instantiate_identity_into(tcx, &mut instantiated);
442        instantiated
443    }
444
445    fn instantiate_identity_into(
446        self,
447        tcx: TyCtxt<'tcx>,
448        instantiated: &mut InstantiatedPredicates<'tcx>,
449    ) {
450        if let Some(def_id) = self.parent {
451            tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated);
452        }
453        instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| Unnormalized::new(*p)));
454        instantiated.spans.extend(self.predicates.iter().map(|(_, s)| s));
455    }
456}
457
458/// `[const]` bounds for a given item. This is represented using a struct much like
459/// `GenericPredicates`, where you can either choose to only instantiate the "own"
460/// bounds or all of the bounds including those from the parent. This distinction
461/// is necessary for code like `compare_method_predicate_entailment`.
462#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for ConstConditions<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for ConstConditions<'tcx> {
    #[inline]
    fn clone(&self) -> ConstConditions<'tcx> {
        let _: ::core::clone::AssertParamIsClone<Option<DefId>>;
        let _:
                ::core::clone::AssertParamIsClone<&'tcx [(ty::PolyTraitRef<'tcx>,
                Span)]>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::default::Default for ConstConditions<'tcx> {
    #[inline]
    fn default() -> ConstConditions<'tcx> {
        ConstConditions {
            parent: ::core::default::Default::default(),
            predicates: ::core::default::Default::default(),
        }
    }
}Default, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ConstConditions<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "ConstConditions", "parent", &self.parent, "predicates",
            &&self.predicates)
    }
}Debug, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for ConstConditions<'tcx> {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    ConstConditions {
                        parent: ref __binding_0, predicates: __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for ConstConditions<'tcx> {
            fn decode(__decoder: &mut __D) -> Self {
                ConstConditions {
                    parent: ::rustc_serialize::Decodable::decode(__decoder),
                    predicates: ::rustc_middle::ty::codec::RefDecodable::decode(__decoder),
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
            ConstConditions<'tcx> {
            #[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 {
                    ConstConditions {
                        parent: ref __binding_0, predicates: ref __binding_1 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
463pub struct ConstConditions<'tcx> {
464    pub parent: Option<DefId>,
465    pub predicates: &'tcx [(ty::PolyTraitRef<'tcx>, Span)],
466}
467
468impl<'tcx> ConstConditions<'tcx> {
469    pub fn instantiate(
470        self,
471        tcx: TyCtxt<'tcx>,
472        args: GenericArgsRef<'tcx>,
473    ) -> Vec<(Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)> {
474        let mut instantiated = ::alloc::vec::Vec::new()vec![];
475        self.instantiate_into(tcx, &mut instantiated, args);
476        instantiated
477    }
478
479    pub fn instantiate_own(
480        self,
481        tcx: TyCtxt<'tcx>,
482        args: GenericArgsRef<'tcx>,
483    ) -> impl Iterator<Item = (Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)>
484    + DoubleEndedIterator
485    + ExactSizeIterator {
486        EarlyBinder::bind(self.predicates).iter_instantiated_copied(tcx, args).map(|u| {
487            let (trait_ref, span) = u.unzip();
488            (trait_ref, span.skip_normalization())
489        })
490    }
491
492    pub fn instantiate_own_identity(
493        self,
494    ) -> impl Iterator<Item = (Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)>
495    + DoubleEndedIterator
496    + ExactSizeIterator {
497        EarlyBinder::bind(self.predicates).iter_identity_copied().map(|u| {
498            let (trait_ref, span) = u.unzip();
499            (trait_ref, span.skip_normalization())
500        })
501    }
502
503    #[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("instantiate_into",
                                    "rustc_middle::ty::generics", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/ty/generics.rs"),
                                    ::tracing_core::__macro_support::Option::Some(503u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_middle::ty::generics"),
                                    ::tracing_core::field::FieldSet::new(&["instantiated",
                                                    "args"], ::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};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&instantiated)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&args)
                                                            as &dyn 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: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if let Some(def_id) = self.parent {
                tcx.const_conditions(def_id).instantiate_into(tcx,
                    instantiated, args);
            }
            instantiated.extend(self.predicates.iter().map(|&(p, s)|
                        (EarlyBinder::bind(p).instantiate(tcx, args), s)));
        }
    }
}#[instrument(level = "debug", skip(self, tcx))]
504    fn instantiate_into(
505        self,
506        tcx: TyCtxt<'tcx>,
507        instantiated: &mut Vec<(Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)>,
508        args: GenericArgsRef<'tcx>,
509    ) {
510        if let Some(def_id) = self.parent {
511            tcx.const_conditions(def_id).instantiate_into(tcx, instantiated, args);
512        }
513        instantiated.extend(
514            self.predicates.iter().map(|&(p, s)| (EarlyBinder::bind(p).instantiate(tcx, args), s)),
515        );
516    }
517
518    pub fn instantiate_identity(
519        self,
520        tcx: TyCtxt<'tcx>,
521    ) -> Vec<(Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)> {
522        let mut instantiated = ::alloc::vec::Vec::new()vec![];
523        self.instantiate_identity_into(tcx, &mut instantiated);
524        instantiated
525    }
526
527    fn instantiate_identity_into(
528        self,
529        tcx: TyCtxt<'tcx>,
530        instantiated: &mut Vec<(Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)>,
531    ) {
532        if let Some(def_id) = self.parent {
533            tcx.const_conditions(def_id).instantiate_identity_into(tcx, instantiated);
534        }
535        instantiated.extend(
536            self.predicates
537                .iter()
538                .copied()
539                .map(|(trait_ref, span)| (Unnormalized::new(trait_ref), span)),
540        );
541    }
542}