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