11//! An iterator over the type substructure.
22//! WARNING: this does not keep track of the region depth.
33
4- use crate :: ty;
54use crate :: ty:: subst:: { GenericArg , GenericArgKind } ;
5+ use crate :: ty:: { self , TyCtxt } ;
66use rustc_data_structures:: sso:: SsoHashSet ;
77use smallvec:: { self , SmallVec } ;
88
@@ -11,6 +11,7 @@ use smallvec::{self, SmallVec};
1111type TypeWalkerStack < ' tcx > = SmallVec < [ GenericArg < ' tcx > ; 8 ] > ;
1212
1313pub struct TypeWalker < ' tcx > {
14+ tcx : TyCtxt < ' tcx > ,
1415 stack : TypeWalkerStack < ' tcx > ,
1516 last_subtree : usize ,
1617 pub visited : SsoHashSet < GenericArg < ' tcx > > ,
@@ -25,8 +26,8 @@ pub struct TypeWalker<'tcx> {
2526/// It maintains a set of visited types and
2627/// skips any types that are already there.
2728impl < ' tcx > TypeWalker < ' tcx > {
28- pub fn new ( root : GenericArg < ' tcx > ) -> Self {
29- Self { stack : smallvec ! [ root] , last_subtree : 1 , visited : SsoHashSet :: new ( ) }
29+ fn new ( tcx : TyCtxt < ' tcx > , root : GenericArg < ' tcx > ) -> Self {
30+ Self { tcx , stack : smallvec ! [ root] , last_subtree : 1 , visited : SsoHashSet :: new ( ) }
3031 }
3132
3233 /// Skips the subtree corresponding to the last type
@@ -55,7 +56,7 @@ impl<'tcx> Iterator for TypeWalker<'tcx> {
5556 let next = self . stack . pop ( ) ?;
5657 self . last_subtree = self . stack . len ( ) ;
5758 if self . visited . insert ( next) {
58- push_inner ( & mut self . stack , next) ;
59+ push_inner ( self . tcx , & mut self . stack , next) ;
5960 debug ! ( "next: stack={:?}" , self . stack) ;
6061 return Some ( next) ;
6162 }
@@ -74,8 +75,8 @@ impl GenericArg<'tcx> {
7475 /// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
7576 /// [isize] => { [isize], isize }
7677 /// ```
77- pub fn walk ( self ) -> TypeWalker < ' tcx > {
78- TypeWalker :: new ( self )
78+ pub fn walk ( self , tcx : TyCtxt < ' tcx > ) -> TypeWalker < ' tcx > {
79+ TypeWalker :: new ( tcx , self )
7980 }
8081
8182 /// Iterator that walks the immediate children of `self`. Hence
@@ -87,10 +88,11 @@ impl GenericArg<'tcx> {
8788 /// and skips any types that are already there.
8889 pub fn walk_shallow (
8990 self ,
91+ tcx : TyCtxt < ' tcx > ,
9092 visited : & mut SsoHashSet < GenericArg < ' tcx > > ,
9193 ) -> impl Iterator < Item = GenericArg < ' tcx > > {
9294 let mut stack = SmallVec :: new ( ) ;
93- push_inner ( & mut stack, self ) ;
95+ push_inner ( tcx , & mut stack, self ) ;
9496 stack. retain ( |a| visited. insert ( * a) ) ;
9597 stack. into_iter ( )
9698 }
@@ -107,18 +109,22 @@ impl<'tcx> super::TyS<'tcx> {
107109 /// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
108110 /// [isize] => { [isize], isize }
109111 /// ```
110- pub fn walk ( & ' tcx self ) -> TypeWalker < ' tcx > {
111- TypeWalker :: new ( self . into ( ) )
112+ pub fn walk ( & ' tcx self , tcx : TyCtxt < ' tcx > ) -> TypeWalker < ' tcx > {
113+ TypeWalker :: new ( tcx , self . into ( ) )
112114 }
113115}
114116
115- // We push `GenericArg`s on the stack in reverse order so as to
116- // maintain a pre-order traversal. As of the time of this
117- // writing, the fact that the traversal is pre-order is not
118- // known to be significant to any code, but it seems like the
119- // natural order one would expect (basically, the order of the
120- // types as they are written).
121- fn push_inner < ' tcx > ( stack : & mut TypeWalkerStack < ' tcx > , parent : GenericArg < ' tcx > ) {
117+ /// We push `GenericArg`s on the stack in reverse order so as to
118+ /// maintain a pre-order traversal. As of the time of this
119+ /// writing, the fact that the traversal is pre-order is not
120+ /// known to be significant to any code, but it seems like the
121+ /// natural order one would expect (basically, the order of the
122+ /// types as they are written).
123+ fn push_inner < ' tcx > (
124+ tcx : TyCtxt < ' tcx > ,
125+ stack : & mut TypeWalkerStack < ' tcx > ,
126+ parent : GenericArg < ' tcx > ,
127+ ) {
122128 match parent. unpack ( ) {
123129 GenericArgKind :: Type ( parent_ty) => match * parent_ty. kind ( ) {
124130 ty:: Bool
@@ -196,8 +202,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
196202 | ty:: ConstKind :: Error ( _) => { }
197203
198204 ty:: ConstKind :: Unevaluated ( ct) => {
199- // TODO
200- stack. extend ( ct. substs_ . unwrap ( ) . iter ( ) . rev ( ) ) ;
205+ stack. extend ( ct. substs ( tcx) . iter ( ) . rev ( ) ) ;
201206 }
202207 }
203208 }
0 commit comments