2 use crate::ty::{EarlyBinder, SubstsRef};
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_hir::def_id::DefId;
6 use rustc_span::symbol::{kw, Symbol};
9 use super::{EarlyBoundRegion, InstantiatedPredicates, ParamConst, ParamTy, Predicate, TyCtxt};
11 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
12 pub enum GenericParamDefKind {
14 Type { has_default: bool, synthetic: bool },
15 Const { has_default: bool },
18 impl GenericParamDefKind {
19 pub fn descr(&self) -> &'static str {
21 GenericParamDefKind::Lifetime => "lifetime",
22 GenericParamDefKind::Type { .. } => "type",
23 GenericParamDefKind::Const { .. } => "constant",
26 pub fn to_ord(&self) -> ast::ParamKindOrd {
28 GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
29 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
30 ast::ParamKindOrd::TypeOrConst
35 pub fn is_ty_or_const(&self) -> bool {
37 GenericParamDefKind::Lifetime => false,
38 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true,
42 pub fn is_synthetic(&self) -> bool {
44 GenericParamDefKind::Type { synthetic, .. } => *synthetic,
50 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
51 pub struct GenericParamDef {
56 /// `pure_wrt_drop`, set by the (unsafe) `#[may_dangle]` attribute
57 /// on generic parameter `'a`/`T`, asserts data behind the parameter
58 /// `'a`/`T` won't be accessed during the parent type's `Drop` impl.
59 pub pure_wrt_drop: bool,
61 pub kind: GenericParamDefKind,
64 impl GenericParamDef {
65 pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion {
66 if let GenericParamDefKind::Lifetime = self.kind {
67 ty::EarlyBoundRegion { def_id: self.def_id, index: self.index, name: self.name }
69 bug!("cannot convert a non-lifetime parameter def to an early bound region")
73 pub fn has_default(&self) -> bool {
75 GenericParamDefKind::Type { has_default, .. }
76 | GenericParamDefKind::Const { has_default } => has_default,
77 GenericParamDefKind::Lifetime => false,
81 pub fn is_anonymous_lifetime(&self) -> bool {
83 GenericParamDefKind::Lifetime => {
84 self.name == kw::UnderscoreLifetime || self.name == kw::Empty
90 pub fn default_value<'tcx>(
93 ) -> Option<EarlyBinder<ty::GenericArg<'tcx>>> {
95 GenericParamDefKind::Type { has_default, .. } if has_default => {
96 Some(tcx.bound_type_of(self.def_id).map_bound(|t| t.into()))
98 GenericParamDefKind::Const { has_default } if has_default => {
99 Some(tcx.bound_const_param_default(self.def_id).map_bound(|c| c.into()))
107 pub struct GenericParamCount {
108 pub lifetimes: usize,
113 /// Information about the formal type/lifetime parameters associated
114 /// with an item or method. Analogous to `hir::Generics`.
116 /// The ordering of parameters is the same as in `Subst` (excluding child generics):
117 /// `Self` (optionally), `Lifetime` params..., `Type` params...
118 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
119 pub struct Generics {
120 pub parent: Option<DefId>,
121 pub parent_count: usize,
122 pub params: Vec<GenericParamDef>,
124 /// Reverse map to the `index` field of each `GenericParamDef`.
125 #[stable_hasher(ignore)]
126 pub param_def_id_to_index: FxHashMap<DefId, u32>,
129 pub has_late_bound_regions: Option<Span>,
132 impl<'tcx> Generics {
133 /// Looks through the generics and all parents to find the index of the
134 /// given param def-id. This is in comparison to the `param_def_id_to_index`
135 /// struct member, which only stores information about this item's own
137 pub fn param_def_id_to_index(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<u32> {
138 if let Some(idx) = self.param_def_id_to_index.get(&def_id) {
140 } else if let Some(parent) = self.parent {
141 let parent = tcx.generics_of(parent);
142 parent.param_def_id_to_index(tcx, def_id)
149 pub fn count(&self) -> usize {
150 self.parent_count + self.params.len()
153 pub fn own_counts(&self) -> GenericParamCount {
154 // We could cache this as a property of `GenericParamCount`, but
155 // the aim is to refactor this away entirely eventually and the
156 // presence of this method will be a constant reminder.
157 let mut own_counts = GenericParamCount::default();
159 for param in &self.params {
161 GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
162 GenericParamDefKind::Type { .. } => own_counts.types += 1,
163 GenericParamDefKind::Const { .. } => own_counts.consts += 1,
170 pub fn own_defaults(&self) -> GenericParamCount {
171 let mut own_defaults = GenericParamCount::default();
173 for param in &self.params {
175 GenericParamDefKind::Lifetime => (),
176 GenericParamDefKind::Type { has_default, .. } => {
177 own_defaults.types += has_default as usize;
179 GenericParamDefKind::Const { has_default } => {
180 own_defaults.consts += has_default as usize;
188 pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool {
189 if self.own_requires_monomorphization() {
193 if let Some(parent_def_id) = self.parent {
194 let parent = tcx.generics_of(parent_def_id);
195 parent.requires_monomorphization(tcx)
201 pub fn own_requires_monomorphization(&self) -> bool {
202 for param in &self.params {
204 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
207 GenericParamDefKind::Lifetime => {}
213 /// Returns the `GenericParamDef` with the given index.
214 pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
215 if let Some(index) = param_index.checked_sub(self.parent_count) {
218 tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
219 .param_at(param_index, tcx)
223 /// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`.
226 param: &EarlyBoundRegion,
228 ) -> &'tcx GenericParamDef {
229 let param = self.param_at(param.index as usize, tcx);
231 GenericParamDefKind::Lifetime => param,
232 _ => bug!("expected lifetime parameter, but found another generic parameter"),
236 /// Returns the `GenericParamDef` associated with this `ParamTy`.
237 pub fn type_param(&'tcx self, param: &ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
238 let param = self.param_at(param.index as usize, tcx);
240 GenericParamDefKind::Type { .. } => param,
241 _ => bug!("expected type parameter, but found another generic parameter"),
245 /// Returns the `GenericParamDef` associated with this `ParamConst`.
246 pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
247 let param = self.param_at(param.index as usize, tcx);
249 GenericParamDefKind::Const { .. } => param,
250 _ => bug!("expected const parameter, but found another generic parameter"),
254 /// Returns `true` if `params` has `impl Trait`.
255 pub fn has_impl_trait(&'tcx self) -> bool {
256 self.params.iter().any(|param| {
257 matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. })
261 /// Returns the substs corresponding to the generic parameters
262 /// of this item, excluding `Self`.
264 /// **This should only be used for diagnostics purposes.**
265 pub fn own_substs_no_defaults(
268 substs: &'tcx [ty::GenericArg<'tcx>],
269 ) -> &'tcx [ty::GenericArg<'tcx>] {
270 let mut own_params = self.parent_count..self.count();
271 if self.has_self && self.parent.is_none() {
272 own_params.start = 1;
275 // Filter the default arguments.
277 // This currently uses structural equality instead
278 // of semantic equivalence. While not ideal, that's
279 // good enough for now as this should only be used
280 // for diagnostics anyways.
281 own_params.end -= self
285 .take_while(|param| {
286 param.default_value(tcx).map_or(false, |default| {
287 default.subst(tcx, substs) == substs[param.index as usize]
295 /// Returns the substs corresponding to the generic parameters of this item, excluding `Self`.
297 /// **This should only be used for diagnostics purposes.**
300 substs: &'tcx [ty::GenericArg<'tcx>],
301 ) -> &'tcx [ty::GenericArg<'tcx>] {
302 let own = &substs[self.parent_count..][..self.params.len()];
303 if self.has_self && self.parent.is_none() { &own[1..] } else { &own }
307 /// Bounds on generics.
308 #[derive(Copy, Clone, Default, Debug, TyEncodable, TyDecodable, HashStable)]
309 pub struct GenericPredicates<'tcx> {
310 pub parent: Option<DefId>,
311 pub predicates: &'tcx [(Predicate<'tcx>, Span)],
314 impl<'tcx> GenericPredicates<'tcx> {
318 substs: SubstsRef<'tcx>,
319 ) -> InstantiatedPredicates<'tcx> {
320 let mut instantiated = InstantiatedPredicates::empty();
321 self.instantiate_into(tcx, &mut instantiated, substs);
325 pub fn instantiate_own(
328 substs: SubstsRef<'tcx>,
329 ) -> InstantiatedPredicates<'tcx> {
330 InstantiatedPredicates {
334 .map(|(p, _)| EarlyBinder(*p).subst(tcx, substs))
336 spans: self.predicates.iter().map(|(_, sp)| *sp).collect(),
340 #[instrument(level = "debug", skip(self, tcx))]
344 instantiated: &mut InstantiatedPredicates<'tcx>,
345 substs: SubstsRef<'tcx>,
347 if let Some(def_id) = self.parent {
348 tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, substs);
352 .extend(self.predicates.iter().map(|(p, _)| EarlyBinder(*p).subst(tcx, substs)));
353 instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp));
356 pub fn instantiate_identity(&self, tcx: TyCtxt<'tcx>) -> InstantiatedPredicates<'tcx> {
357 let mut instantiated = InstantiatedPredicates::empty();
358 self.instantiate_identity_into(tcx, &mut instantiated);
362 fn instantiate_identity_into(
365 instantiated: &mut InstantiatedPredicates<'tcx>,
367 if let Some(def_id) = self.parent {
368 tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated);
370 instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p));
371 instantiated.spans.extend(self.predicates.iter().map(|(_, s)| s));