File tree 2 files changed +29
-5
lines changed
compiler/rustc_middle/src/ty
2 files changed +29
-5
lines changed Original file line number Diff line number Diff line change @@ -1729,12 +1729,33 @@ impl<'tcx> TyCtxt<'tcx> {
1729
1729
1730
1730
/// As long as the kind of `ty` is `TyAlias`, then it'll continue to peel it off and return
1731
1731
/// the type below it.
1732
- pub fn peel_off_ty_alias ( self , mut ty : Ty < ' tcx > ) -> Ty < ' tcx > {
1733
- while let ty:: TyAlias ( def_id, substs) = * ty. kind ( ) {
1734
- let binder_ty = self . bound_type_of ( def_id) ;
1735
- ty = binder_ty. subst ( self , substs) ;
1732
+ pub fn peel_off_ty_alias < T : ty:: TypeFoldable < ' tcx > > ( self , ty : T ) -> T {
1733
+ struct TyAliasPeeler < ' t > {
1734
+ tcx : TyCtxt < ' t > ,
1736
1735
}
1737
- ty
1736
+
1737
+ impl < ' t > ty:: TypeFolder < ' t > for TyAliasPeeler < ' t > {
1738
+ fn tcx < ' a > ( & ' a self ) -> TyCtxt < ' t > {
1739
+ self . tcx
1740
+ }
1741
+
1742
+ fn fold_ty ( & mut self , t : Ty < ' t > ) -> Ty < ' t > {
1743
+ use crate :: ty:: fold:: { TypeFoldable , TypeSuperFoldable } ;
1744
+ use crate :: ty:: visit:: TypeVisitable ;
1745
+
1746
+ match * t. kind ( ) {
1747
+ ty:: TyAlias ( def_id, substs) => {
1748
+ let binder_ty = self . tcx . bound_type_of ( def_id) ;
1749
+ let ty = binder_ty. subst ( self . tcx , substs) ;
1750
+ ty. fold_with ( self )
1751
+ }
1752
+ _ if !t. has_ty_alias ( ) => t,
1753
+ _ => t. super_fold_with ( self ) ,
1754
+ }
1755
+ }
1756
+ }
1757
+
1758
+ ty. fold_with ( & mut TyAliasPeeler { tcx : self } )
1738
1759
}
1739
1760
}
1740
1761
Original file line number Diff line number Diff line change @@ -92,6 +92,9 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
92
92
fn has_opaque_types ( & self ) -> bool {
93
93
self . has_type_flags ( TypeFlags :: HAS_TY_OPAQUE )
94
94
}
95
+ fn has_ty_alias ( & self ) -> bool {
96
+ self . has_type_flags ( TypeFlags :: HAS_TY_ALIAS )
97
+ }
95
98
fn references_error ( & self ) -> bool {
96
99
self . has_type_flags ( TypeFlags :: HAS_ERROR )
97
100
}
You can’t perform that action at this time.
0 commit comments