Skip to content

Commit cbf5d22

Browse files
varkoryodaldevoid
andcommitted
Add const type flags
Co-Authored-By: Gabriel Smith <yodaldevoid@users.noreply.github.com>
1 parent 29c272d commit cbf5d22

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/librustc/ty/flags.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::ty::subst::SubstsRef;
2-
use crate::ty::{self, Ty, TypeFlags, TypeFoldable};
1+
use crate::ty::subst::{SubstsRef, UnpackedKind};
2+
use crate::ty::{self, Ty, TypeFlags, TypeFoldable, InferConst};
3+
use crate::mir::interpret::ConstValue;
34

45
#[derive(Debug)]
56
pub struct FlagComputation {
@@ -232,6 +233,21 @@ impl FlagComputation {
232233
}
233234
}
234235

236+
fn add_const(&mut self, c: &ty::LazyConst<'_>) {
237+
match c {
238+
ty::LazyConst::Unevaluated(_, substs) => self.add_substs(substs),
239+
// Only done to add the binder for the type. The type flags are
240+
// included in `Const::type_flags`.
241+
ty::LazyConst::Evaluated(ty::Const { ty, val }) => {
242+
self.add_ty(ty);
243+
if let ConstValue::Infer(InferConst::Canonical(debruijn, _)) = val {
244+
self.add_binder(*debruijn)
245+
}
246+
}
247+
}
248+
self.add_flags(c.type_flags());
249+
}
250+
235251
fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
236252
self.add_substs(projection.substs);
237253
self.add_ty(projection.ty);
@@ -242,12 +258,12 @@ impl FlagComputation {
242258
}
243259

244260
fn add_substs(&mut self, substs: SubstsRef<'_>) {
245-
for ty in substs.types() {
246-
self.add_ty(ty);
247-
}
248-
249-
for r in substs.regions() {
250-
self.add_region(r);
261+
for kind in substs {
262+
match kind.unpack() {
263+
UnpackedKind::Type(ty) => self.add_ty(ty),
264+
UnpackedKind::Lifetime(lt) => self.add_region(lt),
265+
UnpackedKind::Const(ct) => self.add_const(ct),
266+
}
251267
}
252268
}
253269
}

src/librustc/ty/fold.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
9191
self.has_type_flags(TypeFlags::HAS_TY_INFER)
9292
}
9393
fn needs_infer(&self) -> bool {
94-
self.has_type_flags(TypeFlags::HAS_TY_INFER | TypeFlags::HAS_RE_INFER)
94+
self.has_type_flags(
95+
TypeFlags::HAS_TY_INFER | TypeFlags::HAS_RE_INFER | TypeFlags::HAS_CT_INFER
96+
)
9597
}
9698
fn has_placeholders(&self) -> bool {
9799
self.has_type_flags(TypeFlags::HAS_RE_PLACEHOLDER | TypeFlags::HAS_TY_PLACEHOLDER)
@@ -117,7 +119,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
117119
}
118120

119121
/// Indicates whether this value references only 'global'
120-
/// types/lifetimes that are the same regardless of what fn we are
122+
/// generic parameters that are the same regardless of what fn we are
121123
/// in. This is used for caching.
122124
fn is_global(&self) -> bool {
123125
!self.has_type_flags(TypeFlags::HAS_FREE_LOCAL_NAMES)
@@ -841,14 +843,13 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
841843
}
842844

843845
fn visit_const(&mut self, c: &'tcx ty::LazyConst<'tcx>) -> bool {
844-
if let ty::LazyConst::Unevaluated(..) = c {
845-
let projection_flags = TypeFlags::HAS_NORMALIZABLE_PROJECTION |
846-
TypeFlags::HAS_PROJECTION;
847-
if projection_flags.intersects(self.flags) {
848-
return true;
849-
}
846+
let flags = c.type_flags();
847+
debug!("HasTypeFlagsVisitor: c={:?} c.flags={:?} self.flags={:?}", c, flags, self.flags);
848+
if flags.intersects(self.flags) {
849+
true
850+
} else {
851+
c.super_visit_with(self)
850852
}
851-
c.super_visit_with(self)
852853
}
853854
}
854855

0 commit comments

Comments
 (0)