rustc_hir_analysis/collect/
generics_of.rs

1use std::assert_matches::assert_matches;
2use std::ops::ControlFlow;
3
4use hir::intravisit::{self, Visitor};
5use hir::{GenericParamKind, HirId, Node};
6use rustc_hir::def::DefKind;
7use rustc_hir::def_id::LocalDefId;
8use rustc_hir::intravisit::VisitorExt;
9use rustc_hir::{self as hir, AmbigArg};
10use rustc_middle::ty::{self, TyCtxt};
11use rustc_session::lint;
12use rustc_span::{Span, Symbol, kw};
13use tracing::{debug, instrument};
14
15use crate::delegation::inherit_generics_for_delegation_item;
16use crate::middle::resolve_bound_vars as rbv;
17
18#[instrument(level = "debug", skip(tcx), ret)]
19pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
20    use rustc_hir::*;
21
22    // For an RPITIT, synthesize generics which are equal to the opaque's generics
23    // and parent fn's generics compressed into one list.
24    if let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id }) =
25        tcx.opt_rpitit_info(def_id.to_def_id())
26    {
27        debug!("RPITIT fn_def_id={fn_def_id:?} opaque_def_id={opaque_def_id:?}");
28        let trait_def_id = tcx.parent(fn_def_id);
29        let opaque_ty_generics = tcx.generics_of(opaque_def_id);
30        let opaque_ty_parent_count = opaque_ty_generics.parent_count;
31        let mut own_params = opaque_ty_generics.own_params.clone();
32
33        let parent_generics = tcx.generics_of(trait_def_id);
34        let parent_count = parent_generics.parent_count + parent_generics.own_params.len();
35
36        let mut trait_fn_params = tcx.generics_of(fn_def_id).own_params.clone();
37
38        for param in &mut own_params {
39            param.index = param.index + parent_count as u32 + trait_fn_params.len() as u32
40                - opaque_ty_parent_count as u32;
41        }
42
43        trait_fn_params.extend(own_params);
44        own_params = trait_fn_params;
45
46        let param_def_id_to_index =
47            own_params.iter().map(|param| (param.def_id, param.index)).collect();
48
49        return ty::Generics {
50            parent: Some(trait_def_id),
51            parent_count,
52            own_params,
53            param_def_id_to_index,
54            has_self: opaque_ty_generics.has_self,
55            has_late_bound_regions: opaque_ty_generics.has_late_bound_regions,
56        };
57    }
58
59    let hir_id = tcx.local_def_id_to_hir_id(def_id);
60
61    let node = tcx.hir_node(hir_id);
62    if let Some(sig) = node.fn_sig()
63        && let Some(sig_id) = sig.decl.opt_delegation_sig_id()
64    {
65        return inherit_generics_for_delegation_item(tcx, def_id, sig_id);
66    }
67
68    let parent_def_id = match node {
69        Node::ImplItem(_)
70        | Node::TraitItem(_)
71        | Node::Variant(_)
72        | Node::Ctor(..)
73        | Node::Field(_) => {
74            let parent_id = tcx.hir_get_parent_item(hir_id);
75            Some(parent_id.to_def_id())
76        }
77        // FIXME(#43408) always enable this once `lazy_normalization` is
78        // stable enough and does not need a feature gate anymore.
79        Node::AnonConst(_) => {
80            let parent_did = tcx.parent(def_id.to_def_id());
81
82            // We don't do this unconditionally because the `DefId` parent of an anon const
83            // might be an implicitly created closure during `async fn` desugaring. This would
84            // have the wrong generics.
85            //
86            // i.e. `async fn foo<'a>() { let a = [(); { 1 + 2 }]; bar().await() }`
87            // would implicitly have a closure in its body that would be the parent of
88            // the `{ 1 + 2 }` anon const. This closure's generics is simply a witness
89            // instead of `['a]`.
90            let parent_did = if let DefKind::AnonConst = tcx.def_kind(parent_did) {
91                parent_did
92            } else {
93                tcx.hir_get_parent_item(hir_id).to_def_id()
94            };
95            debug!(?parent_did);
96
97            let mut in_param_ty = false;
98            for (_parent, node) in tcx.hir_parent_iter(hir_id) {
99                if let Some(generics) = node.generics() {
100                    let mut visitor = AnonConstInParamTyDetector { in_param_ty: false, ct: hir_id };
101
102                    in_param_ty = visitor.visit_generics(generics).is_break();
103                    break;
104                }
105            }
106
107            if in_param_ty {
108                // We do not allow generic parameters in anon consts if we are inside
109                // of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
110                None
111            } else if tcx.features().generic_const_exprs() {
112                let parent_node = tcx.parent_hir_node(hir_id);
113                debug!(?parent_node);
114                if let Node::Variant(Variant { disr_expr: Some(constant), .. }) = parent_node
115                    && constant.hir_id == hir_id
116                {
117                    // enum variant discriminants are not allowed to use any kind of generics
118                    None
119                } else if let Some(param_id) = tcx.hir_opt_const_param_default_param_def_id(hir_id)
120                {
121                    // If the def_id we are calling generics_of on is an anon ct default i.e:
122                    //
123                    // struct Foo<const N: usize = { .. }>;
124                    //        ^^^       ^          ^^^^^^ def id of this anon const
125                    //        ^         ^ param_id
126                    //        ^ parent_def_id
127                    //
128                    // then we only want to return generics for params to the left of `N`. If we don't do that we
129                    // end up with that const looking like: `ty::ConstKind::Unevaluated(def_id, args: [N#0])`.
130                    //
131                    // This causes ICEs (#86580) when building the args for Foo in `fn foo() -> Foo { .. }` as
132                    // we instantiate the defaults with the partially built args when we build the args. Instantiating
133                    // the `N#0` on the unevaluated const indexes into the empty args we're in the process of building.
134                    //
135                    // We fix this by having this function return the parent's generics ourselves and truncating the
136                    // generics to only include non-forward declared params (with the exception of the `Self` ty)
137                    //
138                    // For the above code example that means we want `args: []`
139                    // For the following struct def we want `args: [N#0]` when generics_of is called on
140                    // the def id of the `{ N + 1 }` anon const
141                    // struct Foo<const N: usize, const M: usize = { N + 1 }>;
142                    //
143                    // This has some implications for how we get the predicates available to the anon const
144                    // see `explicit_predicates_of` for more information on this
145                    let generics = tcx.generics_of(parent_did);
146                    let param_def_idx = generics.param_def_id_to_index[&param_id.to_def_id()];
147                    // In the above example this would be .params[..N#0]
148                    let own_params = generics.params_to(param_def_idx as usize, tcx).to_owned();
149                    let param_def_id_to_index =
150                        own_params.iter().map(|param| (param.def_id, param.index)).collect();
151
152                    return ty::Generics {
153                        // we set the parent of these generics to be our parent's parent so that we
154                        // dont end up with args: [N, M, N] for the const default on a struct like this:
155                        // struct Foo<const N: usize, const M: usize = { ... }>;
156                        parent: generics.parent,
157                        parent_count: generics.parent_count,
158                        own_params,
159                        param_def_id_to_index,
160                        has_self: generics.has_self,
161                        has_late_bound_regions: generics.has_late_bound_regions,
162                    };
163                } else {
164                    // HACK(eddyb) this provides the correct generics when
165                    // `feature(generic_const_expressions)` is enabled, so that const expressions
166                    // used with const generics, e.g. `Foo<{N+1}>`, can work at all.
167                    //
168                    // Note that we do not supply the parent generics when using
169                    // `min_const_generics`.
170                    Some(parent_did)
171                }
172            } else {
173                let parent_node = tcx.parent_hir_node(hir_id);
174                let parent_node = match parent_node {
175                    Node::ConstArg(ca) => tcx.parent_hir_node(ca.hir_id),
176                    _ => parent_node,
177                };
178                match parent_node {
179                    // HACK(eddyb) this provides the correct generics for repeat
180                    // expressions' count (i.e. `N` in `[x; N]`), and explicit
181                    // `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`),
182                    // as they shouldn't be able to cause query cycle errors.
183                    Node::Expr(Expr { kind: ExprKind::Repeat(_, ct), .. })
184                        if ct.anon_const_hir_id() == Some(hir_id) =>
185                    {
186                        Some(parent_did)
187                    }
188                    Node::TyPat(_) => Some(parent_did),
189                    // Field default values inherit the ADT's generics.
190                    Node::Field(_) => Some(parent_did),
191                    _ => None,
192                }
193            }
194        }
195        Node::ConstBlock(_)
196        | Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
197            Some(tcx.typeck_root_def_id(def_id.to_def_id()))
198        }
199        Node::OpaqueTy(&hir::OpaqueTy {
200            origin:
201                hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, in_trait_or_impl }
202                | hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, in_trait_or_impl },
203            ..
204        }) => {
205            if in_trait_or_impl.is_some() {
206                assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn);
207            } else {
208                assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn);
209            }
210            Some(fn_def_id.to_def_id())
211        }
212        Node::OpaqueTy(&hir::OpaqueTy {
213            origin: hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty },
214            ..
215        }) => {
216            if in_assoc_ty {
217                assert_matches!(tcx.def_kind(parent), DefKind::AssocTy);
218            } else {
219                assert_matches!(tcx.def_kind(parent), DefKind::TyAlias);
220            }
221            debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent);
222            // Opaque types are always nested within another item, and
223            // inherit the generics of the item.
224            Some(parent.to_def_id())
225        }
226        _ => None,
227    };
228
229    enum Defaults {
230        Allowed,
231        // See #36887
232        FutureCompatDisallowed,
233        Deny,
234    }
235
236    let hir_generics = node.generics().unwrap_or(hir::Generics::empty());
237    let (opt_self, allow_defaults) = match node {
238        Node::Item(item) => {
239            match item.kind {
240                ItemKind::Trait(..) | ItemKind::TraitAlias(..) => {
241                    // Add in the self type parameter.
242                    //
243                    // Something of a hack: use the node id for the trait, also as
244                    // the node id for the Self type parameter.
245                    let opt_self = Some(ty::GenericParamDef {
246                        index: 0,
247                        name: kw::SelfUpper,
248                        def_id: def_id.to_def_id(),
249                        pure_wrt_drop: false,
250                        kind: ty::GenericParamDefKind::Type {
251                            has_default: false,
252                            synthetic: false,
253                        },
254                    });
255
256                    (opt_self, Defaults::Allowed)
257                }
258                ItemKind::TyAlias(..)
259                | ItemKind::Enum(..)
260                | ItemKind::Struct(..)
261                | ItemKind::Union(..) => (None, Defaults::Allowed),
262                ItemKind::Const(..) => (None, Defaults::Deny),
263                _ => (None, Defaults::FutureCompatDisallowed),
264            }
265        }
266
267        Node::OpaqueTy(..) => (None, Defaults::Allowed),
268
269        // GATs
270        Node::TraitItem(item) if matches!(item.kind, TraitItemKind::Type(..)) => {
271            (None, Defaults::Deny)
272        }
273        Node::ImplItem(item) if matches!(item.kind, ImplItemKind::Type(..)) => {
274            (None, Defaults::Deny)
275        }
276
277        _ => (None, Defaults::FutureCompatDisallowed),
278    };
279
280    let has_self = opt_self.is_some();
281    let mut parent_has_self = false;
282    let mut own_start = has_self as u32;
283    let parent_count = parent_def_id.map_or(0, |def_id| {
284        let generics = tcx.generics_of(def_id);
285        assert!(!has_self);
286        parent_has_self = generics.has_self;
287        own_start = generics.count() as u32;
288        generics.parent_count + generics.own_params.len()
289    });
290
291    let mut own_params: Vec<_> = Vec::with_capacity(hir_generics.params.len() + has_self as usize);
292
293    if let Some(opt_self) = opt_self {
294        own_params.push(opt_self);
295    }
296
297    let early_lifetimes = super::early_bound_lifetimes_from_generics(tcx, hir_generics);
298    own_params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
299        name: param.name.ident().name,
300        index: own_start + i as u32,
301        def_id: param.def_id.to_def_id(),
302        pure_wrt_drop: param.pure_wrt_drop,
303        kind: ty::GenericParamDefKind::Lifetime,
304    }));
305
306    // Now create the real type and const parameters.
307    let type_start = own_start - has_self as u32 + own_params.len() as u32;
308    let mut i: u32 = 0;
309    let mut next_index = || {
310        let prev = i;
311        i += 1;
312        prev + type_start
313    };
314
315    const TYPE_DEFAULT_NOT_ALLOWED: &'static str = "defaults for type parameters are only allowed in \
316    `struct`, `enum`, `type`, or `trait` definitions";
317
318    own_params.extend(hir_generics.params.iter().filter_map(|param| match param.kind {
319        GenericParamKind::Lifetime { .. } => None,
320        GenericParamKind::Type { default, synthetic, .. } => {
321            if default.is_some() {
322                match allow_defaults {
323                    Defaults::Allowed => {}
324                    Defaults::FutureCompatDisallowed => {
325                        tcx.node_span_lint(
326                            lint::builtin::INVALID_TYPE_PARAM_DEFAULT,
327                            param.hir_id,
328                            param.span,
329                            |lint| {
330                                lint.primary_message(TYPE_DEFAULT_NOT_ALLOWED);
331                            },
332                        );
333                    }
334                    Defaults::Deny => {
335                        tcx.dcx().span_err(param.span, TYPE_DEFAULT_NOT_ALLOWED);
336                    }
337                }
338            }
339
340            let kind = ty::GenericParamDefKind::Type { has_default: default.is_some(), synthetic };
341
342            Some(ty::GenericParamDef {
343                index: next_index(),
344                name: param.name.ident().name,
345                def_id: param.def_id.to_def_id(),
346                pure_wrt_drop: param.pure_wrt_drop,
347                kind,
348            })
349        }
350        GenericParamKind::Const { ty: _, default, synthetic } => {
351            if !matches!(allow_defaults, Defaults::Allowed) && default.is_some() {
352                tcx.dcx().span_err(
353                    param.span,
354                    "defaults for const parameters are only allowed in \
355                    `struct`, `enum`, `type`, or `trait` definitions",
356                );
357            }
358
359            let index = next_index();
360
361            Some(ty::GenericParamDef {
362                index,
363                name: param.name.ident().name,
364                def_id: param.def_id.to_def_id(),
365                pure_wrt_drop: param.pure_wrt_drop,
366                kind: ty::GenericParamDefKind::Const { has_default: default.is_some(), synthetic },
367            })
368        }
369    }));
370
371    // provide junk type parameter defs - the only place that
372    // cares about anything but the length is instantiation,
373    // and we don't do that for closures.
374    if let Node::Expr(&hir::Expr {
375        kind: hir::ExprKind::Closure(hir::Closure { kind, .. }), ..
376    }) = node
377    {
378        // See `ClosureArgsParts`, `CoroutineArgsParts`, and `CoroutineClosureArgsParts`
379        // for info on the usage of each of these fields.
380        let dummy_args = match kind {
381            ClosureKind::Closure => &["<closure_kind>", "<closure_signature>", "<upvars>"][..],
382            ClosureKind::Coroutine(_) => &[
383                "<coroutine_kind>",
384                "<resume_ty>",
385                "<yield_ty>",
386                "<return_ty>",
387                "<witness>",
388                "<upvars>",
389            ][..],
390            ClosureKind::CoroutineClosure(_) => &[
391                "<closure_kind>",
392                "<closure_signature_parts>",
393                "<upvars>",
394                "<bound_captures_by_ref>",
395                "<witness>",
396            ][..],
397        };
398
399        own_params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
400            index: next_index(),
401            name: Symbol::intern(arg),
402            def_id: def_id.to_def_id(),
403            pure_wrt_drop: false,
404            kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
405        }));
406    }
407
408    // provide junk type parameter defs for const blocks.
409    if let Node::ConstBlock(_) = node {
410        own_params.push(ty::GenericParamDef {
411            index: next_index(),
412            name: rustc_span::sym::const_ty_placeholder,
413            def_id: def_id.to_def_id(),
414            pure_wrt_drop: false,
415            kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
416        });
417    }
418
419    if let Node::OpaqueTy(&hir::OpaqueTy { .. }) = node {
420        assert!(own_params.is_empty());
421
422        let lifetimes = tcx.opaque_captured_lifetimes(def_id);
423        debug!(?lifetimes);
424
425        own_params.extend(lifetimes.iter().map(|&(_, param)| ty::GenericParamDef {
426            name: tcx.item_name(param.to_def_id()),
427            index: next_index(),
428            def_id: param.to_def_id(),
429            pure_wrt_drop: false,
430            kind: ty::GenericParamDefKind::Lifetime,
431        }))
432    }
433
434    let param_def_id_to_index =
435        own_params.iter().map(|param| (param.def_id, param.index)).collect();
436
437    ty::Generics {
438        parent: parent_def_id,
439        parent_count,
440        own_params,
441        param_def_id_to_index,
442        has_self: has_self || parent_has_self,
443        has_late_bound_regions: has_late_bound_regions(tcx, node),
444    }
445}
446
447fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<Span> {
448    struct LateBoundRegionsDetector<'tcx> {
449        tcx: TyCtxt<'tcx>,
450        outer_index: ty::DebruijnIndex,
451    }
452
453    impl<'tcx> Visitor<'tcx> for LateBoundRegionsDetector<'tcx> {
454        type Result = ControlFlow<Span>;
455        fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx, AmbigArg>) -> ControlFlow<Span> {
456            match ty.kind {
457                hir::TyKind::BareFn(..) => {
458                    self.outer_index.shift_in(1);
459                    let res = intravisit::walk_ty(self, ty);
460                    self.outer_index.shift_out(1);
461                    res
462                }
463                hir::TyKind::UnsafeBinder(_) => {
464                    self.outer_index.shift_in(1);
465                    let res = intravisit::walk_ty(self, ty);
466                    self.outer_index.shift_out(1);
467                    res
468                }
469                _ => intravisit::walk_ty(self, ty),
470            }
471        }
472
473        fn visit_poly_trait_ref(&mut self, tr: &'tcx hir::PolyTraitRef<'tcx>) -> ControlFlow<Span> {
474            self.outer_index.shift_in(1);
475            let res = intravisit::walk_poly_trait_ref(self, tr);
476            self.outer_index.shift_out(1);
477            res
478        }
479
480        fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) -> ControlFlow<Span> {
481            match self.tcx.named_bound_var(lt.hir_id) {
482                Some(rbv::ResolvedArg::StaticLifetime | rbv::ResolvedArg::EarlyBound(..)) => {
483                    ControlFlow::Continue(())
484                }
485                Some(rbv::ResolvedArg::LateBound(debruijn, _, _))
486                    if debruijn < self.outer_index =>
487                {
488                    ControlFlow::Continue(())
489                }
490                Some(
491                    rbv::ResolvedArg::LateBound(..)
492                    | rbv::ResolvedArg::Free(..)
493                    | rbv::ResolvedArg::Error(_),
494                )
495                | None => ControlFlow::Break(lt.ident.span),
496            }
497        }
498    }
499
500    fn has_late_bound_regions<'tcx>(
501        tcx: TyCtxt<'tcx>,
502        generics: &'tcx hir::Generics<'tcx>,
503        decl: &'tcx hir::FnDecl<'tcx>,
504    ) -> Option<Span> {
505        let mut visitor = LateBoundRegionsDetector { tcx, outer_index: ty::INNERMOST };
506        for param in generics.params {
507            if let GenericParamKind::Lifetime { .. } = param.kind {
508                if tcx.is_late_bound(param.hir_id) {
509                    return Some(param.span);
510                }
511            }
512        }
513        visitor.visit_fn_decl(decl).break_value()
514    }
515
516    let decl = node.fn_decl()?;
517    let generics = node.generics()?;
518    has_late_bound_regions(tcx, generics, decl)
519}
520
521struct AnonConstInParamTyDetector {
522    in_param_ty: bool,
523    ct: HirId,
524}
525
526impl<'v> Visitor<'v> for AnonConstInParamTyDetector {
527    type Result = ControlFlow<()>;
528
529    fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) -> Self::Result {
530        if let GenericParamKind::Const { ty, default: _, synthetic: _ } = p.kind {
531            let prev = self.in_param_ty;
532            self.in_param_ty = true;
533            let res = self.visit_ty_unambig(ty);
534            self.in_param_ty = prev;
535            res
536        } else {
537            ControlFlow::Continue(())
538        }
539    }
540
541    fn visit_anon_const(&mut self, c: &'v hir::AnonConst) -> Self::Result {
542        if self.in_param_ty && self.ct == c.hir_id {
543            return ControlFlow::Break(());
544        }
545        intravisit::walk_anon_const(self, c)
546    }
547}