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