1 use crate::middle::resolve_lifetime as rl;
3 intravisit::{self, Visitor},
4 GenericParamKind, HirId, Node,
7 use rustc_hir::def::DefKind;
8 use rustc_hir::def_id::DefId;
9 use rustc_middle::ty::{self, TyCtxt};
10 use rustc_session::lint;
11 use rustc_span::symbol::{kw, Symbol};
14 pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
17 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
19 let node = tcx.hir().get(hir_id);
20 let parent_def_id = match node {
26 let parent_id = tcx.hir().get_parent_item(hir_id);
27 Some(parent_id.to_def_id())
29 // FIXME(#43408) always enable this once `lazy_normalization` is
30 // stable enough and does not need a feature gate anymore.
31 Node::AnonConst(_) => {
32 let parent_def_id = tcx.hir().get_parent_item(hir_id);
34 let mut in_param_ty = false;
35 for (_parent, node) in tcx.hir().parent_iter(hir_id) {
36 if let Some(generics) = node.generics() {
37 let mut visitor = AnonConstInParamTyDetector {
39 found_anon_const_in_param_ty: false,
43 visitor.visit_generics(generics);
44 in_param_ty = visitor.found_anon_const_in_param_ty;
50 // We do not allow generic parameters in anon consts if we are inside
51 // of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
53 } else if tcx.lazy_normalization() {
54 if let Some(param_id) = tcx.hir().opt_const_param_default_param_def_id(hir_id) {
55 // If the def_id we are calling generics_of on is an anon ct default i.e:
57 // struct Foo<const N: usize = { .. }>;
58 // ^^^ ^ ^^^^^^ def id of this anon const
62 // then we only want to return generics for params to the left of `N`. If we don't do that we
63 // end up with that const looking like: `ty::ConstKind::Unevaluated(def_id, substs: [N#0])`.
65 // This causes ICEs (#86580) when building the substs for Foo in `fn foo() -> Foo { .. }` as
66 // we substitute the defaults with the partially built substs when we build the substs. Subst'ing
67 // the `N#0` on the unevaluated const indexes into the empty substs we're in the process of building.
69 // We fix this by having this function return the parent's generics ourselves and truncating the
70 // generics to only include non-forward declared params (with the exception of the `Self` ty)
72 // For the above code example that means we want `substs: []`
73 // For the following struct def we want `substs: [N#0]` when generics_of is called on
74 // the def id of the `{ N + 1 }` anon const
75 // struct Foo<const N: usize, const M: usize = { N + 1 }>;
77 // This has some implications for how we get the predicates available to the anon const
78 // see `explicit_predicates_of` for more information on this
79 let generics = tcx.generics_of(parent_def_id.to_def_id());
80 let param_def_idx = generics.param_def_id_to_index[¶m_id.to_def_id()];
81 // In the above example this would be .params[..N#0]
82 let params = generics.params_to(param_def_idx as usize, tcx).to_owned();
83 let param_def_id_to_index =
84 params.iter().map(|param| (param.def_id, param.index)).collect();
87 // we set the parent of these generics to be our parent's parent so that we
88 // dont end up with substs: [N, M, N] for the const default on a struct like this:
89 // struct Foo<const N: usize, const M: usize = { ... }>;
90 parent: generics.parent,
91 parent_count: generics.parent_count,
93 param_def_id_to_index,
94 has_self: generics.has_self,
95 has_late_bound_regions: generics.has_late_bound_regions,
99 // HACK(eddyb) this provides the correct generics when
100 // `feature(generic_const_expressions)` is enabled, so that const expressions
101 // used with const generics, e.g. `Foo<{N+1}>`, can work at all.
103 // Note that we do not supply the parent generics when using
104 // `min_const_generics`.
105 Some(parent_def_id.to_def_id())
107 let parent_node = tcx.hir().get_parent(hir_id);
109 // HACK(eddyb) this provides the correct generics for repeat
110 // expressions' count (i.e. `N` in `[x; N]`), and explicit
111 // `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`),
112 // as they shouldn't be able to cause query cycle errors.
113 Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
114 if constant.hir_id() == hir_id =>
116 Some(parent_def_id.to_def_id())
118 Node::Variant(Variant { disr_expr: Some(ref constant), .. })
119 if constant.hir_id == hir_id =>
121 Some(parent_def_id.to_def_id())
123 Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) => {
124 Some(tcx.typeck_root_def_id(def_id))
126 // Exclude `GlobalAsm` here which cannot have generics.
127 Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
128 if asm.operands.iter().any(|(op, _op_sp)| match op {
129 hir::InlineAsmOperand::Const { anon_const }
130 | hir::InlineAsmOperand::SymFn { anon_const } => {
131 anon_const.hir_id == hir_id
136 Some(parent_def_id.to_def_id())
142 Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
143 Some(tcx.typeck_root_def_id(def_id))
145 Node::Item(item) => match item.kind {
146 ItemKind::OpaqueTy(hir::OpaqueTy {
148 hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
153 assert!(matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn))
155 assert!(matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn))
157 Some(fn_def_id.to_def_id())
159 ItemKind::OpaqueTy(hir::OpaqueTy { origin: hir::OpaqueTyOrigin::TyAlias, .. }) => {
160 let parent_id = tcx.hir().get_parent_item(hir_id);
161 assert_ne!(parent_id, hir::CRATE_OWNER_ID);
162 debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent_id);
163 // Opaque types are always nested within another item, and
164 // inherit the generics of the item.
165 Some(parent_id.to_def_id())
175 FutureCompatDisallowed,
179 let no_generics = hir::Generics::empty();
180 let ast_generics = node.generics().unwrap_or(&no_generics);
181 let (opt_self, allow_defaults) = match node {
182 Node::Item(item) => {
184 ItemKind::Trait(..) | ItemKind::TraitAlias(..) => {
185 // Add in the self type parameter.
187 // Something of a hack: use the node id for the trait, also as
188 // the node id for the Self type parameter.
189 let opt_self = Some(ty::GenericParamDef {
193 pure_wrt_drop: false,
194 kind: ty::GenericParamDefKind::Type {
200 (opt_self, Defaults::Allowed)
202 ItemKind::TyAlias(..)
204 | ItemKind::Struct(..)
205 | ItemKind::OpaqueTy(..)
206 | ItemKind::Union(..) => (None, Defaults::Allowed),
207 _ => (None, Defaults::FutureCompatDisallowed),
212 Node::TraitItem(item) if matches!(item.kind, TraitItemKind::Type(..)) => {
213 (None, Defaults::Deny)
215 Node::ImplItem(item) if matches!(item.kind, ImplItemKind::Type(..)) => {
216 (None, Defaults::Deny)
219 _ => (None, Defaults::FutureCompatDisallowed),
222 let has_self = opt_self.is_some();
223 let mut parent_has_self = false;
224 let mut own_start = has_self as u32;
225 let parent_count = parent_def_id.map_or(0, |def_id| {
226 let generics = tcx.generics_of(def_id);
228 parent_has_self = generics.has_self;
229 own_start = generics.count() as u32;
230 generics.parent_count + generics.params.len()
233 let mut params: Vec<_> = Vec::with_capacity(ast_generics.params.len() + has_self as usize);
235 if let Some(opt_self) = opt_self {
236 params.push(opt_self);
239 let early_lifetimes = super::early_bound_lifetimes_from_generics(tcx, ast_generics);
240 params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
241 name: param.name.ident().name,
242 index: own_start + i as u32,
243 def_id: param.def_id.to_def_id(),
244 pure_wrt_drop: param.pure_wrt_drop,
245 kind: ty::GenericParamDefKind::Lifetime,
248 // Now create the real type and const parameters.
249 let type_start = own_start - has_self as u32 + params.len() as u32;
251 let mut next_index = || {
254 prev as u32 + type_start
257 const TYPE_DEFAULT_NOT_ALLOWED: &'static str = "defaults for type parameters are only allowed in \
258 `struct`, `enum`, `type`, or `trait` definitions";
260 params.extend(ast_generics.params.iter().filter_map(|param| match param.kind {
261 GenericParamKind::Lifetime { .. } => None,
262 GenericParamKind::Type { ref default, synthetic, .. } => {
263 if default.is_some() {
264 match allow_defaults {
265 Defaults::Allowed => {}
266 Defaults::FutureCompatDisallowed
267 if tcx.features().default_type_parameter_fallback => {}
268 Defaults::FutureCompatDisallowed => {
269 tcx.struct_span_lint_hir(
270 lint::builtin::INVALID_TYPE_PARAM_DEFAULT,
273 TYPE_DEFAULT_NOT_ALLOWED,
278 tcx.sess.span_err(param.span, TYPE_DEFAULT_NOT_ALLOWED);
283 let kind = ty::GenericParamDefKind::Type { has_default: default.is_some(), synthetic };
285 Some(ty::GenericParamDef {
287 name: param.name.ident().name,
288 def_id: param.def_id.to_def_id(),
289 pure_wrt_drop: param.pure_wrt_drop,
293 GenericParamKind::Const { default, .. } => {
294 if !matches!(allow_defaults, Defaults::Allowed) && default.is_some() {
297 "defaults for const parameters are only allowed in \
298 `struct`, `enum`, `type`, or `trait` definitions",
302 Some(ty::GenericParamDef {
304 name: param.name.ident().name,
305 def_id: param.def_id.to_def_id(),
306 pure_wrt_drop: param.pure_wrt_drop,
307 kind: ty::GenericParamDefKind::Const { has_default: default.is_some() },
312 // provide junk type parameter defs - the only place that
313 // cares about anything but the length is instantiation,
314 // and we don't do that for closures.
315 if let Node::Expr(&hir::Expr {
316 kind: hir::ExprKind::Closure(hir::Closure { movability: gen, .. }),
320 let dummy_args = if gen.is_some() {
321 &["<resume_ty>", "<yield_ty>", "<return_ty>", "<witness>", "<upvars>"][..]
323 &["<closure_kind>", "<closure_signature>", "<upvars>"][..]
326 params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
328 name: Symbol::intern(arg),
330 pure_wrt_drop: false,
331 kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
335 // provide junk type parameter defs for const blocks.
336 if let Node::AnonConst(_) = node {
337 let parent_node = tcx.hir().get_parent(hir_id);
338 if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node {
339 params.push(ty::GenericParamDef {
341 name: Symbol::intern("<const_ty>"),
343 pure_wrt_drop: false,
344 kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
349 let param_def_id_to_index = params.iter().map(|param| (param.def_id, param.index)).collect();
352 parent: parent_def_id,
355 param_def_id_to_index,
356 has_self: has_self || parent_has_self,
357 has_late_bound_regions: has_late_bound_regions(tcx, node),
361 fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<Span> {
362 struct LateBoundRegionsDetector<'tcx> {
364 outer_index: ty::DebruijnIndex,
365 has_late_bound_regions: Option<Span>,
368 impl<'tcx> Visitor<'tcx> for LateBoundRegionsDetector<'tcx> {
369 fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) {
370 if self.has_late_bound_regions.is_some() {
374 hir::TyKind::BareFn(..) => {
375 self.outer_index.shift_in(1);
376 intravisit::walk_ty(self, ty);
377 self.outer_index.shift_out(1);
379 _ => intravisit::walk_ty(self, ty),
383 fn visit_poly_trait_ref(&mut self, tr: &'tcx hir::PolyTraitRef<'tcx>) {
384 if self.has_late_bound_regions.is_some() {
387 self.outer_index.shift_in(1);
388 intravisit::walk_poly_trait_ref(self, tr);
389 self.outer_index.shift_out(1);
392 fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
393 if self.has_late_bound_regions.is_some() {
397 match self.tcx.named_region(lt.hir_id) {
398 Some(rl::Region::Static | rl::Region::EarlyBound(..)) => {}
399 Some(rl::Region::LateBound(debruijn, _, _)) if debruijn < self.outer_index => {}
400 Some(rl::Region::LateBound(..) | rl::Region::Free(..)) | None => {
401 self.has_late_bound_regions = Some(lt.ident.span);
407 fn has_late_bound_regions<'tcx>(
409 generics: &'tcx hir::Generics<'tcx>,
410 decl: &'tcx hir::FnDecl<'tcx>,
412 let mut visitor = LateBoundRegionsDetector {
414 outer_index: ty::INNERMOST,
415 has_late_bound_regions: None,
417 for param in generics.params {
418 if let GenericParamKind::Lifetime { .. } = param.kind {
419 if tcx.is_late_bound(param.hir_id) {
420 return Some(param.span);
424 visitor.visit_fn_decl(decl);
425 visitor.has_late_bound_regions
429 Node::TraitItem(item) => match item.kind {
430 hir::TraitItemKind::Fn(ref sig, _) => {
431 has_late_bound_regions(tcx, &item.generics, sig.decl)
435 Node::ImplItem(item) => match item.kind {
436 hir::ImplItemKind::Fn(ref sig, _) => {
437 has_late_bound_regions(tcx, &item.generics, sig.decl)
441 Node::ForeignItem(item) => match item.kind {
442 hir::ForeignItemKind::Fn(fn_decl, _, ref generics) => {
443 has_late_bound_regions(tcx, generics, fn_decl)
447 Node::Item(item) => match item.kind {
448 hir::ItemKind::Fn(ref sig, .., ref generics, _) => {
449 has_late_bound_regions(tcx, generics, sig.decl)
457 struct AnonConstInParamTyDetector {
459 found_anon_const_in_param_ty: bool,
463 impl<'v> Visitor<'v> for AnonConstInParamTyDetector {
464 fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) {
465 if let GenericParamKind::Const { ty, default: _ } = p.kind {
466 let prev = self.in_param_ty;
467 self.in_param_ty = true;
469 self.in_param_ty = prev;
473 fn visit_anon_const(&mut self, c: &'v hir::AnonConst) {
474 if self.in_param_ty && self.ct == c.hir_id {
475 self.found_anon_const_in_param_ty = true;
477 intravisit::walk_anon_const(self, c)