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::{HashStable, TyDecodable, TyEncodable};
5use rustc_span::{Span, Symbol, kw};
6use tracing::instrument;
7
8use super::{Clause, InstantiatedPredicates, ParamConst, ParamTy, Ty, TyCtxt};
9use crate::ty;
10use crate::ty::{EarlyBinder, GenericArgsRef};
11
12#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
13pub enum GenericParamDefKind {
14    Lifetime,
15    Type { has_default: bool, synthetic: bool },
16    Const { has_default: bool, synthetic: 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(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
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            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(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(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
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_hasher(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<'tcx> rustc_type_ir::inherent::GenericsOf<TyCtxt<'tcx>> for &'tcx Generics {
132    fn count(&self) -> usize {
133        self.parent_count + self.own_params.len()
134    }
135}
136
137impl<'tcx> Generics {
138    /// Looks through the generics and all parents to find the index of the
139    /// given param def-id. This is in comparison to the `param_def_id_to_index`
140    /// struct member, which only stores information about this item's own
141    /// generics.
142    pub fn param_def_id_to_index(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<u32> {
143        if let Some(idx) = self.param_def_id_to_index.get(&def_id) {
144            Some(*idx)
145        } else if let Some(parent) = self.parent {
146            let parent = tcx.generics_of(parent);
147            parent.param_def_id_to_index(tcx, def_id)
148        } else {
149            None
150        }
151    }
152
153    #[inline]
154    pub fn count(&self) -> usize {
155        self.parent_count + self.own_params.len()
156    }
157
158    pub fn own_counts(&self) -> GenericParamCount {
159        // We could cache this as a property of `GenericParamCount`, but
160        // the aim is to refactor this away entirely eventually and the
161        // presence of this method will be a constant reminder.
162        let mut own_counts = GenericParamCount::default();
163
164        for param in &self.own_params {
165            match param.kind {
166                GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
167                GenericParamDefKind::Type { .. } => own_counts.types += 1,
168                GenericParamDefKind::Const { .. } => own_counts.consts += 1,
169            }
170        }
171
172        own_counts
173    }
174
175    pub fn own_defaults(&self) -> GenericParamCount {
176        let mut own_defaults = GenericParamCount::default();
177
178        for param in &self.own_params {
179            match param.kind {
180                GenericParamDefKind::Lifetime => (),
181                GenericParamDefKind::Type { has_default, .. } => {
182                    own_defaults.types += has_default as usize;
183                }
184                GenericParamDefKind::Const { has_default, .. } => {
185                    own_defaults.consts += has_default as usize;
186                }
187            }
188        }
189
190        own_defaults
191    }
192
193    pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool {
194        if self.own_requires_monomorphization() {
195            return true;
196        }
197
198        if let Some(parent_def_id) = self.parent {
199            let parent = tcx.generics_of(parent_def_id);
200            parent.requires_monomorphization(tcx)
201        } else {
202            false
203        }
204    }
205
206    pub fn own_requires_monomorphization(&self) -> bool {
207        for param in &self.own_params {
208            match param.kind {
209                GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
210                    return true;
211                }
212                GenericParamDefKind::Lifetime => {}
213            }
214        }
215        false
216    }
217
218    /// Returns the `GenericParamDef` with the given index.
219    pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
220        if let Some(index) = param_index.checked_sub(self.parent_count) {
221            &self.own_params[index]
222        } else {
223            tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
224                .param_at(param_index, tcx)
225        }
226    }
227
228    pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
229        if let Some(index) = param_index.checked_sub(self.parent_count) {
230            &self.own_params[..index]
231        } else {
232            tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
233                .params_to(param_index, tcx)
234        }
235    }
236
237    /// Returns the `GenericParamDef` associated with this `EarlyParamRegion`.
238    pub fn region_param(
239        &'tcx self,
240        param: ty::EarlyParamRegion,
241        tcx: TyCtxt<'tcx>,
242    ) -> &'tcx GenericParamDef {
243        let param = self.param_at(param.index as usize, tcx);
244        match param.kind {
245            GenericParamDefKind::Lifetime => param,
246            _ => {
247                bug!("expected lifetime parameter, but found another generic parameter: {param:#?}")
248            }
249        }
250    }
251
252    /// Returns the `GenericParamDef` associated with this `ParamTy`.
253    pub fn type_param(&'tcx self, param: ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
254        let param = self.param_at(param.index as usize, tcx);
255        match param.kind {
256            GenericParamDefKind::Type { .. } => param,
257            _ => bug!("expected type parameter, but found another generic parameter: {param:#?}"),
258        }
259    }
260
261    /// Returns the `GenericParamDef` associated with this `ParamConst`.
262    pub fn const_param(&'tcx self, param: ParamConst, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
263        let param = self.param_at(param.index as usize, tcx);
264        match param.kind {
265            GenericParamDefKind::Const { .. } => param,
266            _ => bug!("expected const parameter, but found another generic parameter: {param:#?}"),
267        }
268    }
269
270    /// Returns `true` if `params` has `impl Trait`.
271    pub fn has_impl_trait(&'tcx self) -> bool {
272        self.own_params.iter().any(|param| {
273            matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. })
274        })
275    }
276
277    /// Returns the args corresponding to the generic parameters
278    /// of this item, excluding `Self`.
279    ///
280    /// **This should only be used for diagnostics purposes.**
281    pub fn own_args_no_defaults<'a>(
282        &'tcx self,
283        tcx: TyCtxt<'tcx>,
284        args: &'a [ty::GenericArg<'tcx>],
285    ) -> &'a [ty::GenericArg<'tcx>] {
286        let mut own_params = self.parent_count..self.count();
287        if self.has_self && self.parent.is_none() {
288            own_params.start = 1;
289        }
290
291        // Filter the default arguments.
292        //
293        // This currently uses structural equality instead
294        // of semantic equivalence. While not ideal, that's
295        // good enough for now as this should only be used
296        // for diagnostics anyways.
297        own_params.end -= self
298            .own_params
299            .iter()
300            .rev()
301            .take_while(|param| {
302                param.default_value(tcx).is_some_and(|default| {
303                    default.instantiate(tcx, args) == args[param.index as usize]
304                })
305            })
306            .count();
307
308        &args[own_params]
309    }
310
311    /// Returns the args corresponding to the generic parameters of this item, excluding `Self`.
312    ///
313    /// **This should only be used for diagnostics purposes.**
314    pub fn own_args(
315        &'tcx self,
316        args: &'tcx [ty::GenericArg<'tcx>],
317    ) -> &'tcx [ty::GenericArg<'tcx>] {
318        let own = &args[self.parent_count..][..self.own_params.len()];
319        if self.has_self && self.parent.is_none() { &own[1..] } else { own }
320    }
321
322    /// Returns true if a concrete type is specified after a default type.
323    /// For example, consider `struct T<W = usize, X = Vec<W>>(W, X)`
324    /// `T<usize, String>` will return true
325    /// `T<usize>` will return false
326    pub fn check_concrete_type_after_default(
327        &'tcx self,
328        tcx: TyCtxt<'tcx>,
329        args: &'tcx [ty::GenericArg<'tcx>],
330    ) -> bool {
331        let mut default_param_seen = false;
332        for param in self.own_params.iter() {
333            if let Some(inst) =
334                param.default_value(tcx).map(|default| default.instantiate(tcx, args))
335            {
336                if inst == args[param.index as usize] {
337                    default_param_seen = true;
338                } else if default_param_seen {
339                    return true;
340                }
341            }
342        }
343        false
344    }
345
346    pub fn is_empty(&'tcx self) -> bool {
347        self.count() == 0
348    }
349
350    pub fn is_own_empty(&'tcx self) -> bool {
351        self.own_params.is_empty()
352    }
353}
354
355/// Bounds on generics.
356#[derive(Copy, Clone, Default, Debug, TyEncodable, TyDecodable, HashStable)]
357pub struct GenericPredicates<'tcx> {
358    pub parent: Option<DefId>,
359    pub predicates: &'tcx [(Clause<'tcx>, Span)],
360}
361
362impl<'tcx> GenericPredicates<'tcx> {
363    pub fn instantiate(
364        self,
365        tcx: TyCtxt<'tcx>,
366        args: GenericArgsRef<'tcx>,
367    ) -> InstantiatedPredicates<'tcx> {
368        let mut instantiated = InstantiatedPredicates::empty();
369        self.instantiate_into(tcx, &mut instantiated, args);
370        instantiated
371    }
372
373    pub fn instantiate_own(
374        self,
375        tcx: TyCtxt<'tcx>,
376        args: GenericArgsRef<'tcx>,
377    ) -> impl Iterator<Item = (Clause<'tcx>, Span)> + DoubleEndedIterator + ExactSizeIterator {
378        EarlyBinder::bind(self.predicates).iter_instantiated_copied(tcx, args)
379    }
380
381    pub fn instantiate_own_identity(
382        self,
383    ) -> impl Iterator<Item = (Clause<'tcx>, Span)> + DoubleEndedIterator + ExactSizeIterator {
384        EarlyBinder::bind(self.predicates).iter_identity_copied()
385    }
386
387    #[instrument(level = "debug", skip(self, tcx))]
388    fn instantiate_into(
389        self,
390        tcx: TyCtxt<'tcx>,
391        instantiated: &mut InstantiatedPredicates<'tcx>,
392        args: GenericArgsRef<'tcx>,
393    ) {
394        if let Some(def_id) = self.parent {
395            tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, args);
396        }
397        instantiated.predicates.extend(
398            self.predicates.iter().map(|(p, _)| EarlyBinder::bind(*p).instantiate(tcx, args)),
399        );
400        instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp));
401    }
402
403    pub fn instantiate_identity(self, tcx: TyCtxt<'tcx>) -> InstantiatedPredicates<'tcx> {
404        let mut instantiated = InstantiatedPredicates::empty();
405        self.instantiate_identity_into(tcx, &mut instantiated);
406        instantiated
407    }
408
409    fn instantiate_identity_into(
410        self,
411        tcx: TyCtxt<'tcx>,
412        instantiated: &mut InstantiatedPredicates<'tcx>,
413    ) {
414        if let Some(def_id) = self.parent {
415            tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated);
416        }
417        instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p));
418        instantiated.spans.extend(self.predicates.iter().map(|(_, s)| s));
419    }
420}
421
422/// `~const` bounds for a given item. This is represented using a struct much like
423/// `GenericPredicates`, where you can either choose to only instantiate the "own"
424/// bounds or all of the bounds including those from the parent. This distinction
425/// is necessary for code like `compare_method_predicate_entailment`.
426#[derive(Copy, Clone, Default, Debug, TyEncodable, TyDecodable, HashStable)]
427pub struct ConstConditions<'tcx> {
428    pub parent: Option<DefId>,
429    pub predicates: &'tcx [(ty::PolyTraitRef<'tcx>, Span)],
430}
431
432impl<'tcx> ConstConditions<'tcx> {
433    pub fn instantiate(
434        self,
435        tcx: TyCtxt<'tcx>,
436        args: GenericArgsRef<'tcx>,
437    ) -> Vec<(ty::PolyTraitRef<'tcx>, Span)> {
438        let mut instantiated = vec![];
439        self.instantiate_into(tcx, &mut instantiated, args);
440        instantiated
441    }
442
443    pub fn instantiate_own(
444        self,
445        tcx: TyCtxt<'tcx>,
446        args: GenericArgsRef<'tcx>,
447    ) -> impl Iterator<Item = (ty::PolyTraitRef<'tcx>, Span)> + DoubleEndedIterator + ExactSizeIterator
448    {
449        EarlyBinder::bind(self.predicates).iter_instantiated_copied(tcx, args)
450    }
451
452    pub fn instantiate_own_identity(
453        self,
454    ) -> impl Iterator<Item = (ty::PolyTraitRef<'tcx>, Span)> + DoubleEndedIterator + ExactSizeIterator
455    {
456        EarlyBinder::bind(self.predicates).iter_identity_copied()
457    }
458
459    #[instrument(level = "debug", skip(self, tcx))]
460    fn instantiate_into(
461        self,
462        tcx: TyCtxt<'tcx>,
463        instantiated: &mut Vec<(ty::PolyTraitRef<'tcx>, Span)>,
464        args: GenericArgsRef<'tcx>,
465    ) {
466        if let Some(def_id) = self.parent {
467            tcx.const_conditions(def_id).instantiate_into(tcx, instantiated, args);
468        }
469        instantiated.extend(
470            self.predicates.iter().map(|&(p, s)| (EarlyBinder::bind(p).instantiate(tcx, args), s)),
471        );
472    }
473
474    pub fn instantiate_identity(self, tcx: TyCtxt<'tcx>) -> Vec<(ty::PolyTraitRef<'tcx>, Span)> {
475        let mut instantiated = vec![];
476        self.instantiate_identity_into(tcx, &mut instantiated);
477        instantiated
478    }
479
480    fn instantiate_identity_into(
481        self,
482        tcx: TyCtxt<'tcx>,
483        instantiated: &mut Vec<(ty::PolyTraitRef<'tcx>, Span)>,
484    ) {
485        if let Some(def_id) = self.parent {
486            tcx.const_conditions(def_id).instantiate_identity_into(tcx, instantiated);
487        }
488        instantiated.extend(self.predicates.iter().copied());
489    }
490}