rustc_hir_analysis/collect/
generics_of.rs1use std::assert_matches;
2use std::ops::ControlFlow;
3
4use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level};
5use rustc_hir::def::DefKind;
6use rustc_hir::def_id::LocalDefId;
7use rustc_hir::intravisit::{self, Visitor, VisitorExt};
8use rustc_hir::{self as hir, AmbigArg, GenericParamKind, HirId, Node};
9use rustc_middle::span_bug;
10use rustc_middle::ty::{self, TyCtxt};
11use rustc_session::lint;
12use rustc_span::{Span, Symbol, kw};
13use tracing::{debug, instrument};
14
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 struct GenericParametersForbiddenHere {
22 msg: &'static str,
23 }
24
25 impl<'a> Diagnostic<'a, ()> for GenericParametersForbiddenHere {
26 fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
27 let Self { msg } = self;
28 Diag::new(dcx, level, msg)
29 }
30 }
31
32 if let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id }) =
35 tcx.opt_rpitit_info(def_id.to_def_id())
36 {
37 debug!("RPITIT fn_def_id={fn_def_id:?} opaque_def_id={opaque_def_id:?}");
38 let trait_def_id = tcx.parent(fn_def_id);
39 let opaque_ty_generics = tcx.generics_of(opaque_def_id);
40 let opaque_ty_parent_count = opaque_ty_generics.parent_count;
41 let mut own_params = opaque_ty_generics.own_params.clone();
42
43 let parent_generics = tcx.generics_of(trait_def_id);
44 let parent_count = parent_generics.parent_count + parent_generics.own_params.len();
45
46 let mut trait_fn_params = tcx.generics_of(fn_def_id).own_params.clone();
47
48 for param in &mut own_params {
49 param.index = param.index + parent_count as u32 + trait_fn_params.len() as u32
50 - opaque_ty_parent_count as u32;
51 }
52
53 trait_fn_params.extend(own_params);
54 own_params = trait_fn_params;
55
56 let param_def_id_to_index =
57 own_params.iter().map(|param| (param.def_id, param.index)).collect();
58
59 return ty::Generics {
60 parent: Some(trait_def_id),
61 parent_count,
62 own_params,
63 param_def_id_to_index,
64 has_self: opaque_ty_generics.has_self,
65 has_late_bound_regions: opaque_ty_generics.has_late_bound_regions,
66 };
67 }
68
69 let hir_id = tcx.local_def_id_to_hir_id(def_id);
70 let node = tcx.hir_node(hir_id);
71
72 let parent_def_id = match node {
73 Node::ImplItem(_)
74 | Node::TraitItem(_)
75 | Node::Variant(_)
76 | Node::Ctor(..)
77 | Node::Field(_) => {
78 let parent_id = tcx.hir_get_parent_item(hir_id);
79 Some(parent_id.def_id)
80 }
81 Node::AnonConst(_) => {
84 let parent_did = tcx.local_parent(def_id);
85 debug!(?parent_did);
86
87 let mut in_param_ty = false;
88 for (_parent, node) in tcx.hir_parent_iter(hir_id) {
89 if let Some(generics) = node.generics() {
90 let mut visitor = AnonConstInParamTyDetector { in_param_ty: false, ct: hir_id };
91
92 in_param_ty = visitor.visit_generics(generics).is_break();
93 break;
94 }
95 }
96
97 match tcx.anon_const_kind(def_id) {
98 ty::AnonConstKind::MCG => None,
100 ty::AnonConstKind::OGCA => Some(parent_did),
102 ty::AnonConstKind::RepeatExprCount => Some(parent_did),
104
105 ty::AnonConstKind::GCE if in_param_ty => None,
112 ty::AnonConstKind::GCE
118 if let Some(param_id) =
119 tcx.hir_opt_const_param_default_param_def_id(hir_id) =>
120 {
121 let generics = tcx.generics_of(parent_did);
146 let param_def_idx = generics.param_def_id_to_index[¶m_id.to_def_id()];
147 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 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 }
164 ty::AnonConstKind::GCE => Some(parent_did),
165
166 ty::AnonConstKind::NonTypeSystem
168 if matches!(tcx.parent_hir_node(hir_id), Node::TyPat(_) | Node::Field(_)) =>
169 {
170 Some(parent_did)
171 }
172 ty::AnonConstKind::NonTypeSystem => None,
174 }
175 }
176 Node::ConstBlock(_)
177 | Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
178 Some(tcx.typeck_root_def_id_local(def_id))
179 }
180 Node::OpaqueTy(&hir::OpaqueTy {
181 origin:
182 hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, in_trait_or_impl }
183 | hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, in_trait_or_impl },
184 ..
185 }) => {
186 if in_trait_or_impl.is_some() {
187 assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn);
188 } else {
189 assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn);
190 }
191 Some(fn_def_id)
192 }
193 Node::OpaqueTy(&hir::OpaqueTy {
194 origin: hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty },
195 ..
196 }) => {
197 if in_assoc_ty {
198 assert_matches!(tcx.def_kind(parent), DefKind::AssocTy);
199 } else {
200 assert_matches!(tcx.def_kind(parent), DefKind::TyAlias);
201 }
202 debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent);
203 Some(parent)
206 }
207
208 Node::Item(_) | Node::ForeignItem(_) => None,
210
211 Node::GenericParam(_) => None,
213
214 Node::Synthetic => span_bug!(
215 tcx.def_span(def_id),
216 "synthetic HIR should have its `generics_of` explicitly fed"
217 ),
218
219 _ => span_bug!(tcx.def_span(def_id), "generics_of: unexpected node kind {node:?}"),
220 };
221
222 let opt_self = if let Node::Item(item) = node
224 && let ItemKind::Trait(..) | ItemKind::TraitAlias(..) = item.kind
225 {
226 Some(ty::GenericParamDef {
228 index: 0,
229 name: kw::SelfUpper,
230 def_id: def_id.to_def_id(),
231 pure_wrt_drop: false,
232 kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
233 })
234 } else {
235 None
236 };
237
238 let param_default_policy = param_default_policy(node);
239 let hir_generics = node.generics().unwrap_or(hir::Generics::empty());
240 let has_self = opt_self.is_some();
241 let mut parent_has_self = false;
242 let mut own_start = has_self as u32;
243 let parent_count = parent_def_id.map_or(0, |def_id| {
244 let generics = tcx.generics_of(def_id);
245 assert!(!has_self);
246 parent_has_self = generics.has_self;
247 own_start = generics.count() as u32;
248 generics.parent_count + generics.own_params.len()
249 });
250
251 let mut own_params: Vec<_> = Vec::with_capacity(hir_generics.params.len() + has_self as usize);
252
253 if let Some(opt_self) = opt_self {
254 own_params.push(opt_self);
255 }
256
257 let early_lifetimes = super::early_bound_lifetimes_from_generics(tcx, hir_generics);
258 own_params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
259 name: param.name.ident().name,
260 index: own_start + i as u32,
261 def_id: param.def_id.to_def_id(),
262 pure_wrt_drop: param.pure_wrt_drop,
263 kind: ty::GenericParamDefKind::Lifetime,
264 }));
265
266 let type_start = own_start - has_self as u32 + own_params.len() as u32;
268 let mut i: u32 = 0;
269 let mut next_index = || {
270 let prev = i;
271 i += 1;
272 prev + type_start
273 };
274
275 own_params.extend(hir_generics.params.iter().filter_map(|param| {
276 const MESSAGE: &str = "defaults for generic parameters are not allowed here";
277 let kind = match param.kind {
278 GenericParamKind::Lifetime { .. } => return None,
279 GenericParamKind::Type { default, synthetic } => {
280 if default.is_some() {
281 match param_default_policy.expect("no policy for generic param default") {
282 ParamDefaultPolicy::Allowed => {}
283 ParamDefaultPolicy::FutureCompatForbidden => {
284 tcx.emit_node_span_lint(
285 lint::builtin::INVALID_TYPE_PARAM_DEFAULT,
286 param.hir_id,
287 param.span,
288 GenericParametersForbiddenHere { msg: MESSAGE },
289 );
290 }
291 ParamDefaultPolicy::Forbidden => {
292 tcx.dcx().span_err(param.span, MESSAGE);
293 }
294 }
295 }
296
297 ty::GenericParamDefKind::Type { has_default: default.is_some(), synthetic }
298 }
299 GenericParamKind::Const { ty: _, default } => {
300 if default.is_some() {
301 match param_default_policy.expect("no policy for generic param default") {
302 ParamDefaultPolicy::Allowed => {}
303 ParamDefaultPolicy::FutureCompatForbidden
304 | ParamDefaultPolicy::Forbidden => {
305 tcx.dcx().span_err(param.span, MESSAGE);
306 }
307 }
308 }
309
310 ty::GenericParamDefKind::Const { has_default: default.is_some() }
311 }
312 };
313 Some(ty::GenericParamDef {
314 index: next_index(),
315 name: param.name.ident().name,
316 def_id: param.def_id.to_def_id(),
317 pure_wrt_drop: param.pure_wrt_drop,
318 kind,
319 })
320 }));
321
322 if let Node::Expr(&hir::Expr {
326 kind: hir::ExprKind::Closure(hir::Closure { kind, .. }), ..
327 }) = node
328 {
329 let dummy_args = match kind {
332 ClosureKind::Closure => &["<closure_kind>", "<closure_signature>", "<upvars>"][..],
333 ClosureKind::Coroutine(_) => {
334 &["<coroutine_kind>", "<resume_ty>", "<yield_ty>", "<return_ty>", "<upvars>"][..]
335 }
336 ClosureKind::CoroutineClosure(_) => &[
337 "<closure_kind>",
338 "<closure_signature_parts>",
339 "<upvars>",
340 "<bound_captures_by_ref>",
341 ][..],
342 };
343
344 own_params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
345 index: next_index(),
346 name: Symbol::intern(arg),
347 def_id: def_id.to_def_id(),
348 pure_wrt_drop: false,
349 kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
350 }));
351 }
352
353 if let Node::ConstBlock(_) = node {
355 own_params.push(ty::GenericParamDef {
356 index: next_index(),
357 name: rustc_span::sym::const_ty_placeholder,
358 def_id: def_id.to_def_id(),
359 pure_wrt_drop: false,
360 kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
361 });
362 }
363
364 if let Node::OpaqueTy(&hir::OpaqueTy { .. }) = node {
365 assert!(own_params.is_empty());
366
367 let lifetimes = tcx.opaque_captured_lifetimes(def_id);
368 debug!(?lifetimes);
369
370 own_params.extend(lifetimes.iter().map(|&(_, param)| ty::GenericParamDef {
371 name: tcx.item_name(param.to_def_id()),
372 index: next_index(),
373 def_id: param.to_def_id(),
374 pure_wrt_drop: false,
375 kind: ty::GenericParamDefKind::Lifetime,
376 }))
377 }
378
379 let param_def_id_to_index =
380 own_params.iter().map(|param| (param.def_id, param.index)).collect();
381
382 ty::Generics {
383 parent: parent_def_id.map(LocalDefId::to_def_id),
384 parent_count,
385 own_params,
386 param_def_id_to_index,
387 has_self: has_self || parent_has_self,
388 has_late_bound_regions: has_late_bound_regions(tcx, node),
389 }
390}
391
392#[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)]
393enum ParamDefaultPolicy {
394 Allowed,
395 FutureCompatForbidden,
397 Forbidden,
398}
399
400fn param_default_policy(node: Node<'_>) -> Option<ParamDefaultPolicy> {
401 use rustc_hir::*;
402
403 Some(match node {
404 Node::Item(item) => match item.kind {
405 ItemKind::Trait(..)
406 | ItemKind::TraitAlias(..)
407 | ItemKind::TyAlias(..)
408 | ItemKind::Enum(..)
409 | ItemKind::Struct(..)
410 | ItemKind::Union(..) => ParamDefaultPolicy::Allowed,
411 ItemKind::Fn { .. } | ItemKind::Impl(_) => ParamDefaultPolicy::FutureCompatForbidden,
412 ItemKind::Const(..) => ParamDefaultPolicy::Forbidden,
414 _ => return None,
415 },
416 Node::TraitItem(item) => match item.kind {
417 TraitItemKind::Const(..) | TraitItemKind::Type(..) => ParamDefaultPolicy::Forbidden,
419 TraitItemKind::Fn(..) => ParamDefaultPolicy::FutureCompatForbidden,
420 },
421 Node::ImplItem(item) => match item.kind {
422 ImplItemKind::Const(..) | ImplItemKind::Type(..) => ParamDefaultPolicy::Forbidden,
424 ImplItemKind::Fn(..) => ParamDefaultPolicy::FutureCompatForbidden,
425 },
426 Node::ForeignItem(_) => ParamDefaultPolicy::Forbidden,
429 Node::OpaqueTy(..) => ParamDefaultPolicy::Allowed,
430 _ => return None,
431 })
432}
433
434fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<Span> {
435 struct LateBoundRegionsDetector<'tcx> {
436 tcx: TyCtxt<'tcx>,
437 outer_index: ty::DebruijnIndex,
438 }
439
440 impl<'tcx> Visitor<'tcx> for LateBoundRegionsDetector<'tcx> {
441 type Result = ControlFlow<Span>;
442 fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx, AmbigArg>) -> ControlFlow<Span> {
443 match ty.kind {
444 hir::TyKind::FnPtr(..) => {
445 self.outer_index.shift_in(1);
446 let res = intravisit::walk_ty(self, ty);
447 self.outer_index.shift_out(1);
448 res
449 }
450 hir::TyKind::UnsafeBinder(_) => {
451 self.outer_index.shift_in(1);
452 let res = intravisit::walk_ty(self, ty);
453 self.outer_index.shift_out(1);
454 res
455 }
456 _ => intravisit::walk_ty(self, ty),
457 }
458 }
459
460 fn visit_poly_trait_ref(&mut self, tr: &'tcx hir::PolyTraitRef<'tcx>) -> ControlFlow<Span> {
461 self.outer_index.shift_in(1);
462 let res = intravisit::walk_poly_trait_ref(self, tr);
463 self.outer_index.shift_out(1);
464 res
465 }
466
467 fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) -> ControlFlow<Span> {
468 match self.tcx.named_bound_var(lt.hir_id) {
469 Some(rbv::ResolvedArg::StaticLifetime | rbv::ResolvedArg::EarlyBound(..)) => {
470 ControlFlow::Continue(())
471 }
472 Some(rbv::ResolvedArg::LateBound(debruijn, _, _))
473 if debruijn < self.outer_index =>
474 {
475 ControlFlow::Continue(())
476 }
477 Some(
478 rbv::ResolvedArg::LateBound(..)
479 | rbv::ResolvedArg::Free(..)
480 | rbv::ResolvedArg::Error(_),
481 )
482 | None => ControlFlow::Break(lt.ident.span),
483 }
484 }
485 }
486
487 fn has_late_bound_regions<'tcx>(
488 tcx: TyCtxt<'tcx>,
489 generics: &'tcx hir::Generics<'tcx>,
490 decl: &'tcx hir::FnDecl<'tcx>,
491 ) -> Option<Span> {
492 let mut visitor = LateBoundRegionsDetector { tcx, outer_index: ty::INNERMOST };
493 for param in generics.params {
494 if let GenericParamKind::Lifetime { .. } = param.kind {
495 if tcx.is_late_bound(param.hir_id) {
496 return Some(param.span);
497 }
498 }
499 }
500 visitor.visit_fn_decl(decl).break_value()
501 }
502
503 let decl = node.fn_decl()?;
504 let generics = node.generics()?;
505 has_late_bound_regions(tcx, generics, decl)
506}
507
508struct AnonConstInParamTyDetector {
509 in_param_ty: bool,
510 ct: HirId,
511}
512
513impl<'v> Visitor<'v> for AnonConstInParamTyDetector {
514 type Result = ControlFlow<()>;
515
516 fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) -> Self::Result {
517 if let GenericParamKind::Const { ty, default: _ } = p.kind {
518 let prev = self.in_param_ty;
519 self.in_param_ty = true;
520 let res = self.visit_ty_unambig(ty);
521 self.in_param_ty = prev;
522 res
523 } else {
524 ControlFlow::Continue(())
525 }
526 }
527
528 fn visit_anon_const(&mut self, c: &'v hir::AnonConst) -> Self::Result {
529 if self.in_param_ty && self.ct == c.hir_id {
530 return ControlFlow::Break(());
531 }
532 intravisit::walk_anon_const(self, c)
533 }
534}