]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/generics.rs
Add query for const_param_default
[rust.git] / compiler / rustc_middle / src / ty / generics.rs
1 use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
2 use crate::ty;
3 use crate::ty::subst::{Subst, SubstsRef};
4 use rustc_ast as ast;
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_hir as hir;
7 use rustc_hir::def_id::DefId;
8 use rustc_span::symbol::Symbol;
9 use rustc_span::Span;
10
11 use super::{EarlyBoundRegion, InstantiatedPredicates, ParamConst, ParamTy, Predicate, TyCtxt};
12
13 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
14 pub enum GenericParamDefKind {
15     Lifetime,
16     Type {
17         has_default: bool,
18         object_lifetime_default: ObjectLifetimeDefault,
19         synthetic: Option<hir::SyntheticTyParamKind>,
20     },
21     Const {
22         has_default: bool,
23     },
24 }
25
26 impl GenericParamDefKind {
27     pub fn descr(&self) -> &'static str {
28         match self {
29             GenericParamDefKind::Lifetime => "lifetime",
30             GenericParamDefKind::Type { .. } => "type",
31             GenericParamDefKind::Const { .. } => "constant",
32         }
33     }
34     pub fn to_ord(&self, tcx: TyCtxt<'_>) -> ast::ParamKindOrd {
35         match self {
36             GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
37             GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
38             GenericParamDefKind::Const { .. } => {
39                 ast::ParamKindOrd::Const { unordered: tcx.features().const_generics }
40             }
41         }
42     }
43 }
44
45 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
46 pub struct GenericParamDef {
47     pub name: Symbol,
48     pub def_id: DefId,
49     pub index: u32,
50
51     /// `pure_wrt_drop`, set by the (unsafe) `#[may_dangle]` attribute
52     /// on generic parameter `'a`/`T`, asserts data behind the parameter
53     /// `'a`/`T` won't be accessed during the parent type's `Drop` impl.
54     pub pure_wrt_drop: bool,
55
56     pub kind: GenericParamDefKind,
57 }
58
59 impl GenericParamDef {
60     pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion {
61         if let GenericParamDefKind::Lifetime = self.kind {
62             ty::EarlyBoundRegion { def_id: self.def_id, index: self.index, name: self.name }
63         } else {
64             bug!("cannot convert a non-lifetime parameter def to an early bound region")
65         }
66     }
67 }
68
69 #[derive(Default)]
70 pub struct GenericParamCount {
71     pub lifetimes: usize,
72     pub types: usize,
73     pub consts: usize,
74 }
75
76 /// Information about the formal type/lifetime parameters associated
77 /// with an item or method. Analogous to `hir::Generics`.
78 ///
79 /// The ordering of parameters is the same as in `Subst` (excluding child generics):
80 /// `Self` (optionally), `Lifetime` params..., `Type` params...
81 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
82 pub struct Generics {
83     pub parent: Option<DefId>,
84     pub parent_count: usize,
85     pub params: Vec<GenericParamDef>,
86
87     /// Reverse map to the `index` field of each `GenericParamDef`.
88     #[stable_hasher(ignore)]
89     pub param_def_id_to_index: FxHashMap<DefId, u32>,
90
91     pub has_self: bool,
92     pub has_late_bound_regions: Option<Span>,
93 }
94
95 impl<'tcx> Generics {
96     pub fn count(&self) -> usize {
97         self.parent_count + self.params.len()
98     }
99
100     pub fn own_counts(&self) -> GenericParamCount {
101         // We could cache this as a property of `GenericParamCount`, but
102         // the aim is to refactor this away entirely eventually and the
103         // presence of this method will be a constant reminder.
104         let mut own_counts = GenericParamCount::default();
105
106         for param in &self.params {
107             match param.kind {
108                 GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
109                 GenericParamDefKind::Type { .. } => own_counts.types += 1,
110                 GenericParamDefKind::Const { .. } => own_counts.consts += 1,
111             }
112         }
113
114         own_counts
115     }
116
117     pub fn own_defaults(&self) -> GenericParamCount {
118         let mut own_defaults = GenericParamCount::default();
119
120         for param in &self.params {
121             match param.kind {
122                 GenericParamDefKind::Lifetime => (),
123                 GenericParamDefKind::Type { has_default, .. }
124                 | GenericParamDefKind::Const { has_default } => {
125                     own_defaults.types += has_default as usize;
126                 }
127             }
128         }
129
130         own_defaults
131     }
132
133     pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool {
134         if self.own_requires_monomorphization() {
135             return true;
136         }
137
138         if let Some(parent_def_id) = self.parent {
139             let parent = tcx.generics_of(parent_def_id);
140             parent.requires_monomorphization(tcx)
141         } else {
142             false
143         }
144     }
145
146     pub fn own_requires_monomorphization(&self) -> bool {
147         for param in &self.params {
148             match param.kind {
149                 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
150                     return true;
151                 }
152                 GenericParamDefKind::Lifetime => {}
153             }
154         }
155         false
156     }
157
158     /// Returns the `GenericParamDef` with the given index.
159     pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
160         if let Some(index) = param_index.checked_sub(self.parent_count) {
161             &self.params[index]
162         } else {
163             tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
164                 .param_at(param_index, tcx)
165         }
166     }
167
168     /// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`.
169     pub fn region_param(
170         &'tcx self,
171         param: &EarlyBoundRegion,
172         tcx: TyCtxt<'tcx>,
173     ) -> &'tcx GenericParamDef {
174         let param = self.param_at(param.index as usize, tcx);
175         match param.kind {
176             GenericParamDefKind::Lifetime => param,
177             _ => bug!("expected lifetime parameter, but found another generic parameter"),
178         }
179     }
180
181     /// Returns the `GenericParamDef` associated with this `ParamTy`.
182     pub fn type_param(&'tcx self, param: &ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
183         let param = self.param_at(param.index as usize, tcx);
184         match param.kind {
185             GenericParamDefKind::Type { .. } => param,
186             _ => bug!("expected type parameter, but found another generic parameter"),
187         }
188     }
189
190     /// Returns the `GenericParamDef` associated with this `ParamConst`.
191     pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
192         let param = self.param_at(param.index as usize, tcx);
193         match param.kind {
194             GenericParamDefKind::Const { .. } => param,
195             _ => bug!("expected const parameter, but found another generic parameter"),
196         }
197     }
198 }
199
200 /// Bounds on generics.
201 #[derive(Copy, Clone, Default, Debug, TyEncodable, TyDecodable, HashStable)]
202 pub struct GenericPredicates<'tcx> {
203     pub parent: Option<DefId>,
204     pub predicates: &'tcx [(Predicate<'tcx>, Span)],
205 }
206
207 impl<'tcx> GenericPredicates<'tcx> {
208     pub fn instantiate(
209         &self,
210         tcx: TyCtxt<'tcx>,
211         substs: SubstsRef<'tcx>,
212     ) -> InstantiatedPredicates<'tcx> {
213         let mut instantiated = InstantiatedPredicates::empty();
214         self.instantiate_into(tcx, &mut instantiated, substs);
215         instantiated
216     }
217
218     pub fn instantiate_own(
219         &self,
220         tcx: TyCtxt<'tcx>,
221         substs: SubstsRef<'tcx>,
222     ) -> InstantiatedPredicates<'tcx> {
223         InstantiatedPredicates {
224             predicates: self.predicates.iter().map(|(p, _)| p.subst(tcx, substs)).collect(),
225             spans: self.predicates.iter().map(|(_, sp)| *sp).collect(),
226         }
227     }
228
229     fn instantiate_into(
230         &self,
231         tcx: TyCtxt<'tcx>,
232         instantiated: &mut InstantiatedPredicates<'tcx>,
233         substs: SubstsRef<'tcx>,
234     ) {
235         if let Some(def_id) = self.parent {
236             tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, substs);
237         }
238         instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p.subst(tcx, substs)));
239         instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp));
240     }
241
242     pub fn instantiate_identity(&self, tcx: TyCtxt<'tcx>) -> InstantiatedPredicates<'tcx> {
243         let mut instantiated = InstantiatedPredicates::empty();
244         self.instantiate_identity_into(tcx, &mut instantiated);
245         instantiated
246     }
247
248     fn instantiate_identity_into(
249         &self,
250         tcx: TyCtxt<'tcx>,
251         instantiated: &mut InstantiatedPredicates<'tcx>,
252     ) {
253         if let Some(def_id) = self.parent {
254             tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated);
255         }
256         instantiated.predicates.extend(self.predicates.iter().map(|(p, _)| p));
257         instantiated.spans.extend(self.predicates.iter().map(|(_, s)| s));
258     }
259 }