]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/ty/flags.rs
Merge branch 'master' into feature/incorporate-tracing
[rust.git] / src / librustc_middle / 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(kind: &ty::PredicateKind<'_>) -> FlagComputation {
26         let mut result = FlagComputation::new();
27         result.add_predicate_kind(kind);
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 add_bound_computation(&mut self, computation: FlagComputation) {
57         self.add_flags(computation.flags);
58
59         // The types that contributed to `computation` occurred within
60         // a region binder, so subtract one from the region depth
61         // within when adding the depth to `self`.
62         let outer_exclusive_binder = computation.outer_exclusive_binder;
63         if outer_exclusive_binder > ty::INNERMOST {
64             self.add_exclusive_binder(outer_exclusive_binder.shifted_out(1));
65         } // otherwise, this binder captures nothing
66     }
67
68     #[allow(rustc::usage_of_ty_tykind)]
69     fn add_kind(&mut self, kind: &ty::TyKind<'_>) {
70         match kind {
71             &ty::Bool
72             | &ty::Char
73             | &ty::Int(_)
74             | &ty::Float(_)
75             | &ty::Uint(_)
76             | &ty::Never
77             | &ty::Str
78             | &ty::Foreign(..) => {}
79
80             &ty::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
81
82             &ty::Param(_) => {
83                 self.add_flags(TypeFlags::HAS_TY_PARAM);
84                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
85             }
86
87             &ty::Generator(_, ref substs, _) => {
88                 self.add_flags(TypeFlags::MAY_POLYMORPHIZE);
89
90                 let substs = substs.as_generator();
91                 let should_remove_further_specializable =
92                     !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
93                 self.add_substs(substs.parent_substs());
94                 if should_remove_further_specializable {
95                     self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE;
96                 }
97
98                 self.add_ty(substs.resume_ty());
99                 self.add_ty(substs.return_ty());
100                 self.add_ty(substs.witness());
101                 self.add_ty(substs.yield_ty());
102                 self.add_ty(substs.tupled_upvars_ty());
103             }
104
105             &ty::GeneratorWitness(ts) => {
106                 let mut computation = FlagComputation::new();
107                 computation.add_tys(ts.skip_binder());
108                 self.add_bound_computation(computation);
109             }
110
111             &ty::Closure(_, substs) => {
112                 self.add_flags(TypeFlags::MAY_POLYMORPHIZE);
113
114                 let substs = substs.as_closure();
115                 let should_remove_further_specializable =
116                     !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
117                 self.add_substs(substs.parent_substs());
118                 if should_remove_further_specializable {
119                     self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE;
120                 }
121
122                 self.add_ty(substs.sig_as_fn_ptr_ty());
123                 self.add_ty(substs.kind_ty());
124                 self.add_ty(substs.tupled_upvars_ty());
125             }
126
127             &ty::Bound(debruijn, _) => {
128                 self.add_bound_var(debruijn);
129             }
130
131             &ty::Placeholder(..) => {
132                 self.add_flags(TypeFlags::HAS_TY_PLACEHOLDER);
133                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
134             }
135
136             &ty::Infer(infer) => {
137                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
138                 match infer {
139                     ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {}
140
141                     ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_) => {
142                         self.add_flags(TypeFlags::HAS_TY_INFER)
143                     }
144                 }
145             }
146
147             &ty::Adt(_, substs) => {
148                 self.add_substs(substs);
149             }
150
151             &ty::Projection(data) => {
152                 self.add_flags(TypeFlags::HAS_TY_PROJECTION);
153                 self.add_projection_ty(data);
154             }
155
156             &ty::Opaque(_, substs) => {
157                 self.add_flags(TypeFlags::HAS_TY_OPAQUE);
158                 self.add_substs(substs);
159             }
160
161             &ty::Dynamic(ref obj, r) => {
162                 let mut computation = FlagComputation::new();
163                 for predicate in obj.skip_binder().iter() {
164                     match predicate {
165                         ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
166                         ty::ExistentialPredicate::Projection(p) => {
167                             let mut proj_computation = FlagComputation::new();
168                             proj_computation.add_existential_projection(&p);
169                             self.add_bound_computation(proj_computation);
170                         }
171                         ty::ExistentialPredicate::AutoTrait(_) => {}
172                     }
173                 }
174                 self.add_bound_computation(computation);
175                 self.add_region(r);
176             }
177
178             &ty::Array(tt, len) => {
179                 self.add_ty(tt);
180                 self.add_const(len);
181             }
182
183             &ty::Slice(tt) => self.add_ty(tt),
184
185             &ty::RawPtr(ref m) => {
186                 self.add_ty(m.ty);
187             }
188
189             &ty::Ref(r, ty, _) => {
190                 self.add_region(r);
191                 self.add_ty(ty);
192             }
193
194             &ty::Tuple(ref substs) => {
195                 self.add_substs(substs);
196             }
197
198             &ty::FnDef(_, substs) => {
199                 self.add_flags(TypeFlags::MAY_POLYMORPHIZE);
200
201                 self.add_substs(substs);
202             }
203
204             &ty::FnPtr(f) => {
205                 self.add_fn_sig(f);
206             }
207         }
208     }
209
210     fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) {
211         match kind {
212             ty::PredicateKind::ForAll(binder) => {
213                 let mut computation = FlagComputation::new();
214
215                 computation.add_predicate_atom(binder.skip_binder());
216
217                 self.add_bound_computation(computation);
218             }
219             &ty::PredicateKind::Atom(atom) => self.add_predicate_atom(atom),
220         }
221     }
222
223     fn add_predicate_atom(&mut self, atom: ty::PredicateAtom<'_>) {
224         match atom {
225             ty::PredicateAtom::Trait(trait_pred, _constness) => {
226                 self.add_substs(trait_pred.trait_ref.substs);
227             }
228             ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
229                 self.add_region(a);
230                 self.add_region(b);
231             }
232             ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) => {
233                 self.add_ty(ty);
234                 self.add_region(region);
235             }
236             ty::PredicateAtom::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => {
237                 self.add_ty(a);
238                 self.add_ty(b);
239             }
240             ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty }) => {
241                 self.add_projection_ty(projection_ty);
242                 self.add_ty(ty);
243             }
244             ty::PredicateAtom::WellFormed(arg) => {
245                 self.add_substs(slice::from_ref(&arg));
246             }
247             ty::PredicateAtom::ObjectSafe(_def_id) => {}
248             ty::PredicateAtom::ClosureKind(_def_id, substs, _kind) => {
249                 self.add_substs(substs);
250             }
251             ty::PredicateAtom::ConstEvaluatable(_def_id, substs) => {
252                 self.add_substs(substs);
253             }
254             ty::PredicateAtom::ConstEquate(expected, found) => {
255                 self.add_const(expected);
256                 self.add_const(found);
257             }
258         }
259     }
260
261     fn add_ty(&mut self, ty: Ty<'_>) {
262         self.add_flags(ty.flags);
263         self.add_exclusive_binder(ty.outer_exclusive_binder);
264     }
265
266     fn add_tys(&mut self, tys: &[Ty<'_>]) {
267         for &ty in tys {
268             self.add_ty(ty);
269         }
270     }
271
272     fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig<'_>) {
273         let mut computation = FlagComputation::new();
274
275         computation.add_tys(fn_sig.skip_binder().inputs());
276         computation.add_ty(fn_sig.skip_binder().output());
277
278         self.add_bound_computation(computation);
279     }
280
281     fn add_region(&mut self, r: ty::Region<'_>) {
282         self.add_flags(r.type_flags());
283         if let ty::ReLateBound(debruijn, _) = *r {
284             self.add_bound_var(debruijn);
285         }
286     }
287
288     fn add_const(&mut self, c: &ty::Const<'_>) {
289         self.add_ty(c.ty);
290         match c.val {
291             ty::ConstKind::Unevaluated(_, substs, _) => {
292                 self.add_substs(substs);
293                 self.add_flags(TypeFlags::HAS_CT_PROJECTION);
294             }
295             ty::ConstKind::Infer(infer) => {
296                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
297                 match infer {
298                     InferConst::Fresh(_) => {}
299                     InferConst::Var(_) => self.add_flags(TypeFlags::HAS_CT_INFER),
300                 }
301             }
302             ty::ConstKind::Bound(debruijn, _) => {
303                 self.add_bound_var(debruijn);
304             }
305             ty::ConstKind::Param(_) => {
306                 self.add_flags(TypeFlags::HAS_CT_PARAM);
307                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
308             }
309             ty::ConstKind::Placeholder(_) => {
310                 self.add_flags(TypeFlags::HAS_CT_PLACEHOLDER);
311                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
312             }
313             ty::ConstKind::Value(_) => {}
314             ty::ConstKind::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
315         }
316     }
317
318     fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
319         self.add_substs(projection.substs);
320         self.add_ty(projection.ty);
321     }
322
323     fn add_projection_ty(&mut self, projection_ty: ty::ProjectionTy<'_>) {
324         self.add_substs(projection_ty.substs);
325     }
326
327     fn add_substs(&mut self, substs: &[GenericArg<'_>]) {
328         for kind in substs {
329             match kind.unpack() {
330                 GenericArgKind::Type(ty) => self.add_ty(ty),
331                 GenericArgKind::Lifetime(lt) => self.add_region(lt),
332                 GenericArgKind::Const(ct) => self.add_const(ct),
333             }
334         }
335     }
336 }