]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/ty/flags.rs
Rollup merge of #73883 - ehuss:rustdoc-stage-previous, r=Mark-Simulacrum
[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_substs(substs);
89             }
90
91             &ty::GeneratorWitness(ts) => {
92                 let mut computation = FlagComputation::new();
93                 computation.add_tys(ts.skip_binder());
94                 self.add_bound_computation(computation);
95             }
96
97             &ty::Closure(_, substs) => {
98                 self.add_substs(substs);
99             }
100
101             &ty::Bound(debruijn, _) => {
102                 self.add_bound_var(debruijn);
103             }
104
105             &ty::Placeholder(..) => {
106                 self.add_flags(TypeFlags::HAS_TY_PLACEHOLDER);
107                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
108             }
109
110             &ty::Infer(infer) => {
111                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
112                 match infer {
113                     ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {}
114
115                     ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_) => {
116                         self.add_flags(TypeFlags::HAS_TY_INFER)
117                     }
118                 }
119             }
120
121             &ty::Adt(_, substs) => {
122                 self.add_substs(substs);
123             }
124
125             &ty::Projection(data) => {
126                 self.add_flags(TypeFlags::HAS_TY_PROJECTION);
127                 self.add_projection_ty(data);
128             }
129
130             &ty::Opaque(_, substs) => {
131                 self.add_flags(TypeFlags::HAS_TY_OPAQUE);
132                 self.add_substs(substs);
133             }
134
135             &ty::Dynamic(ref obj, r) => {
136                 let mut computation = FlagComputation::new();
137                 for predicate in obj.skip_binder().iter() {
138                     match predicate {
139                         ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
140                         ty::ExistentialPredicate::Projection(p) => {
141                             let mut proj_computation = FlagComputation::new();
142                             proj_computation.add_existential_projection(&p);
143                             self.add_bound_computation(proj_computation);
144                         }
145                         ty::ExistentialPredicate::AutoTrait(_) => {}
146                     }
147                 }
148                 self.add_bound_computation(computation);
149                 self.add_region(r);
150             }
151
152             &ty::Array(tt, len) => {
153                 self.add_ty(tt);
154                 self.add_const(len);
155             }
156
157             &ty::Slice(tt) => self.add_ty(tt),
158
159             &ty::RawPtr(ref m) => {
160                 self.add_ty(m.ty);
161             }
162
163             &ty::Ref(r, ty, _) => {
164                 self.add_region(r);
165                 self.add_ty(ty);
166             }
167
168             &ty::Tuple(ref substs) => {
169                 self.add_substs(substs);
170             }
171
172             &ty::FnDef(_, substs) => {
173                 self.add_substs(substs);
174             }
175
176             &ty::FnPtr(f) => {
177                 self.add_fn_sig(f);
178             }
179         }
180     }
181
182     fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) {
183         match kind {
184             ty::PredicateKind::Trait(trait_pred, _constness) => {
185                 let mut computation = FlagComputation::new();
186                 computation.add_substs(trait_pred.skip_binder().trait_ref.substs);
187
188                 self.add_bound_computation(computation);
189             }
190             ty::PredicateKind::RegionOutlives(poly_outlives) => {
191                 let mut computation = FlagComputation::new();
192                 let ty::OutlivesPredicate(a, b) = poly_outlives.skip_binder();
193                 computation.add_region(a);
194                 computation.add_region(b);
195
196                 self.add_bound_computation(computation);
197             }
198             ty::PredicateKind::TypeOutlives(poly_outlives) => {
199                 let mut computation = FlagComputation::new();
200                 let ty::OutlivesPredicate(ty, region) = poly_outlives.skip_binder();
201                 computation.add_ty(ty);
202                 computation.add_region(region);
203
204                 self.add_bound_computation(computation);
205             }
206             ty::PredicateKind::Subtype(poly_subtype) => {
207                 let mut computation = FlagComputation::new();
208                 let ty::SubtypePredicate { a_is_expected: _, a, b } = poly_subtype.skip_binder();
209                 computation.add_ty(a);
210                 computation.add_ty(b);
211
212                 self.add_bound_computation(computation);
213             }
214             &ty::PredicateKind::Projection(projection) => {
215                 let mut computation = FlagComputation::new();
216                 let ty::ProjectionPredicate { projection_ty, ty } = projection.skip_binder();
217                 computation.add_projection_ty(projection_ty);
218                 computation.add_ty(ty);
219
220                 self.add_bound_computation(computation);
221             }
222             ty::PredicateKind::WellFormed(arg) => {
223                 self.add_substs(slice::from_ref(arg));
224             }
225             ty::PredicateKind::ObjectSafe(_def_id) => {}
226             ty::PredicateKind::ClosureKind(_def_id, substs, _kind) => {
227                 self.add_substs(substs);
228             }
229             ty::PredicateKind::ConstEvaluatable(_def_id, substs) => {
230                 self.add_substs(substs);
231             }
232             ty::PredicateKind::ConstEquate(expected, found) => {
233                 self.add_const(expected);
234                 self.add_const(found);
235             }
236         }
237     }
238
239     fn add_ty(&mut self, ty: Ty<'_>) {
240         self.add_flags(ty.flags);
241         self.add_exclusive_binder(ty.outer_exclusive_binder);
242     }
243
244     fn add_tys(&mut self, tys: &[Ty<'_>]) {
245         for &ty in tys {
246             self.add_ty(ty);
247         }
248     }
249
250     fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig<'_>) {
251         let mut computation = FlagComputation::new();
252
253         computation.add_tys(fn_sig.skip_binder().inputs());
254         computation.add_ty(fn_sig.skip_binder().output());
255
256         self.add_bound_computation(computation);
257     }
258
259     fn add_region(&mut self, r: ty::Region<'_>) {
260         self.add_flags(r.type_flags());
261         if let ty::ReLateBound(debruijn, _) = *r {
262             self.add_bound_var(debruijn);
263         }
264     }
265
266     fn add_const(&mut self, c: &ty::Const<'_>) {
267         self.add_ty(c.ty);
268         match c.val {
269             ty::ConstKind::Unevaluated(_, substs, _) => {
270                 self.add_substs(substs);
271                 self.add_flags(TypeFlags::HAS_CT_PROJECTION);
272             }
273             ty::ConstKind::Infer(infer) => {
274                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
275                 match infer {
276                     InferConst::Fresh(_) => {}
277                     InferConst::Var(_) => self.add_flags(TypeFlags::HAS_CT_INFER),
278                 }
279             }
280             ty::ConstKind::Bound(debruijn, _) => {
281                 self.add_bound_var(debruijn);
282             }
283             ty::ConstKind::Param(_) => {
284                 self.add_flags(TypeFlags::HAS_CT_PARAM);
285                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
286             }
287             ty::ConstKind::Placeholder(_) => {
288                 self.add_flags(TypeFlags::HAS_CT_PLACEHOLDER);
289                 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
290             }
291             ty::ConstKind::Value(_) => {}
292             ty::ConstKind::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
293         }
294     }
295
296     fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
297         self.add_substs(projection.substs);
298         self.add_ty(projection.ty);
299     }
300
301     fn add_projection_ty(&mut self, projection_ty: ty::ProjectionTy<'_>) {
302         self.add_substs(projection_ty.substs);
303     }
304
305     fn add_substs(&mut self, substs: &[GenericArg<'_>]) {
306         for kind in substs {
307             match kind.unpack() {
308                 GenericArgKind::Type(ty) => self.add_ty(ty),
309                 GenericArgKind::Lifetime(lt) => self.add_region(lt),
310                 GenericArgKind::Const(ct) => self.add_const(ct),
311             }
312         }
313     }
314 }