@@ -17,7 +17,7 @@ use smallvec::SmallVec;
1717use crate :: ty:: codec:: { TyDecoder , TyEncoder } ;
1818use crate :: ty:: {
1919 self , ClosureArgs , CoroutineArgs , CoroutineClosureArgs , FallibleTypeFolder , InlineConstArgs ,
20- Lift , List , Ty , TyCtxt , TypeFoldable , TypeVisitable , TypeVisitor , VisitorResult ,
20+ Lift , List , Ty , TyCtxt , TypeFoldable , TypeFolder , TypeVisitable , TypeVisitor , VisitorResult ,
2121 walk_visitable_list,
2222} ;
2323
@@ -337,6 +337,14 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for GenericArg<'tcx> {
337337 GenericArgKind :: Const ( ct) => ct. try_fold_with ( folder) . map ( Into :: into) ,
338338 }
339339 }
340+
341+ fn fold_with < F : TypeFolder < TyCtxt < ' tcx > > > ( self , folder : & mut F ) -> Self {
342+ match self . unpack ( ) {
343+ GenericArgKind :: Lifetime ( lt) => lt. fold_with ( folder) . into ( ) ,
344+ GenericArgKind :: Type ( ty) => ty. fold_with ( folder) . into ( ) ,
345+ GenericArgKind :: Const ( ct) => ct. fold_with ( folder) . into ( ) ,
346+ }
347+ }
340348}
341349
342350impl < ' tcx > TypeVisitable < TyCtxt < ' tcx > > for GenericArg < ' tcx > {
@@ -606,6 +614,27 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for GenericArgsRef<'tcx> {
606614 }
607615 }
608616 0 => Ok ( self ) ,
617+ _ => ty:: util:: try_fold_list ( self , folder, |tcx, v| tcx. mk_args ( v) ) ,
618+ }
619+ }
620+
621+ fn fold_with < F : TypeFolder < TyCtxt < ' tcx > > > ( self , folder : & mut F ) -> Self {
622+ // See justification for this behavior in `try_fold_with`.
623+ match self . len ( ) {
624+ 1 => {
625+ let param0 = self [ 0 ] . fold_with ( folder) ;
626+ if param0 == self [ 0 ] { self } else { folder. cx ( ) . mk_args ( & [ param0] ) }
627+ }
628+ 2 => {
629+ let param0 = self [ 0 ] . fold_with ( folder) ;
630+ let param1 = self [ 1 ] . fold_with ( folder) ;
631+ if param0 == self [ 0 ] && param1 == self [ 1 ] {
632+ self
633+ } else {
634+ folder. cx ( ) . mk_args ( & [ param0, param1] )
635+ }
636+ }
637+ 0 => self ,
609638 _ => ty:: util:: fold_list ( self , folder, |tcx, v| tcx. mk_args ( v) ) ,
610639 }
611640 }
@@ -641,6 +670,22 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<Ty<'tcx>> {
641670 Ok ( folder. cx ( ) . mk_type_list ( & [ param0, param1] ) )
642671 }
643672 }
673+ _ => ty:: util:: try_fold_list ( self , folder, |tcx, v| tcx. mk_type_list ( v) ) ,
674+ }
675+ }
676+
677+ fn fold_with < F : TypeFolder < TyCtxt < ' tcx > > > ( self , folder : & mut F ) -> Self {
678+ // See comment justifying behavior in `try_fold_with`.
679+ match self . len ( ) {
680+ 2 => {
681+ let param0 = self [ 0 ] . fold_with ( folder) ;
682+ let param1 = self [ 1 ] . fold_with ( folder) ;
683+ if param0 == self [ 0 ] && param1 == self [ 1 ] {
684+ self
685+ } else {
686+ folder. cx ( ) . mk_type_list ( & [ param0, param1] )
687+ }
688+ }
644689 _ => ty:: util:: fold_list ( self , folder, |tcx, v| tcx. mk_type_list ( v) ) ,
645690 }
646691 }
0 commit comments