1 use crate::ty; 2 use crate::ty::{EarlyBinder, SubstsRef}; 3 use rustc_ast as ast; 4 use rustc_data_structures::fx::FxHashMap; 5 use rustc_hir::def_id::DefId; 6 use rustc_span::symbol::{kw, Symbol}; 7 use rustc_span::Span; 8 9 use super::{Clause, EarlyBoundRegion, InstantiatedPredicates, ParamConst, ParamTy, Ty, TyCtxt}; 10 11 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)] 12 pub enum GenericParamDefKind { 13 Lifetime, 14 Type { has_default: bool, synthetic: bool }, 15 Const { has_default: bool }, 16 } 17 18 impl GenericParamDefKind { descr(&self) -> &'static str19 pub fn descr(&self) -> &'static str { 20 match self { 21 GenericParamDefKind::Lifetime => "lifetime", 22 GenericParamDefKind::Type { .. } => "type", 23 GenericParamDefKind::Const { .. } => "constant", 24 } 25 } to_ord(&self) -> ast::ParamKindOrd26 pub fn to_ord(&self) -> ast::ParamKindOrd { 27 match self { 28 GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime, 29 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => { 30 ast::ParamKindOrd::TypeOrConst 31 } 32 } 33 } 34 is_ty_or_const(&self) -> bool35 pub fn is_ty_or_const(&self) -> bool { 36 match self { 37 GenericParamDefKind::Lifetime => false, 38 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true, 39 } 40 } 41 is_synthetic(&self) -> bool42 pub fn is_synthetic(&self) -> bool { 43 match self { 44 GenericParamDefKind::Type { synthetic, .. } => *synthetic, 45 _ => false, 46 } 47 } 48 } 49 50 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)] 51 pub struct GenericParamDef { 52 pub name: Symbol, 53 pub def_id: DefId, 54 pub index: u32, 55 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, 60 61 pub kind: GenericParamDefKind, 62 } 63 64 impl GenericParamDef { to_early_bound_region_data(&self) -> ty::EarlyBoundRegion65 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 } 68 } else { 69 bug!("cannot convert a non-lifetime parameter def to an early bound region") 70 } 71 } 72 is_anonymous_lifetime(&self) -> bool73 pub fn is_anonymous_lifetime(&self) -> bool { 74 match self.kind { 75 GenericParamDefKind::Lifetime => { 76 self.name == kw::UnderscoreLifetime || self.name == kw::Empty 77 } 78 _ => false, 79 } 80 } 81 default_value<'tcx>( &self, tcx: TyCtxt<'tcx>, ) -> Option<EarlyBinder<ty::GenericArg<'tcx>>>82 pub fn default_value<'tcx>( 83 &self, 84 tcx: TyCtxt<'tcx>, 85 ) -> Option<EarlyBinder<ty::GenericArg<'tcx>>> { 86 match self.kind { 87 GenericParamDefKind::Type { has_default, .. } if has_default => { 88 Some(tcx.type_of(self.def_id).map_bound(|t| t.into())) 89 } 90 GenericParamDefKind::Const { has_default } if has_default => { 91 Some(tcx.const_param_default(self.def_id).map_bound(|c| c.into())) 92 } 93 _ => None, 94 } 95 } 96 to_error<'tcx>( &self, tcx: TyCtxt<'tcx>, preceding_substs: &[ty::GenericArg<'tcx>], ) -> ty::GenericArg<'tcx>97 pub fn to_error<'tcx>( 98 &self, 99 tcx: TyCtxt<'tcx>, 100 preceding_substs: &[ty::GenericArg<'tcx>], 101 ) -> ty::GenericArg<'tcx> { 102 match &self.kind { 103 ty::GenericParamDefKind::Lifetime => ty::Region::new_error_misc(tcx).into(), 104 ty::GenericParamDefKind::Type { .. } => Ty::new_misc_error(tcx).into(), 105 ty::GenericParamDefKind::Const { .. } => ty::Const::new_misc_error( 106 tcx, 107 tcx.type_of(self.def_id).subst(tcx, preceding_substs), 108 ) 109 .into(), 110 } 111 } 112 } 113 114 #[derive(Default)] 115 pub struct GenericParamCount { 116 pub lifetimes: usize, 117 pub types: usize, 118 pub consts: usize, 119 } 120 121 /// Information about the formal type/lifetime parameters associated 122 /// with an item or method. Analogous to `hir::Generics`. 123 /// 124 /// The ordering of parameters is the same as in `Subst` (excluding child generics): 125 /// `Self` (optionally), `Lifetime` params..., `Type` params... 126 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)] 127 pub struct Generics { 128 pub parent: Option<DefId>, 129 pub parent_count: usize, 130 pub params: Vec<GenericParamDef>, 131 132 /// Reverse map to the `index` field of each `GenericParamDef`. 133 #[stable_hasher(ignore)] 134 pub param_def_id_to_index: FxHashMap<DefId, u32>, 135 136 pub has_self: bool, 137 pub has_late_bound_regions: Option<Span>, 138 139 // The index of the host effect when substituted. (i.e. might be index to parent substs) 140 pub host_effect_index: Option<usize>, 141 } 142 143 impl<'tcx> Generics { 144 /// Looks through the generics and all parents to find the index of the 145 /// given param def-id. This is in comparison to the `param_def_id_to_index` 146 /// struct member, which only stores information about this item's own 147 /// generics. param_def_id_to_index(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<u32>148 pub fn param_def_id_to_index(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<u32> { 149 if let Some(idx) = self.param_def_id_to_index.get(&def_id) { 150 Some(*idx) 151 } else if let Some(parent) = self.parent { 152 let parent = tcx.generics_of(parent); 153 parent.param_def_id_to_index(tcx, def_id) 154 } else { 155 None 156 } 157 } 158 159 #[inline] count(&self) -> usize160 pub fn count(&self) -> usize { 161 self.parent_count + self.params.len() 162 } 163 own_counts(&self) -> GenericParamCount164 pub fn own_counts(&self) -> GenericParamCount { 165 // We could cache this as a property of `GenericParamCount`, but 166 // the aim is to refactor this away entirely eventually and the 167 // presence of this method will be a constant reminder. 168 let mut own_counts = GenericParamCount::default(); 169 170 for param in &self.params { 171 match param.kind { 172 GenericParamDefKind::Lifetime => own_counts.lifetimes += 1, 173 GenericParamDefKind::Type { .. } => own_counts.types += 1, 174 GenericParamDefKind::Const { .. } => own_counts.consts += 1, 175 } 176 } 177 178 own_counts 179 } 180 own_defaults(&self) -> GenericParamCount181 pub fn own_defaults(&self) -> GenericParamCount { 182 let mut own_defaults = GenericParamCount::default(); 183 184 for param in &self.params { 185 match param.kind { 186 GenericParamDefKind::Lifetime => (), 187 GenericParamDefKind::Type { has_default, .. } => { 188 own_defaults.types += has_default as usize; 189 } 190 GenericParamDefKind::Const { has_default } => { 191 own_defaults.consts += has_default as usize; 192 } 193 } 194 } 195 196 own_defaults 197 } 198 requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool199 pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool { 200 if self.own_requires_monomorphization() { 201 return true; 202 } 203 204 if let Some(parent_def_id) = self.parent { 205 let parent = tcx.generics_of(parent_def_id); 206 parent.requires_monomorphization(tcx) 207 } else { 208 false 209 } 210 } 211 own_requires_monomorphization(&self) -> bool212 pub fn own_requires_monomorphization(&self) -> bool { 213 for param in &self.params { 214 match param.kind { 215 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => { 216 return true; 217 } 218 GenericParamDefKind::Lifetime => {} 219 } 220 } 221 false 222 } 223 224 /// Returns the `GenericParamDef` with the given index. param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef225 pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef { 226 if let Some(index) = param_index.checked_sub(self.parent_count) { 227 &self.params[index] 228 } else { 229 tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?")) 230 .param_at(param_index, tcx) 231 } 232 } 233 params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef]234 pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] { 235 if let Some(index) = param_index.checked_sub(self.parent_count) { 236 &self.params[..index] 237 } else { 238 tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?")) 239 .params_to(param_index, tcx) 240 } 241 } 242 243 /// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`. region_param( &'tcx self, param: &EarlyBoundRegion, tcx: TyCtxt<'tcx>, ) -> &'tcx GenericParamDef244 pub fn region_param( 245 &'tcx self, 246 param: &EarlyBoundRegion, 247 tcx: TyCtxt<'tcx>, 248 ) -> &'tcx GenericParamDef { 249 let param = self.param_at(param.index as usize, tcx); 250 match param.kind { 251 GenericParamDefKind::Lifetime => param, 252 _ => bug!("expected lifetime parameter, but found another generic parameter"), 253 } 254 } 255 256 /// Returns the `GenericParamDef` associated with this `ParamTy`. type_param(&'tcx self, param: &ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef257 pub fn type_param(&'tcx self, param: &ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef { 258 let param = self.param_at(param.index as usize, tcx); 259 match param.kind { 260 GenericParamDefKind::Type { .. } => param, 261 _ => bug!("expected type parameter, but found another generic parameter"), 262 } 263 } 264 265 /// Returns the `GenericParamDef` associated with this `ParamConst`. const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef266 pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef { 267 let param = self.param_at(param.index as usize, tcx); 268 match param.kind { 269 GenericParamDefKind::Const { .. } => param, 270 _ => bug!("expected const parameter, but found another generic parameter"), 271 } 272 } 273 274 /// Returns `true` if `params` has `impl Trait`. has_impl_trait(&'tcx self) -> bool275 pub fn has_impl_trait(&'tcx self) -> bool { 276 self.params.iter().any(|param| { 277 matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. }) 278 }) 279 } 280 281 /// Returns the substs corresponding to the generic parameters 282 /// of this item, excluding `Self`. 283 /// 284 /// **This should only be used for diagnostics purposes.** own_substs_no_defaults( &'tcx self, tcx: TyCtxt<'tcx>, substs: &'tcx [ty::GenericArg<'tcx>], ) -> &'tcx [ty::GenericArg<'tcx>]285 pub fn own_substs_no_defaults( 286 &'tcx self, 287 tcx: TyCtxt<'tcx>, 288 substs: &'tcx [ty::GenericArg<'tcx>], 289 ) -> &'tcx [ty::GenericArg<'tcx>] { 290 let mut own_params = self.parent_count..self.count(); 291 if self.has_self && self.parent.is_none() { 292 own_params.start = 1; 293 } 294 295 // Filter the default arguments. 296 // 297 // This currently uses structural equality instead 298 // of semantic equivalence. While not ideal, that's 299 // good enough for now as this should only be used 300 // for diagnostics anyways. 301 own_params.end -= self 302 .params 303 .iter() 304 .rev() 305 .take_while(|param| { 306 param.default_value(tcx).is_some_and(|default| { 307 default.subst(tcx, substs) == substs[param.index as usize] 308 }) 309 }) 310 .count(); 311 312 &substs[own_params] 313 } 314 315 /// Returns the substs corresponding to the generic parameters of this item, excluding `Self`. 316 /// 317 /// **This should only be used for diagnostics purposes.** own_substs( &'tcx self, substs: &'tcx [ty::GenericArg<'tcx>], ) -> &'tcx [ty::GenericArg<'tcx>]318 pub fn own_substs( 319 &'tcx self, 320 substs: &'tcx [ty::GenericArg<'tcx>], 321 ) -> &'tcx [ty::GenericArg<'tcx>] { 322 let own = &substs[self.parent_count..][..self.params.len()]; 323 if self.has_self && self.parent.is_none() { &own[1..] } else { &own } 324 } 325 } 326 327 /// Bounds on generics. 328 #[derive(Copy, Clone, Default, Debug, TyEncodable, TyDecodable, HashStable)] 329 pub struct GenericPredicates<'tcx> { 330 pub parent: Option<DefId>, 331 pub predicates: &'tcx [(Clause<'tcx>, Span)], 332 } 333 334 impl<'tcx> GenericPredicates<'tcx> { instantiate( &self, tcx: TyCtxt<'tcx>, substs: SubstsRef<'tcx>, ) -> InstantiatedPredicates<'tcx>335 pub fn instantiate( 336 &self, 337 tcx: TyCtxt<'tcx>, 338 substs: SubstsRef<'tcx>, 339 ) -> InstantiatedPredicates<'tcx> { 340 let mut instantiated = InstantiatedPredicates::empty(); 341 self.instantiate_into(tcx, &mut instantiated, substs); 342 instantiated 343 } 344 instantiate_own( &self, tcx: TyCtxt<'tcx>, substs: SubstsRef<'tcx>, ) -> impl Iterator<Item = (Clause<'tcx>, Span)> + DoubleEndedIterator + ExactSizeIterator345 pub fn instantiate_own( 346 &self, 347 tcx: TyCtxt<'tcx>, 348 substs: SubstsRef<'tcx>, 349 ) -> impl Iterator<Item = (Clause<'tcx>, Span)> + DoubleEndedIterator + ExactSizeIterator { 350 EarlyBinder::bind(self.predicates).subst_iter_copied(tcx, substs) 351 } 352 353 #[instrument(level = "debug", skip(self, tcx))] instantiate_into( &self, tcx: TyCtxt<'tcx>, instantiated: &mut InstantiatedPredicates<'tcx>, substs: SubstsRef<'tcx>, )354 fn instantiate_into( 355 &self, 356 tcx: TyCtxt<'tcx>, 357 instantiated: &mut InstantiatedPredicates<'tcx>, 358 substs: SubstsRef<'tcx>, 359 ) { 360 if let Some(def_id) = self.parent { 361 tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, substs); 362 } 363 instantiated 364 .predicates 365 .extend(self.predicates.iter().map(|(p, _)| EarlyBinder::bind(*p).subst(tcx, substs))); 366 instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp)); 367 } 368 instantiate_identity(&self, tcx: TyCtxt<'tcx>) -> InstantiatedPredicates<'tcx>369 pub fn instantiate_identity(&self, tcx: TyCtxt<'tcx>) -> InstantiatedPredicates<'tcx> { 370 let mut instantiated = InstantiatedPredicates::empty(); 371 self.instantiate_identity_into(tcx, &mut instantiated); 372 instantiated 373 } 374 instantiate_identity_into( &self, tcx: TyCtxt<'tcx>, instantiated: &mut InstantiatedPredicates<'tcx>, )375 fn instantiate_identity_into( 376 &self, 377 tcx: TyCtxt<'tcx>, 378 instantiated: &mut InstantiatedPredicates<'tcx>, 379 ) { 380 if let Some(def_id) = self.parent { 381 tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated); 382 } 383 instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p)); 384 instantiated.spans.extend(self.predicates.iter().map(|(_, s)| s)); 385 } 386 } 387