1 use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
3 use crate::ty::subst::{Subst, SubstsRef};
4 use crate::ty::EarlyBinder;
6 use rustc_data_structures::fx::FxHashMap;
7 use rustc_hir::def_id::DefId;
8 use rustc_span::symbol::Symbol;
11 use super::{EarlyBoundRegion, InstantiatedPredicates, ParamConst, ParamTy, Predicate, TyCtxt};
13 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
14 pub enum GenericParamDefKind {
16 Type { has_default: bool, object_lifetime_default: ObjectLifetimeDefault, synthetic: bool },
17 Const { has_default: bool },
20 impl GenericParamDefKind {
21 pub fn descr(&self) -> &'static str {
23 GenericParamDefKind::Lifetime => "lifetime",
24 GenericParamDefKind::Type { .. } => "type",
25 GenericParamDefKind::Const { .. } => "constant",
28 pub fn to_ord(&self) -> ast::ParamKindOrd {
30 GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
31 GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
32 GenericParamDefKind::Const { .. } => ast::ParamKindOrd::Const,
36 pub fn is_ty_or_const(&self) -> bool {
38 GenericParamDefKind::Lifetime => false,
39 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true,
44 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
45 pub struct GenericParamDef {
50 /// `pure_wrt_drop`, set by the (unsafe) `#[may_dangle]` attribute
51 /// on generic parameter `'a`/`T`, asserts data behind the parameter
52 /// `'a`/`T` won't be accessed during the parent type's `Drop` impl.
53 pub pure_wrt_drop: bool,
55 pub kind: GenericParamDefKind,
58 impl GenericParamDef {
59 pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion {
60 if let GenericParamDefKind::Lifetime = self.kind {
61 ty::EarlyBoundRegion { def_id: self.def_id, index: self.index, name: self.name }
63 bug!("cannot convert a non-lifetime parameter def to an early bound region")
67 pub fn has_default(&self) -> bool {
69 GenericParamDefKind::Type { has_default, .. }
70 | GenericParamDefKind::Const { has_default } => has_default,
71 GenericParamDefKind::Lifetime => false,
75 pub fn default_value<'tcx>(
78 ) -> Option<EarlyBinder<ty::GenericArg<'tcx>>> {
80 GenericParamDefKind::Type { has_default, .. } if has_default => {
81 Some(EarlyBinder(tcx.type_of(self.def_id).into()))
83 GenericParamDefKind::Const { has_default } if has_default => {
84 Some(EarlyBinder(tcx.const_param_default(self.def_id).into()))
92 pub struct GenericParamCount {
98 /// Information about the formal type/lifetime parameters associated
99 /// with an item or method. Analogous to `hir::Generics`.
101 /// The ordering of parameters is the same as in `Subst` (excluding child generics):
102 /// `Self` (optionally), `Lifetime` params..., `Type` params...
103 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
104 pub struct Generics {
105 pub parent: Option<DefId>,
106 pub parent_count: usize,
107 pub params: Vec<GenericParamDef>,
109 /// Reverse map to the `index` field of each `GenericParamDef`.
110 #[stable_hasher(ignore)]
111 pub param_def_id_to_index: FxHashMap<DefId, u32>,
114 pub has_late_bound_regions: Option<Span>,
117 impl<'tcx> Generics {
119 pub fn count(&self) -> usize {
120 self.parent_count + self.params.len()
123 pub fn own_counts(&self) -> GenericParamCount {
124 // We could cache this as a property of `GenericParamCount`, but
125 // the aim is to refactor this away entirely eventually and the
126 // presence of this method will be a constant reminder.
127 let mut own_counts = GenericParamCount::default();
129 for param in &self.params {
131 GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
132 GenericParamDefKind::Type { .. } => own_counts.types += 1,
133 GenericParamDefKind::Const { .. } => own_counts.consts += 1,
140 pub fn own_defaults(&self) -> GenericParamCount {
141 let mut own_defaults = GenericParamCount::default();
143 for param in &self.params {
145 GenericParamDefKind::Lifetime => (),
146 GenericParamDefKind::Type { has_default, .. } => {
147 own_defaults.types += has_default as usize;
149 GenericParamDefKind::Const { has_default } => {
150 own_defaults.consts += has_default as usize;
158 pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool {
159 if self.own_requires_monomorphization() {
163 if let Some(parent_def_id) = self.parent {
164 let parent = tcx.generics_of(parent_def_id);
165 parent.requires_monomorphization(tcx)
171 pub fn own_requires_monomorphization(&self) -> bool {
172 for param in &self.params {
174 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
177 GenericParamDefKind::Lifetime => {}
183 /// Returns the `GenericParamDef` with the given index.
184 pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
185 if let Some(index) = param_index.checked_sub(self.parent_count) {
188 tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
189 .param_at(param_index, tcx)
193 /// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`.
196 param: &EarlyBoundRegion,
198 ) -> &'tcx GenericParamDef {
199 let param = self.param_at(param.index as usize, tcx);
201 GenericParamDefKind::Lifetime => param,
202 _ => bug!("expected lifetime parameter, but found another generic parameter"),
206 /// Returns the `GenericParamDef` associated with this `ParamTy`.
207 pub fn type_param(&'tcx self, param: &ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
208 let param = self.param_at(param.index as usize, tcx);
210 GenericParamDefKind::Type { .. } => param,
211 _ => bug!("expected type parameter, but found another generic parameter"),
215 /// Returns the `GenericParamDef` associated with this `ParamConst`.
216 pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
217 let param = self.param_at(param.index as usize, tcx);
219 GenericParamDefKind::Const { .. } => param,
220 _ => bug!("expected const parameter, but found another generic parameter"),
224 /// Returns `true` if `params` has `impl Trait`.
225 pub fn has_impl_trait(&'tcx self) -> bool {
226 self.params.iter().any(|param| {
227 matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. })
231 /// Returns the substs corresponding to the generic parameters of this item, excluding `Self`.
232 pub fn own_substs(&'tcx self, substs: SubstsRef<'tcx>) -> &'tcx [ty::GenericArg<'tcx>] {
233 let own = &substs[self.parent_count..][..self.params.len()];
234 if self.has_self && self.parent.is_none() { &own[1..] } else { &own }
238 /// Bounds on generics.
239 #[derive(Copy, Clone, Default, Debug, TyEncodable, TyDecodable, HashStable)]
240 pub struct GenericPredicates<'tcx> {
241 pub parent: Option<DefId>,
242 pub predicates: &'tcx [(Predicate<'tcx>, Span)],
245 impl<'tcx> GenericPredicates<'tcx> {
249 substs: SubstsRef<'tcx>,
250 ) -> InstantiatedPredicates<'tcx> {
251 let mut instantiated = InstantiatedPredicates::empty();
252 self.instantiate_into(tcx, &mut instantiated, substs);
256 pub fn instantiate_own(
259 substs: SubstsRef<'tcx>,
260 ) -> InstantiatedPredicates<'tcx> {
261 InstantiatedPredicates {
265 .map(|(p, _)| EarlyBinder(*p).subst(tcx, substs))
267 spans: self.predicates.iter().map(|(_, sp)| *sp).collect(),
274 instantiated: &mut InstantiatedPredicates<'tcx>,
275 substs: SubstsRef<'tcx>,
277 if let Some(def_id) = self.parent {
278 tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, substs);
282 .extend(self.predicates.iter().map(|(p, _)| EarlyBinder(*p).subst(tcx, substs)));
283 instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp));
286 pub fn instantiate_identity(&self, tcx: TyCtxt<'tcx>) -> InstantiatedPredicates<'tcx> {
287 let mut instantiated = InstantiatedPredicates::empty();
288 self.instantiate_identity_into(tcx, &mut instantiated);
292 fn instantiate_identity_into(
295 instantiated: &mut InstantiatedPredicates<'tcx>,
297 if let Some(def_id) = self.parent {
298 tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated);
300 instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p));
301 instantiated.spans.extend(self.predicates.iter().map(|(_, s)| s));