Skip to main content

rustc_hir_analysis/collect/
generics_of.rs

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