]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/flags.rs
Auto merge of #82980 - tmiasko:import-cold-multiplier, r=michaelwoerister
[rust.git] / compiler / rustc_middle / src / ty / flags.rs
1 use crate::ty::subst::{GenericArg, GenericArgKind};
2 use crate::ty::{self, InferConst, Ty, TypeFlags};
3 use std::slice;
4
5 #[derive(Debug)]
6 pub struct FlagComputation {
7     pub flags: TypeFlags,
8
9     // see `TyS::outer_exclusive_binder` for details
10     pub outer_exclusive_binder: ty::DebruijnIndex,
11 }
12
13 impl FlagComputation {
14     fn new() -> FlagComputation {
15         FlagComputation { flags: TypeFlags::empty(), outer_exclusive_binder: ty::INNERMOST }
16     }
17
18     #[allow(rustc::usage_of_ty_tykind)]
19     pub fn for_kind(kind: &ty::TyKind<'_>) -> FlagComputation {
20         let mut result = FlagComputation::new();
21         result.add_kind(kind);
22         result
23     }
24
25     pub fn for_predicate(binder: ty::Binder<ty::PredicateKind<'_>>) -> FlagComputation {
26         let mut result = FlagComputation::new();
27         result.add_predicate(binder);
28         result
29     }
30
31     pub fn for_const(c: &ty::Const<'_>) -> TypeFlags {
32         let mut result = FlagComputation::new();
33         result.add_const(c);
34         result.flags
35     }
36
37     fn add_flags(&mut self, flags: TypeFlags) {
38         self.flags = self.flags | flags;
39     }
40
41     /// indicates that `self` refers to something at binding level `binder`
42     fn add_bound_var(&mut self, binder: ty::DebruijnIndex) {
43         let exclusive_binder = binder.shifted_in(1);
44         self.add_exclusive_binder(exclusive_binder);
45     }
46
47     /// indicates that `self` refers to something *inside* binding
48     /// level `binder` -- not bound by `binder`, but bound by the next
49     /// binder internal to it
50     fn add_exclusive_binder(&mut self, exclusive_binder: ty::DebruijnIndex) {
51         self.outer_exclusive_binder = self.outer_exclusive_binder.max(exclusive_binder);
52     }
53
54     /// Adds the flags/depth from a set of types that appear within the current type, but within a
55     /// region binder.
56     fn bound_computation<T, F>(&mut self, value: ty::Binder<T>, f: F)
57     where
58         F: FnOnce(&mut Self, T),
59     {
60         let mut computation = FlagComputation::new();
61
62         f(&mut computation, value.skip_binder());
63
64         self.add_flags(computation.flags);
65
66         // The types that contributed to `computation` occurred within
67         // a region binder, so subtract one from the region depth
68         // within when adding the depth to `self`.
69         let outer_exclusive_binder = computation.outer_exclusive_binder;
70         if outer_exclusive_binder > ty::INNERMOST {
71             self.add_exclusive_binder(outer_exclusive_binder.shifted_out(1));
72         } // otherwise, this binder captures nothing
73     }
74
75     #[allow(rustc::usage_of_ty_tykind)]
76     fn add_kind(&mut self, kind: &ty::TyKind<'_>) {
77         match kind {
78             &ty::Bool
79             | &ty::Char
80             | &ty::Int(_)
81             | &ty::Float(_)
82             | &ty::Uint(_)
83             | &ty::Never
84             | &ty::Str
85             | &ty::Foreign(..) => {}
86
87             &ty::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
88
89             &ty::Param(_) => {
90                 self.add_flags(TypeFlags::HAS_TY_PARAM);
91                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
92             }
93
94             &ty::Generator(_, ref substs, _) => {
95                 let substs = substs.as_generator();
96                 let should_remove_further_specializable =
97                     !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
98                 self.add_substs(substs.parent_substs());
99                 if should_remove_further_specializable {
100                     self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE;
101                 }
102
103                 self.add_ty(substs.resume_ty());
104                 self.add_ty(substs.return_ty());
105                 self.add_ty(substs.witness());
106                 self.add_ty(substs.yield_ty());
107                 self.add_ty(substs.tupled_upvars_ty());
108             }
109
110             &ty::GeneratorWitness(ts) => {
111                 self.bound_computation(ts, |flags, ts| flags.add_tys(ts));
112             }
113
114             &ty::Closure(_, substs) => {
115                 let substs = substs.as_closure();
116                 let should_remove_further_specializable =
117                     !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
118                 self.add_substs(substs.parent_substs());
119                 if should_remove_further_specializable {
120                     self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE;
121                 }
122
123                 self.add_ty(substs.sig_as_fn_ptr_ty());
124                 self.add_ty(substs.kind_ty());
125                 self.add_ty(substs.tupled_upvars_ty());
126             }
127
128             &ty::Bound(debruijn, _) => {
129                 self.add_bound_var(debruijn);
130             }
131
132             &ty::Placeholder(..) => {
133                 self.add_flags(TypeFlags::HAS_TY_PLACEHOLDER);
134                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
135             }
136
137             &ty::Infer(infer) => {
138                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
139                 match infer {
140                     ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {}
141
142                     ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_) => {
143                         self.add_flags(TypeFlags::HAS_TY_INFER)
144                     }
145                 }
146             }
147
148             &ty::Adt(_, substs) => {
149                 self.add_substs(substs);
150             }
151
152             &ty::Projection(data) => {
153                 self.add_flags(TypeFlags::HAS_TY_PROJECTION);
154                 self.add_projection_ty(data);
155             }
156
157             &ty::Opaque(_, substs) => {
158                 self.add_flags(TypeFlags::HAS_TY_OPAQUE);
159                 self.add_substs(substs);
160             }
161
162             &ty::Dynamic(obj, r) => {
163                 for predicate in obj.iter() {
164                     self.bound_computation(predicate, |computation, predicate| match predicate {
165                         ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
166                         ty::ExistentialPredicate::Projection(p) => {
167                             computation.add_existential_projection(&p);
168                         }
169                         ty::ExistentialPredicate::AutoTrait(_) => {}
170                     });
171                 }
172
173                 self.add_region(r);
174             }
175
176             &ty::Array(tt, len) => {
177                 self.add_ty(tt);
178                 self.add_const(len);
179             }
180
181             &ty::Slice(tt) => self.add_ty(tt),
182
183             &ty::RawPtr(ref m) => {
184                 self.add_ty(m.ty);
185             }
186
187             &ty::Ref(r, ty, _) => {
188                 self.add_region(r);
189                 self.add_ty(ty);
190             }
191
192             &ty::Tuple(ref substs) => {
193                 self.add_substs(substs);
194             }
195
196             &ty::FnDef(_, substs) => {
197                 self.add_substs(substs);
198             }
199
200             &ty::FnPtr(fn_sig) => self.bound_computation(fn_sig, |computation, fn_sig| {
201                 computation.add_tys(fn_sig.inputs());
202                 computation.add_ty(fn_sig.output());
203             }),
204         }
205     }
206
207     fn add_predicate(&mut self, binder: ty::Binder<ty::PredicateKind<'_>>) {
208         self.bound_computation(binder, |computation, atom| computation.add_predicate_atom(atom));
209     }
210
211     fn add_predicate_atom(&mut self, atom: ty::PredicateKind<'_>) {
212         match atom {
213             ty::PredicateKind::Trait(trait_pred, _constness) => {
214                 self.add_substs(trait_pred.trait_ref.substs);
215             }
216             ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
217                 self.add_region(a);
218                 self.add_region(b);
219             }
220             ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, region)) => {
221                 self.add_ty(ty);
222                 self.add_region(region);
223             }
224             ty::PredicateKind::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => {
225                 self.add_ty(a);
226                 self.add_ty(b);
227             }
228             ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => {
229                 self.add_projection_ty(projection_ty);
230                 self.add_ty(ty);
231             }
232             ty::PredicateKind::WellFormed(arg) => {
233                 self.add_substs(slice::from_ref(&arg));
234             }
235             ty::PredicateKind::ObjectSafe(_def_id) => {}
236             ty::PredicateKind::ClosureKind(_def_id, substs, _kind) => {
237                 self.add_substs(substs);
238             }
239             ty::PredicateKind::ConstEvaluatable(_def_id, substs) => {
240                 self.add_substs(substs);
241             }
242             ty::PredicateKind::ConstEquate(expected, found) => {
243                 self.add_const(expected);
244                 self.add_const(found);
245             }
246             ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
247                 self.add_ty(ty);
248             }
249         }
250     }
251
252     fn add_ty(&mut self, ty: Ty<'_>) {
253         self.add_flags(ty.flags());
254         self.add_exclusive_binder(ty.outer_exclusive_binder);
255     }
256
257     fn add_tys(&mut self, tys: &[Ty<'_>]) {
258         for &ty in tys {
259             self.add_ty(ty);
260         }
261     }
262
263     fn add_region(&mut self, r: ty::Region<'_>) {
264         self.add_flags(r.type_flags());
265         if let ty::ReLateBound(debruijn, _) = *r {
266             self.add_bound_var(debruijn);
267         }
268     }
269
270     fn add_const(&mut self, c: &ty::Const<'_>) {
271         self.add_ty(c.ty);
272         match c.val {
273             ty::ConstKind::Unevaluated(unevaluated) => self.add_unevaluated_const(unevaluated),
274             ty::ConstKind::Infer(infer) => {
275                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
276                 match infer {
277                     InferConst::Fresh(_) => {}
278                     InferConst::Var(_) => self.add_flags(TypeFlags::HAS_CT_INFER),
279                 }
280             }
281             ty::ConstKind::Bound(debruijn, _) => {
282                 self.add_bound_var(debruijn);
283             }
284             ty::ConstKind::Param(_) => {
285                 self.add_flags(TypeFlags::HAS_CT_PARAM);
286                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
287             }
288             ty::ConstKind::Placeholder(_) => {
289                 self.add_flags(TypeFlags::HAS_CT_PLACEHOLDER);
290                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
291             }
292             ty::ConstKind::Value(_) => {}
293             ty::ConstKind::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
294         }
295     }
296
297     fn add_unevaluated_const(&mut self, ct: ty::Unevaluated<'tcx>) {
298         self.add_substs(ct.substs);
299         self.add_flags(TypeFlags::HAS_CT_PROJECTION);
300     }
301
302     fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
303         self.add_substs(projection.substs);
304         self.add_ty(projection.ty);
305     }
306
307     fn add_projection_ty(&mut self, projection_ty: ty::ProjectionTy<'_>) {
308         self.add_substs(projection_ty.substs);
309     }
310
311     fn add_substs(&mut self, substs: &[GenericArg<'_>]) {
312         for kind in substs {
313             match kind.unpack() {
314                 GenericArgKind::Type(ty) => self.add_ty(ty),
315                 GenericArgKind::Lifetime(lt) => self.add_region(lt),
316                 GenericArgKind::Const(ct) => self.add_const(ct),
317             }
318         }
319     }
320 }