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