]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/generics.rs
Be more careful about unresolved exprs in suggestion
[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
106 #[derive(Default)]
107 pub struct GenericParamCount {
108     pub lifetimes: usize,
109     pub types: usize,
110     pub consts: usize,
111 }
112
113 /// Information about the formal type/lifetime parameters associated
114 /// with an item or method. Analogous to `hir::Generics`.
115 ///
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>,
123
124     /// Reverse map to the `index` field of each `GenericParamDef`.
125     #[stable_hasher(ignore)]
126     pub param_def_id_to_index: FxHashMap<DefId, u32>,
127
128     pub has_self: bool,
129     pub has_late_bound_regions: Option<Span>,
130 }
131
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
136     /// generics.
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) {
139             Some(*idx)
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)
143         } else {
144             None
145         }
146     }
147
148     #[inline]
149     pub fn count(&self) -> usize {
150         self.parent_count + self.params.len()
151     }
152
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();
158
159         for param in &self.params {
160             match param.kind {
161                 GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
162                 GenericParamDefKind::Type { .. } => own_counts.types += 1,
163                 GenericParamDefKind::Const { .. } => own_counts.consts += 1,
164             }
165         }
166
167         own_counts
168     }
169
170     pub fn own_defaults(&self) -> GenericParamCount {
171         let mut own_defaults = GenericParamCount::default();
172
173         for param in &self.params {
174             match param.kind {
175                 GenericParamDefKind::Lifetime => (),
176                 GenericParamDefKind::Type { has_default, .. } => {
177                     own_defaults.types += has_default as usize;
178                 }
179                 GenericParamDefKind::Const { has_default } => {
180                     own_defaults.consts += has_default as usize;
181                 }
182             }
183         }
184
185         own_defaults
186     }
187
188     pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool {
189         if self.own_requires_monomorphization() {
190             return true;
191         }
192
193         if let Some(parent_def_id) = self.parent {
194             let parent = tcx.generics_of(parent_def_id);
195             parent.requires_monomorphization(tcx)
196         } else {
197             false
198         }
199     }
200
201     pub fn own_requires_monomorphization(&self) -> bool {
202         for param in &self.params {
203             match param.kind {
204                 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
205                     return true;
206                 }
207                 GenericParamDefKind::Lifetime => {}
208             }
209         }
210         false
211     }
212
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) {
216             &self.params[index]
217         } else {
218             tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
219                 .param_at(param_index, tcx)
220         }
221     }
222
223     /// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`.
224     pub fn region_param(
225         &'tcx self,
226         param: &EarlyBoundRegion,
227         tcx: TyCtxt<'tcx>,
228     ) -> &'tcx GenericParamDef {
229         let param = self.param_at(param.index as usize, tcx);
230         match param.kind {
231             GenericParamDefKind::Lifetime => param,
232             _ => bug!("expected lifetime parameter, but found another generic parameter"),
233         }
234     }
235
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);
239         match param.kind {
240             GenericParamDefKind::Type { .. } => param,
241             _ => bug!("expected type parameter, but found another generic parameter"),
242         }
243     }
244
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);
248         match param.kind {
249             GenericParamDefKind::Const { .. } => param,
250             _ => bug!("expected const parameter, but found another generic parameter"),
251         }
252     }
253
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, .. })
258         })
259     }
260
261     /// Returns the substs corresponding to the generic parameters
262     /// of this item, excluding `Self`.
263     ///
264     /// **This should only be used for diagnostics purposes.**
265     pub fn own_substs_no_defaults(
266         &'tcx self,
267         tcx: TyCtxt<'tcx>,
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;
273         }
274
275         // Filter the default arguments.
276         //
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
282             .params
283             .iter()
284             .rev()
285             .take_while(|param| {
286                 param.default_value(tcx).map_or(false, |default| {
287                     default.subst(tcx, substs) == substs[param.index as usize]
288                 })
289             })
290             .count();
291
292         &substs[own_params]
293     }
294
295     /// Returns the substs corresponding to the generic parameters of this item, excluding `Self`.
296     ///
297     /// **This should only be used for diagnostics purposes.**
298     pub fn own_substs(
299         &'tcx self,
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 }
304     }
305 }
306
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)],
312 }
313
314 impl<'tcx> GenericPredicates<'tcx> {
315     pub fn instantiate(
316         &self,
317         tcx: TyCtxt<'tcx>,
318         substs: SubstsRef<'tcx>,
319     ) -> InstantiatedPredicates<'tcx> {
320         let mut instantiated = InstantiatedPredicates::empty();
321         self.instantiate_into(tcx, &mut instantiated, substs);
322         instantiated
323     }
324
325     pub fn instantiate_own(
326         &self,
327         tcx: TyCtxt<'tcx>,
328         substs: SubstsRef<'tcx>,
329     ) -> InstantiatedPredicates<'tcx> {
330         InstantiatedPredicates {
331             predicates: self
332                 .predicates
333                 .iter()
334                 .map(|(p, _)| EarlyBinder(*p).subst(tcx, substs))
335                 .collect(),
336             spans: self.predicates.iter().map(|(_, sp)| *sp).collect(),
337         }
338     }
339
340     #[instrument(level = "debug", skip(self, tcx))]
341     fn instantiate_into(
342         &self,
343         tcx: TyCtxt<'tcx>,
344         instantiated: &mut InstantiatedPredicates<'tcx>,
345         substs: SubstsRef<'tcx>,
346     ) {
347         if let Some(def_id) = self.parent {
348             tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, substs);
349         }
350         instantiated
351             .predicates
352             .extend(self.predicates.iter().map(|(p, _)| EarlyBinder(*p).subst(tcx, substs)));
353         instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp));
354     }
355
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);
359         instantiated
360     }
361
362     fn instantiate_identity_into(
363         &self,
364         tcx: TyCtxt<'tcx>,
365         instantiated: &mut InstantiatedPredicates<'tcx>,
366     ) {
367         if let Some(def_id) = self.parent {
368             tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated);
369         }
370         instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p));
371         instantiated.spans.extend(self.predicates.iter().map(|(_, s)| s));
372     }
373 }