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 self.add_substs(substs);
91 &ty::GeneratorWitness(ts) => {
92 let mut computation = FlagComputation::new();
93 computation.add_tys(ts.skip_binder());
94 self.add_bound_computation(computation);
97 &ty::Closure(_, substs) => {
98 self.add_substs(substs);
101 &ty::Bound(debruijn, _) => {
102 self.add_bound_var(debruijn);
105 &ty::Placeholder(..) => {
106 self.add_flags(TypeFlags::HAS_TY_PLACEHOLDER);
107 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
110 &ty::Infer(infer) => {
111 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
113 ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {}
115 ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_) => {
116 self.add_flags(TypeFlags::HAS_TY_INFER)
121 &ty::Adt(_, substs) => {
122 self.add_substs(substs);
125 &ty::Projection(data) => {
126 self.add_flags(TypeFlags::HAS_TY_PROJECTION);
127 self.add_projection_ty(data);
130 &ty::Opaque(_, substs) => {
131 self.add_flags(TypeFlags::HAS_TY_OPAQUE);
132 self.add_substs(substs);
135 &ty::Dynamic(ref obj, r) => {
136 let mut computation = FlagComputation::new();
137 for predicate in obj.skip_binder().iter() {
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);
145 ty::ExistentialPredicate::AutoTrait(_) => {}
148 self.add_bound_computation(computation);
152 &ty::Array(tt, len) => {
157 &ty::Slice(tt) => self.add_ty(tt),
159 &ty::RawPtr(ref m) => {
163 &ty::Ref(r, ty, _) => {
168 &ty::Tuple(ref substs) => {
169 self.add_substs(substs);
172 &ty::FnDef(_, substs) => {
173 self.add_substs(substs);
182 fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) {
184 ty::PredicateKind::Trait(trait_pred, _constness) => {
185 let mut computation = FlagComputation::new();
186 computation.add_substs(trait_pred.skip_binder().trait_ref.substs);
188 self.add_bound_computation(computation);
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);
196 self.add_bound_computation(computation);
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);
204 self.add_bound_computation(computation);
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);
212 self.add_bound_computation(computation);
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);
220 self.add_bound_computation(computation);
222 ty::PredicateKind::WellFormed(arg) => {
223 self.add_substs(slice::from_ref(arg));
225 ty::PredicateKind::ObjectSafe(_def_id) => {}
226 ty::PredicateKind::ClosureKind(_def_id, substs, _kind) => {
227 self.add_substs(substs);
229 ty::PredicateKind::ConstEvaluatable(_def_id, substs) => {
230 self.add_substs(substs);
232 ty::PredicateKind::ConstEquate(expected, found) => {
233 self.add_const(expected);
234 self.add_const(found);
239 fn add_ty(&mut self, ty: Ty<'_>) {
240 self.add_flags(ty.flags);
241 self.add_exclusive_binder(ty.outer_exclusive_binder);
244 fn add_tys(&mut self, tys: &[Ty<'_>]) {
250 fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig<'_>) {
251 let mut computation = FlagComputation::new();
253 computation.add_tys(fn_sig.skip_binder().inputs());
254 computation.add_ty(fn_sig.skip_binder().output());
256 self.add_bound_computation(computation);
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);
266 fn add_const(&mut self, c: &ty::Const<'_>) {
269 ty::ConstKind::Unevaluated(_, substs, _) => {
270 self.add_substs(substs);
271 self.add_flags(TypeFlags::HAS_CT_PROJECTION);
273 ty::ConstKind::Infer(infer) => {
274 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
276 InferConst::Fresh(_) => {}
277 InferConst::Var(_) => self.add_flags(TypeFlags::HAS_CT_INFER),
280 ty::ConstKind::Bound(debruijn, _) => {
281 self.add_bound_var(debruijn);
283 ty::ConstKind::Param(_) => {
284 self.add_flags(TypeFlags::HAS_CT_PARAM);
285 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
287 ty::ConstKind::Placeholder(_) => {
288 self.add_flags(TypeFlags::HAS_CT_PLACEHOLDER);
289 self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
291 ty::ConstKind::Value(_) => {}
292 ty::ConstKind::Error(_) => self.add_flags(TypeFlags::HAS_ERROR),
296 fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
297 self.add_substs(projection.substs);
298 self.add_ty(projection.ty);
301 fn add_projection_ty(&mut self, projection_ty: ty::ProjectionTy<'_>) {
302 self.add_substs(projection_ty.substs);
305 fn add_substs(&mut self, substs: &[GenericArg<'_>]) {
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),