1 use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
3 use crate::ty::subst::{Subst, SubstsRef};
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_hir::def_id::DefId;
7 use rustc_span::symbol::Symbol;
10 use super::{EarlyBoundRegion, InstantiatedPredicates, ParamConst, ParamTy, Predicate, TyCtxt};
12 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
13 pub enum GenericParamDefKind {
15 Type { has_default: bool, object_lifetime_default: ObjectLifetimeDefault, synthetic: bool },
16 Const { has_default: bool },
19 impl GenericParamDefKind {
20 pub fn descr(&self) -> &'static str {
22 GenericParamDefKind::Lifetime => "lifetime",
23 GenericParamDefKind::Type { .. } => "type",
24 GenericParamDefKind::Const { .. } => "constant",
27 pub fn to_ord(&self) -> ast::ParamKindOrd {
29 GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
30 GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
31 GenericParamDefKind::Const { .. } => ast::ParamKindOrd::Const,
35 pub fn is_ty_or_const(&self) -> bool {
37 GenericParamDefKind::Lifetime => false,
38 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true,
43 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
44 pub struct GenericParamDef {
49 /// `pure_wrt_drop`, set by the (unsafe) `#[may_dangle]` attribute
50 /// on generic parameter `'a`/`T`, asserts data behind the parameter
51 /// `'a`/`T` won't be accessed during the parent type's `Drop` impl.
52 pub pure_wrt_drop: bool,
54 pub kind: GenericParamDefKind,
57 impl GenericParamDef {
58 pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion {
59 if let GenericParamDefKind::Lifetime = self.kind {
60 ty::EarlyBoundRegion { def_id: self.def_id, index: self.index, name: self.name }
62 bug!("cannot convert a non-lifetime parameter def to an early bound region")
68 pub struct GenericParamCount {
74 /// Information about the formal type/lifetime parameters associated
75 /// with an item or method. Analogous to `hir::Generics`.
77 /// The ordering of parameters is the same as in `Subst` (excluding child generics):
78 /// `Self` (optionally), `Lifetime` params..., `Type` params...
79 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
81 pub parent: Option<DefId>,
82 pub parent_count: usize,
83 pub params: Vec<GenericParamDef>,
85 /// Reverse map to the `index` field of each `GenericParamDef`.
86 #[stable_hasher(ignore)]
87 pub param_def_id_to_index: FxHashMap<DefId, u32>,
90 pub has_late_bound_regions: Option<Span>,
95 pub fn count(&self) -> usize {
96 self.parent_count + self.params.len()
99 pub fn own_counts(&self) -> GenericParamCount {
100 // We could cache this as a property of `GenericParamCount`, but
101 // the aim is to refactor this away entirely eventually and the
102 // presence of this method will be a constant reminder.
103 let mut own_counts = GenericParamCount::default();
105 for param in &self.params {
107 GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
108 GenericParamDefKind::Type { .. } => own_counts.types += 1,
109 GenericParamDefKind::Const { .. } => own_counts.consts += 1,
116 pub fn own_defaults(&self) -> GenericParamCount {
117 let mut own_defaults = GenericParamCount::default();
119 for param in &self.params {
121 GenericParamDefKind::Lifetime => (),
122 GenericParamDefKind::Type { has_default, .. } => {
123 own_defaults.types += has_default as usize;
125 GenericParamDefKind::Const { has_default } => {
126 own_defaults.consts += has_default as usize;
134 pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool {
135 if self.own_requires_monomorphization() {
139 if let Some(parent_def_id) = self.parent {
140 let parent = tcx.generics_of(parent_def_id);
141 parent.requires_monomorphization(tcx)
147 pub fn own_requires_monomorphization(&self) -> bool {
148 for param in &self.params {
150 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
153 GenericParamDefKind::Lifetime => {}
159 /// Returns the `GenericParamDef` with the given index.
160 pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
161 if let Some(index) = param_index.checked_sub(self.parent_count) {
164 tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
165 .param_at(param_index, tcx)
169 /// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`.
172 param: &EarlyBoundRegion,
174 ) -> &'tcx GenericParamDef {
175 let param = self.param_at(param.index as usize, tcx);
177 GenericParamDefKind::Lifetime => param,
178 _ => bug!("expected lifetime parameter, but found another generic parameter"),
182 /// Returns the `GenericParamDef` associated with this `ParamTy`.
183 pub fn type_param(&'tcx self, param: &ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
184 let param = self.param_at(param.index as usize, tcx);
186 GenericParamDefKind::Type { .. } => param,
187 _ => bug!("expected type parameter, but found another generic parameter"),
191 /// Returns the `GenericParamDef` associated with this `ParamConst`.
192 pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
193 let param = self.param_at(param.index as usize, tcx);
195 GenericParamDefKind::Const { .. } => param,
196 _ => bug!("expected const parameter, but found another generic parameter"),
200 /// Returns `true` if `params` has `impl Trait`.
201 pub fn has_impl_trait(&'tcx self) -> bool {
202 self.params.iter().any(|param| {
203 matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. })
208 /// Bounds on generics.
209 #[derive(Copy, Clone, Default, Debug, TyEncodable, TyDecodable, HashStable)]
210 pub struct GenericPredicates<'tcx> {
211 pub parent: Option<DefId>,
212 pub predicates: &'tcx [(Predicate<'tcx>, Span)],
215 impl<'tcx> GenericPredicates<'tcx> {
219 substs: SubstsRef<'tcx>,
220 ) -> InstantiatedPredicates<'tcx> {
221 let mut instantiated = InstantiatedPredicates::empty();
222 self.instantiate_into(tcx, &mut instantiated, substs);
226 pub fn instantiate_own(
229 substs: SubstsRef<'tcx>,
230 ) -> InstantiatedPredicates<'tcx> {
231 InstantiatedPredicates {
232 predicates: self.predicates.iter().map(|(p, _)| p.subst(tcx, substs)).collect(),
233 spans: self.predicates.iter().map(|(_, sp)| *sp).collect(),
240 instantiated: &mut InstantiatedPredicates<'tcx>,
241 substs: SubstsRef<'tcx>,
243 if let Some(def_id) = self.parent {
244 tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, substs);
246 instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p.subst(tcx, substs)));
247 instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp));
250 pub fn instantiate_identity(&self, tcx: TyCtxt<'tcx>) -> InstantiatedPredicates<'tcx> {
251 let mut instantiated = InstantiatedPredicates::empty();
252 self.instantiate_identity_into(tcx, &mut instantiated);
256 fn instantiate_identity_into(
259 instantiated: &mut InstantiatedPredicates<'tcx>,
261 if let Some(def_id) = self.parent {
262 tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated);
264 instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p));
265 instantiated.spans.extend(self.predicates.iter().map(|(_, s)| s));