]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/flags.rs
Rollup merge of #63621 - jgalenson:dndebug, r=alexcrichton
[rust.git] / src / librustc / ty / flags.rs
1 use crate::ty::subst::{SubstsRef, UnpackedKind};
2 use crate::ty::{self, Ty, TypeFlags, TypeFoldable, InferConst};
3 use crate::mir::interpret::ConstValue;
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 {
16             flags: TypeFlags::empty(),
17             outer_exclusive_binder: ty::INNERMOST,
18         }
19     }
20
21     #[allow(rustc::usage_of_ty_tykind)]
22     pub fn for_sty(st: &ty::TyKind<'_>) -> FlagComputation {
23         let mut result = FlagComputation::new();
24         result.add_sty(st);
25         result
26     }
27
28     pub fn for_const(c: &ty::Const<'_>) -> TypeFlags {
29         let mut result = FlagComputation::new();
30         result.add_const(c);
31         result.flags
32     }
33
34     fn add_flags(&mut self, flags: TypeFlags) {
35         self.flags = self.flags | (flags & TypeFlags::NOMINAL_FLAGS);
36     }
37
38     /// indicates that `self` refers to something at binding level `binder`
39     fn add_binder(&mut self, binder: ty::DebruijnIndex) {
40         let exclusive_binder = binder.shifted_in(1);
41         self.add_exclusive_binder(exclusive_binder);
42     }
43
44     /// indicates that `self` refers to something *inside* binding
45     /// level `binder` -- not bound by `binder`, but bound by the next
46     /// binder internal to it
47     fn add_exclusive_binder(&mut self, exclusive_binder: ty::DebruijnIndex) {
48         self.outer_exclusive_binder = self.outer_exclusive_binder.max(exclusive_binder);
49     }
50
51     /// Adds the flags/depth from a set of types that appear within the current type, but within a
52     /// region binder.
53     fn add_bound_computation(&mut self, computation: &FlagComputation) {
54         self.add_flags(computation.flags);
55
56         // The types that contributed to `computation` occurred within
57         // a region binder, so subtract one from the region depth
58         // within when adding the depth to `self`.
59         let outer_exclusive_binder = computation.outer_exclusive_binder;
60         if outer_exclusive_binder > ty::INNERMOST {
61             self.add_exclusive_binder(outer_exclusive_binder.shifted_out(1));
62         } // otherwise, this binder captures nothing
63     }
64
65     #[allow(rustc::usage_of_ty_tykind)]
66     fn add_sty(&mut self, st: &ty::TyKind<'_>) {
67         match st {
68             &ty::Bool |
69             &ty::Char |
70             &ty::Int(_) |
71             &ty::Float(_) |
72             &ty::Uint(_) |
73             &ty::Never |
74             &ty::Str |
75             &ty::Foreign(..) => {
76             }
77
78             // You might think that we could just return Error for
79             // any type containing Error as a component, and get
80             // rid of the TypeFlags::HAS_TY_ERR flag -- likewise for ty_bot (with
81             // the exception of function types that return bot).
82             // But doing so caused sporadic memory corruption, and
83             // neither I (tjc) nor nmatsakis could figure out why,
84             // so we're doing it this way.
85             &ty::Error => {
86                 self.add_flags(TypeFlags::HAS_TY_ERR)
87             }
88
89             &ty::Param(ref p) => {
90                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
91                 if p.is_self() {
92                     self.add_flags(TypeFlags::HAS_SELF);
93                 } else {
94                     self.add_flags(TypeFlags::HAS_PARAMS);
95                 }
96             }
97
98             &ty::Generator(_, ref substs, _) => {
99                 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
100                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
101                 self.add_substs(&substs.substs);
102             }
103
104             &ty::GeneratorWitness(ref ts) => {
105                 let mut computation = FlagComputation::new();
106                 computation.add_tys(&ts.skip_binder()[..]);
107                 self.add_bound_computation(&computation);
108             }
109
110             &ty::Closure(_, ref substs) => {
111                 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
112                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
113                 self.add_substs(&substs.substs);
114             }
115
116             &ty::Bound(debruijn, _) => {
117                 self.add_binder(debruijn);
118             }
119
120             &ty::Placeholder(..) => {
121                 self.add_flags(TypeFlags::HAS_TY_PLACEHOLDER);
122             }
123
124             &ty::Infer(infer) => {
125                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES); // it might, right?
126                 self.add_flags(TypeFlags::HAS_TY_INFER);
127                 match infer {
128                     ty::FreshTy(_) |
129                     ty::FreshIntTy(_) |
130                     ty::FreshFloatTy(_) => {
131                     }
132
133                     ty::TyVar(_) |
134                     ty::IntVar(_) |
135                     ty::FloatVar(_) => {
136                         self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
137                     }
138                 }
139             }
140
141             &ty::Adt(_, substs) => {
142                 self.add_substs(substs);
143             }
144
145             &ty::Projection(ref data) => {
146                 // currently we can't normalize projections that
147                 // include bound regions, so track those separately.
148                 if !data.has_escaping_bound_vars() {
149                     self.add_flags(TypeFlags::HAS_NORMALIZABLE_PROJECTION);
150                 }
151                 self.add_flags(TypeFlags::HAS_PROJECTION);
152                 self.add_projection_ty(data);
153             }
154
155             &ty::UnnormalizedProjection(ref data) => {
156                 self.add_flags(TypeFlags::HAS_PROJECTION);
157                 self.add_projection_ty(data);
158             },
159
160             &ty::Opaque(_, substs) => {
161                 self.add_flags(TypeFlags::HAS_PROJECTION);
162                 self.add_substs(substs);
163             }
164
165             &ty::Dynamic(ref obj, r) => {
166                 let mut computation = FlagComputation::new();
167                 for predicate in obj.skip_binder().iter() {
168                     match *predicate {
169                         ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
170                         ty::ExistentialPredicate::Projection(p) => {
171                             let mut proj_computation = FlagComputation::new();
172                             proj_computation.add_existential_projection(&p);
173                             self.add_bound_computation(&proj_computation);
174                         }
175                         ty::ExistentialPredicate::AutoTrait(_) => {}
176                     }
177                 }
178                 self.add_bound_computation(&computation);
179                 self.add_region(r);
180             }
181
182             &ty::Array(tt, len) => {
183                 self.add_ty(tt);
184                 self.add_const(len);
185             }
186
187             &ty::Slice(tt) => {
188                 self.add_ty(tt)
189             }
190
191             &ty::RawPtr(ref m) => {
192                 self.add_ty(m.ty);
193             }
194
195             &ty::Ref(r, ty, _) => {
196                 self.add_region(r);
197                 self.add_ty(ty);
198             }
199
200             &ty::Tuple(ref substs) => {
201                 self.add_substs(substs);
202             }
203
204             &ty::FnDef(_, substs) => {
205                 self.add_substs(substs);
206             }
207
208             &ty::FnPtr(f) => {
209                 self.add_fn_sig(f);
210             }
211         }
212     }
213
214     fn add_ty(&mut self, ty: Ty<'_>) {
215         self.add_flags(ty.flags);
216         self.add_exclusive_binder(ty.outer_exclusive_binder);
217     }
218
219     fn add_tys(&mut self, tys: &[Ty<'_>]) {
220         for &ty in tys {
221             self.add_ty(ty);
222         }
223     }
224
225     fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig<'_>) {
226         let mut computation = FlagComputation::new();
227
228         computation.add_tys(fn_sig.skip_binder().inputs());
229         computation.add_ty(fn_sig.skip_binder().output());
230
231         self.add_bound_computation(&computation);
232     }
233
234     fn add_region(&mut self, r: ty::Region<'_>) {
235         self.add_flags(r.type_flags());
236         if let ty::ReLateBound(debruijn, _) = *r {
237             self.add_binder(debruijn);
238         }
239     }
240
241     fn add_const(&mut self, c: &ty::Const<'_>) {
242         self.add_ty(c.ty);
243         match c.val {
244             ConstValue::Unevaluated(_, substs) => {
245                 self.add_substs(substs);
246                 self.add_flags(TypeFlags::HAS_NORMALIZABLE_PROJECTION | TypeFlags::HAS_PROJECTION);
247             },
248             ConstValue::Infer(infer) => {
249                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES | TypeFlags::HAS_CT_INFER);
250                 match infer {
251                     InferConst::Fresh(_) => {}
252                     InferConst::Canonical(debruijn, _) => self.add_binder(debruijn),
253                     InferConst::Var(_) => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX),
254                 }
255             }
256             ConstValue::Param(_) => {
257                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES | TypeFlags::HAS_PARAMS);
258             }
259             ConstValue::Placeholder(_) => {
260                 self.add_flags(TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_CT_PLACEHOLDER);
261             }
262             _ => {},
263         }
264     }
265
266     fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
267         self.add_substs(projection.substs);
268         self.add_ty(projection.ty);
269     }
270
271     fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy<'_>) {
272         self.add_substs(projection_ty.substs);
273     }
274
275     fn add_substs(&mut self, substs: SubstsRef<'_>) {
276         for kind in substs {
277             match kind.unpack() {
278                 UnpackedKind::Type(ty) => self.add_ty(ty),
279                 UnpackedKind::Lifetime(lt) => self.add_region(lt),
280                 UnpackedKind::Const(ct) => self.add_const(ct),
281             }
282         }
283     }
284 }