@@ -14,8 +14,8 @@ use rustc_middle::mir::ConstraintCategory;
14
14
use rustc_middle:: ty:: query:: Providers ;
15
15
use rustc_middle:: ty:: trait_def:: TraitSpecializationKind ;
16
16
use rustc_middle:: ty:: {
17
- self , AdtKind , DefIdTree , GenericParamDefKind , ToPredicate , Ty , TyCtxt , TypeFoldable ,
18
- TypeSuperVisitable , TypeVisitable , TypeVisitor ,
17
+ self , AdtKind , DefIdTree , GenericParamDefKind , SubstsRef , ToPredicate , Ty , TyCtxt ,
18
+ TypeFoldable , TypeSuperVisitable , TypeVisitable , TypeVisitor ,
19
19
} ;
20
20
use rustc_middle:: ty:: { GenericArgKind , InternalSubsts } ;
21
21
use rustc_session:: parse:: feature_err;
@@ -239,6 +239,51 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
239
239
hir:: ItemKind :: TraitAlias ( ..) => {
240
240
check_trait ( tcx, item) ;
241
241
}
242
+ hir:: ItemKind :: TyAlias { .. } => {
243
+ struct TyAliasPeeler < ' t > {
244
+ tcx : TyCtxt < ' t > ,
245
+ visited : FxHashSet < ( DefId , SubstsRef < ' t > ) > ,
246
+ }
247
+
248
+ impl < ' t > ty:: TypeFolder < ' t > for TyAliasPeeler < ' t > {
249
+ fn tcx < ' a > ( & ' a self ) -> TyCtxt < ' t > {
250
+ self . tcx
251
+ }
252
+
253
+ fn fold_ty ( & mut self , t : Ty < ' t > ) -> Ty < ' t > {
254
+ use crate :: ty:: fold:: { TypeFoldable , TypeSuperFoldable } ;
255
+ use crate :: ty:: visit:: TypeVisitable ;
256
+
257
+ match * t. kind ( ) {
258
+ ty:: TyAlias ( def_id, substs) => {
259
+ if !self . visited . insert ( ( def_id, substs) ) {
260
+ let def_span = self . tcx . def_span ( def_id) ;
261
+ self . tcx
262
+ . sess
263
+ . struct_span_err (
264
+ def_span,
265
+ "cycle detected when expanding type alias" ,
266
+ )
267
+ . emit ( ) ;
268
+ return t;
269
+ }
270
+ let binder_ty = self . tcx . bound_type_of ( def_id) ;
271
+ let ty = binder_ty. subst ( self . tcx , substs) ;
272
+ ty. fold_with ( self )
273
+ }
274
+ _ if !t. has_ty_alias ( ) => t,
275
+ _ => t. super_fold_with ( self ) ,
276
+ }
277
+ }
278
+ }
279
+
280
+ let ty = tcx. bound_type_of ( item. def_id . to_def_id ( ) ) . 0 ;
281
+ if let ty:: TyAlias ( def_id, substs) = * ty. kind ( ) {
282
+ let binder_ty = tcx. bound_type_of ( def_id) ;
283
+ let ty = binder_ty. subst ( tcx, substs) ;
284
+ ty. fold_with ( & mut TyAliasPeeler { tcx, visited : FxHashSet :: default ( ) } ) ;
285
+ }
286
+ }
242
287
// `ForeignItem`s are handled separately.
243
288
hir:: ItemKind :: ForeignMod { .. } => { }
244
289
_ => { }
0 commit comments