1 use crate::ty::subst::{GenericArg, GenericArgKind};
2 use crate::ty::{self, InferConst, Ty, TypeFlags};
6 pub struct FlagComputation {
9 // see `TyS::outer_exclusive_binder` for details
10 pub outer_exclusive_binder: ty::DebruijnIndex,
13 impl FlagComputation {
14 fn new() -> FlagComputation {
15 FlagComputation { flags: TypeFlags::empty(), outer_exclusive_binder: ty::INNERMOST }
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);
25 pub fn for_predicate(kind: &ty::PredicateKind<'_>) -> FlagComputation {
26 let mut result = FlagComputation::new();
27 result.add_predicate_kind(kind);
31 pub fn for_const(c: &ty::Const<'_>) -> TypeFlags {
32 let mut result = FlagComputation::new();
37 fn add_flags(&mut self, flags: TypeFlags) {
38 self.flags = self.flags | flags;
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);
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);
54 /// Adds the flags/depth from a set of types that appear within the current type, but within a
56 fn add_bound_computation(&mut self, computation: FlagComputation) {
57 self.add_flags(computation.flags);
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
68 #[allow(rustc::usage_of_ty_tykind)]
69 fn add_kind(&mut self, kind: &ty::TyKind<'_>) {
78 | &ty::Foreign(..) => {}
80 &ty::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
83 self.add_flags(TypeFlags::HAS_TY_PARAM);
84 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
87 &ty::Generator(_, ref substs, _) => {
88 let substs = substs.as_generator();
89 let should_remove_further_specializable =
90 !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
91 self.add_substs(substs.parent_substs());
92 if should_remove_further_specializable {
93 self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE;
96 self.add_ty(substs.resume_ty());
97 self.add_ty(substs.return_ty());
98 self.add_ty(substs.witness());
99 self.add_ty(substs.yield_ty());
100 self.add_ty(substs.tupled_upvars_ty());
103 &ty::GeneratorWitness(ts) => {
104 let mut computation = FlagComputation::new();
105 computation.add_tys(ts.skip_binder());
106 self.add_bound_computation(computation);
109 &ty::Closure(_, substs) => {
110 let substs = substs.as_closure();
111 let should_remove_further_specializable =
112 !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
113 self.add_substs(substs.parent_substs());
114 if should_remove_further_specializable {
115 self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE;
118 self.add_ty(substs.sig_as_fn_ptr_ty());
119 self.add_ty(substs.kind_ty());
120 self.add_ty(substs.tupled_upvars_ty());
123 &ty::Bound(debruijn, _) => {
124 self.add_bound_var(debruijn);
127 &ty::Placeholder(..) => {
128 self.add_flags(TypeFlags::HAS_TY_PLACEHOLDER);
129 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
132 &ty::Infer(infer) => {
133 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
135 ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {}
137 ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_) => {
138 self.add_flags(TypeFlags::HAS_TY_INFER)
143 &ty::Adt(_, substs) => {
144 self.add_substs(substs);
147 &ty::Projection(data) => {
148 self.add_flags(TypeFlags::HAS_TY_PROJECTION);
149 self.add_projection_ty(data);
152 &ty::Opaque(_, substs) => {
153 self.add_flags(TypeFlags::HAS_TY_OPAQUE);
154 self.add_substs(substs);
157 &ty::Dynamic(ref obj, r) => {
158 let mut computation = FlagComputation::new();
159 for predicate in obj.skip_binder().iter() {
161 ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
162 ty::ExistentialPredicate::Projection(p) => {
163 let mut proj_computation = FlagComputation::new();
164 proj_computation.add_existential_projection(&p);
165 self.add_bound_computation(proj_computation);
167 ty::ExistentialPredicate::AutoTrait(_) => {}
170 self.add_bound_computation(computation);
174 &ty::Array(tt, len) => {
179 &ty::Slice(tt) => self.add_ty(tt),
181 &ty::RawPtr(ref m) => {
185 &ty::Ref(r, ty, _) => {
190 &ty::Tuple(ref substs) => {
191 self.add_substs(substs);
194 &ty::FnDef(_, substs) => {
195 self.add_substs(substs);
204 fn add_predicate(&mut self, pred: ty::Predicate<'_>) {
205 self.add_flags(pred.inner.flags);
206 self.add_exclusive_binder(pred.inner.outer_exclusive_binder);
209 fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) {
211 ty::PredicateKind::ForAll(binder) => {
212 let mut computation = FlagComputation::new();
214 computation.add_predicate(binder.skip_binder());
216 self.add_bound_computation(computation);
218 &ty::PredicateKind::Atom(atom) => match atom {
219 ty::PredicateAtom::Trait(trait_pred, _constness) => {
220 self.add_substs(trait_pred.trait_ref.substs);
222 ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
226 ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) => {
228 self.add_region(region);
230 ty::PredicateAtom::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => {
234 ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty }) => {
235 self.add_projection_ty(projection_ty);
238 ty::PredicateAtom::WellFormed(arg) => {
239 self.add_substs(slice::from_ref(&arg));
241 ty::PredicateAtom::ObjectSafe(_def_id) => {}
242 ty::PredicateAtom::ClosureKind(_def_id, substs, _kind) => {
243 self.add_substs(substs);
245 ty::PredicateAtom::ConstEvaluatable(_def_id, substs) => {
246 self.add_substs(substs);
248 ty::PredicateAtom::ConstEquate(expected, found) => {
249 self.add_const(expected);
250 self.add_const(found);
256 fn add_ty(&mut self, ty: Ty<'_>) {
257 self.add_flags(ty.flags);
258 self.add_exclusive_binder(ty.outer_exclusive_binder);
261 fn add_tys(&mut self, tys: &[Ty<'_>]) {
267 fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig<'_>) {
268 let mut computation = FlagComputation::new();
270 computation.add_tys(fn_sig.skip_binder().inputs());
271 computation.add_ty(fn_sig.skip_binder().output());
273 self.add_bound_computation(computation);
276 fn add_region(&mut self, r: ty::Region<'_>) {
277 self.add_flags(r.type_flags());
278 if let ty::ReLateBound(debruijn, _) = *r {
279 self.add_bound_var(debruijn);
283 fn add_const(&mut self, c: &ty::Const<'_>) {
286 ty::ConstKind::Unevaluated(_, substs, _) => {
287 self.add_substs(substs);
288 self.add_flags(TypeFlags::HAS_CT_PROJECTION);
290 ty::ConstKind::Infer(infer) => {
291 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
293 InferConst::Fresh(_) => {}
294 InferConst::Var(_) => self.add_flags(TypeFlags::HAS_CT_INFER),
297 ty::ConstKind::Bound(debruijn, _) => {
298 self.add_bound_var(debruijn);
300 ty::ConstKind::Param(_) => {
301 self.add_flags(TypeFlags::HAS_CT_PARAM);
302 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
304 ty::ConstKind::Placeholder(_) => {
305 self.add_flags(TypeFlags::HAS_CT_PLACEHOLDER);
306 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
308 ty::ConstKind::Value(_) => {}
309 ty::ConstKind::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
313 fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
314 self.add_substs(projection.substs);
315 self.add_ty(projection.ty);
318 fn add_projection_ty(&mut self, projection_ty: ty::ProjectionTy<'_>) {
319 self.add_substs(projection_ty.substs);
322 fn add_substs(&mut self, substs: &[GenericArg<'_>]) {
324 match kind.unpack() {
325 GenericArgKind::Type(ty) => self.add_ty(ty),
326 GenericArgKind::Lifetime(lt) => self.add_region(lt),
327 GenericArgKind::Const(ct) => self.add_const(ct),