rustc_hir_analysis/collect/
generics_of.rs

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