ty/walk: keep track of GenericArgs on the stack, instead of Tys.

This commit is contained in:
Eduard-Mihai Burtescu 2020-03-19 21:15:59 +02:00
parent 40158901d5
commit e53c42c0b3
2 changed files with 108 additions and 86 deletions

View File

@ -2698,14 +2698,14 @@ impl<'tcx> TyS<'tcx> {
/// [isize] => { [isize], isize } /// [isize] => { [isize], isize }
/// ``` /// ```
pub fn walk(&'tcx self) -> TypeWalker<'tcx> { pub fn walk(&'tcx self) -> TypeWalker<'tcx> {
TypeWalker::new(self) TypeWalker::new(self.into())
} }
/// Iterator that walks the immediate children of `self`. Hence /// Iterator that walks the immediate children of `self`. Hence
/// `Foo<Bar<i32>, u32>` yields the sequence `[Bar<i32>, u32]` /// `Foo<Bar<i32>, u32>` yields the sequence `[Bar<i32>, u32]`
/// (but not `i32`, like `walk`). /// (but not `i32`, like `walk`).
pub fn walk_shallow(&'tcx self) -> smallvec::IntoIter<walk::TypeWalkerArray<'tcx>> { pub fn walk_shallow(&'tcx self) -> impl Iterator<Item = Ty<'tcx>> {
walk::walk_shallow(self) walk::walk_shallow(self.into())
} }
/// Walks `ty` and any types appearing within `ty`, invoking the /// Walks `ty` and any types appearing within `ty`, invoking the

View File

@ -1,13 +1,13 @@
//! An iterator over the type substructure. //! An iterator over the type substructure.
//! WARNING: this does not keep track of the region depth. //! WARNING: this does not keep track of the region depth.
use crate::ty::subst::{GenericArg, GenericArgKind};
use crate::ty::{self, Ty}; use crate::ty::{self, Ty};
use smallvec::{self, SmallVec}; use smallvec::{self, SmallVec};
// The TypeWalker's stack is hot enough that it's worth going to some effort to // The TypeWalker's stack is hot enough that it's worth going to some effort to
// avoid heap allocations. // avoid heap allocations.
pub type TypeWalkerArray<'tcx> = [Ty<'tcx>; 8]; type TypeWalkerStack<'tcx> = SmallVec<[GenericArg<'tcx>; 8]>;
pub type TypeWalkerStack<'tcx> = SmallVec<TypeWalkerArray<'tcx>>;
pub struct TypeWalker<'tcx> { pub struct TypeWalker<'tcx> {
stack: TypeWalkerStack<'tcx>, stack: TypeWalkerStack<'tcx>,
@ -15,11 +15,11 @@ pub struct TypeWalker<'tcx> {
} }
impl<'tcx> TypeWalker<'tcx> { impl<'tcx> TypeWalker<'tcx> {
pub fn new(ty: Ty<'tcx>) -> TypeWalker<'tcx> { pub fn new(root: GenericArg<'tcx>) -> TypeWalker<'tcx> {
TypeWalker { stack: smallvec![ty], last_subtree: 1 } TypeWalker { stack: smallvec![root], last_subtree: 1 }
} }
/// Skips the subtree of types corresponding to the last type /// Skips the subtree corresponding to the last type
/// returned by `next()`. /// returned by `next()`.
/// ///
/// Example: Imagine you are walking `Foo<Bar<int>, usize>`. /// Example: Imagine you are walking `Foo<Bar<int>, usize>`.
@ -41,98 +41,120 @@ impl<'tcx> Iterator for TypeWalker<'tcx> {
fn next(&mut self) -> Option<Ty<'tcx>> { fn next(&mut self) -> Option<Ty<'tcx>> {
debug!("next(): stack={:?}", self.stack); debug!("next(): stack={:?}", self.stack);
match self.stack.pop() { while let Some(next) = self.stack.pop() {
None => None, self.last_subtree = self.stack.len();
Some(ty) => { push_inner(&mut self.stack, next);
self.last_subtree = self.stack.len(); debug!("next: stack={:?}", self.stack);
push_subtypes(&mut self.stack, ty);
debug!("next: stack={:?}", self.stack); // FIXME(eddyb) remove this filter and expose all `GenericArg`s.
Some(ty) match next.unpack() {
GenericArgKind::Type(ty) => return Some(ty),
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => {}
} }
} }
None
} }
} }
pub fn walk_shallow(ty: Ty<'_>) -> smallvec::IntoIter<TypeWalkerArray<'_>> { pub fn walk_shallow(parent: GenericArg<'tcx>) -> impl Iterator<Item = Ty<'tcx>> {
let mut stack = SmallVec::new(); let mut stack = SmallVec::new();
push_subtypes(&mut stack, ty); push_inner(&mut stack, parent);
stack.into_iter() stack.into_iter().filter_map(|child| {
// FIXME(eddyb) remove this filter and expose all `GenericArg`s.
match child.unpack() {
GenericArgKind::Type(ty) => Some(ty),
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => None,
}
})
} }
// We push types on the stack in reverse order so as to // We push `GenericArg`s on the stack in reverse order so as to
// maintain a pre-order traversal. As of the time of this // maintain a pre-order traversal. As of the time of this
// writing, the fact that the traversal is pre-order is not // writing, the fact that the traversal is pre-order is not
// known to be significant to any code, but it seems like the // known to be significant to any code, but it seems like the
// natural order one would expect (basically, the order of the // natural order one would expect (basically, the order of the
// types as they are written). // types as they are written).
fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) { fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) {
match parent_ty.kind { match parent.unpack() {
ty::Bool GenericArgKind::Type(parent_ty) => match parent_ty.kind {
| ty::Char ty::Bool
| ty::Int(_) | ty::Char
| ty::Uint(_) | ty::Int(_)
| ty::Float(_) | ty::Uint(_)
| ty::Str | ty::Float(_)
| ty::Infer(_) | ty::Str
| ty::Param(_) | ty::Infer(_)
| ty::Never | ty::Param(_)
| ty::Error | ty::Never
| ty::Placeholder(..) | ty::Error
| ty::Bound(..) | ty::Placeholder(..)
| ty::Foreign(..) => {} | ty::Bound(..)
ty::Array(ty, len) => { | ty::Foreign(..) => {}
if let ty::ConstKind::Unevaluated(_, substs, promoted) = len.val {
assert!(promoted.is_none());
stack.extend(substs.types().rev());
}
stack.push(len.ty);
stack.push(ty);
}
ty::Slice(ty) => {
stack.push(ty);
}
ty::RawPtr(ref mt) => {
stack.push(mt.ty);
}
ty::Ref(_, ty, _) => {
stack.push(ty);
}
ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
stack.extend(data.substs.types().rev());
}
ty::Dynamic(ref obj, ..) => {
stack.extend(obj.iter().rev().flat_map(|predicate| {
let (substs, opt_ty) = match *predicate.skip_binder() {
ty::ExistentialPredicate::Trait(tr) => (tr.substs, None),
ty::ExistentialPredicate::Projection(p) => (p.substs, Some(p.ty)),
ty::ExistentialPredicate::AutoTrait(_) =>
// Empty iterator
{
(ty::InternalSubsts::empty(), None)
}
};
substs.types().rev().chain(opt_ty) ty::Array(ty, len) => {
})); stack.push(len.into());
} stack.push(ty.into());
ty::Adt(_, substs) | ty::Opaque(_, substs) => { }
stack.extend(substs.types().rev()); ty::Slice(ty) => {
} stack.push(ty.into());
ty::Closure(_, ref substs) | ty::Generator(_, ref substs, _) => { }
stack.extend(substs.types().rev()); ty::RawPtr(mt) => {
} stack.push(mt.ty.into());
ty::GeneratorWitness(ts) => { }
stack.extend(ts.skip_binder().iter().cloned().rev()); ty::Ref(lt, ty, _) => {
} stack.push(ty.into());
ty::Tuple(..) => { stack.push(lt.into());
stack.extend(parent_ty.tuple_fields().rev()); }
} ty::Projection(data) | ty::UnnormalizedProjection(data) => {
ty::FnDef(_, substs) => { stack.extend(data.substs.iter().copied().rev());
stack.extend(substs.types().rev()); }
} ty::Dynamic(obj, lt) => {
ty::FnPtr(sig) => { stack.push(lt.into());
stack.push(sig.skip_binder().output()); stack.extend(obj.iter().rev().flat_map(|predicate| {
stack.extend(sig.skip_binder().inputs().iter().cloned().rev()); let (substs, opt_ty) = match *predicate.skip_binder() {
ty::ExistentialPredicate::Trait(tr) => (tr.substs, None),
ty::ExistentialPredicate::Projection(p) => (p.substs, Some(p.ty)),
ty::ExistentialPredicate::AutoTrait(_) =>
// Empty iterator
{
(ty::InternalSubsts::empty(), None)
}
};
substs.iter().copied().rev().chain(opt_ty.map(|ty| ty.into()))
}));
}
ty::Adt(_, substs)
| ty::Opaque(_, substs)
| ty::Closure(_, substs)
| ty::Generator(_, substs, _)
| ty::Tuple(substs)
| ty::FnDef(_, substs) => {
stack.extend(substs.iter().copied().rev());
}
ty::GeneratorWitness(ts) => {
stack.extend(ts.skip_binder().iter().cloned().rev().map(|ty| ty.into()));
}
ty::FnPtr(sig) => {
stack.push(sig.skip_binder().output().into());
stack.extend(sig.skip_binder().inputs().iter().cloned().rev().map(|ty| ty.into()));
}
},
GenericArgKind::Lifetime(_) => {}
GenericArgKind::Const(parent_ct) => {
stack.push(parent_ct.ty.into());
match parent_ct.val {
ty::ConstKind::Infer(_)
| ty::ConstKind::Param(_)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Bound(..)
| ty::ConstKind::Value(_) => {}
ty::ConstKind::Unevaluated(_, substs, _) => {
stack.extend(substs.iter().copied().rev());
}
}
} }
} }
} }