@@ -407,6 +407,7 @@ where
407407        match  * t. kind ( )  { 
408408            ty:: Bound ( debruijn,  bound_ty)  if  debruijn == self . current_index  => { 
409409                let  ty = self . delegate . replace_ty ( bound_ty) ; 
410+                 debug_assert ! ( !ty. has_vars_bound_above( ty:: INNERMOST ) ) ; 
410411                ty:: fold:: shift_vars ( self . tcx ,  ty,  self . current_index . as_u32 ( ) ) 
411412            } 
412413            _ if  t. has_vars_bound_at_or_above ( self . current_index )  => t. super_fold_with ( self ) , 
@@ -437,6 +438,7 @@ where
437438        match  ct. kind ( )  { 
438439            ty:: ConstKind :: Bound ( debruijn,  bound_const)  if  debruijn == self . current_index  => { 
439440                let  ct = self . delegate . replace_const ( bound_const,  ct. ty ( ) ) ; 
441+                 debug_assert ! ( !ct. has_vars_bound_above( ty:: INNERMOST ) ) ; 
440442                ty:: fold:: shift_vars ( self . tcx ,  ct,  self . current_index . as_u32 ( ) ) 
441443            } 
442444            _ => ct. super_fold_with ( self ) , 
@@ -697,46 +699,41 @@ impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> {
697699
698700    fn  fold_region ( & mut  self ,  r :  ty:: Region < ' tcx > )  -> ty:: Region < ' tcx >  { 
699701        match  * r { 
700-             ty:: ReLateBound ( debruijn,  br)  => { 
701-                 if  self . amount  == 0  || debruijn < self . current_index  { 
702-                     r
703-                 }  else  { 
704-                     let  debruijn = debruijn. shifted_in ( self . amount ) ; 
705-                     let  shifted = ty:: ReLateBound ( debruijn,  br) ; 
706-                     self . tcx . mk_region ( shifted) 
707-                 } 
702+             ty:: ReLateBound ( debruijn,  br)  if  debruijn >= self . current_index  => { 
703+                 let  debruijn = debruijn. shifted_in ( self . amount ) ; 
704+                 let  shifted = ty:: ReLateBound ( debruijn,  br) ; 
705+                 self . tcx . mk_region ( shifted) 
708706            } 
709707            _ => r, 
710708        } 
711709    } 
712710
713711    fn  fold_ty ( & mut  self ,  ty :  Ty < ' tcx > )  -> Ty < ' tcx >  { 
714712        match  * ty. kind ( )  { 
715-             ty:: Bound ( debruijn,  bound_ty)  => { 
716-                 if  self . amount  == 0  || debruijn < self . current_index  { 
717-                     ty
718-                 }  else  { 
719-                     let  debruijn = debruijn. shifted_in ( self . amount ) ; 
720-                     self . tcx . mk_ty ( ty:: Bound ( debruijn,  bound_ty) ) 
721-                 } 
713+             ty:: Bound ( debruijn,  bound_ty)  if  debruijn >= self . current_index  => { 
714+                 let  debruijn = debruijn. shifted_in ( self . amount ) ; 
715+                 self . tcx . mk_ty ( ty:: Bound ( debruijn,  bound_ty) ) 
722716            } 
723717
724-             _ => ty. super_fold_with ( self ) , 
718+             _ if  ty. has_vars_bound_at_or_above ( self . current_index )  => ty. super_fold_with ( self ) , 
719+             _ => ty, 
725720        } 
726721    } 
727722
728723    fn  fold_const ( & mut  self ,  ct :  ty:: Const < ' tcx > )  -> ty:: Const < ' tcx >  { 
729-         if  let  ty:: ConstKind :: Bound ( debruijn,  bound_ct)  = ct. kind ( )  { 
730-             if  self . amount  == 0  || debruijn < self . current_index  { 
731-                 ct
732-             }  else  { 
733-                 let  debruijn = debruijn. shifted_in ( self . amount ) ; 
734-                 self . tcx . mk_const ( ty:: ConstKind :: Bound ( debruijn,  bound_ct) ,  ct. ty ( ) ) 
735-             } 
724+         if  let  ty:: ConstKind :: Bound ( debruijn,  bound_ct)  = ct. kind ( ) 
725+             && debruijn >= self . current_index 
726+         { 
727+             let  debruijn = debruijn. shifted_in ( self . amount ) ; 
728+             self . tcx . mk_const ( ty:: ConstKind :: Bound ( debruijn,  bound_ct) ,  ct. ty ( ) ) 
736729        }  else  { 
737730            ct. super_fold_with ( self ) 
738731        } 
739732    } 
733+ 
734+     fn  fold_predicate ( & mut  self ,  p :  ty:: Predicate < ' tcx > )  -> ty:: Predicate < ' tcx >  { 
735+         if  p. has_vars_bound_at_or_above ( self . current_index )  {  p. super_fold_with ( self )  }  else  {  p } 
736+     } 
740737} 
741738
742739pub  fn  shift_region < ' tcx > ( 
@@ -758,5 +755,9 @@ where
758755{ 
759756    debug ! ( "shift_vars(value={:?}, amount={})" ,  value,  amount) ; 
760757
758+     if  amount == 0  || !value. has_escaping_bound_vars ( )  { 
759+         return  value; 
760+     } 
761+ 
761762    value. fold_with ( & mut  Shifter :: new ( tcx,  amount) ) 
762763} 
0 commit comments