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