@@ -384,15 +384,24 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
384
384
let lifted = self . lift_to_tcx ( cx. tcx ) . unwrap ( ) ;
385
385
let trait_ = lifted. trait_ref ( cx. tcx ) . clean ( cx) ;
386
386
let self_type = self . self_ty ( ) . clean ( cx) ;
387
+ let self_def_id = self_type. def_id ( & cx. cache ) ;
388
+ let should_show_cast = compute_should_show_cast ( self_def_id, & trait_, & self_type) ;
387
389
Type :: QPath {
388
390
assoc : Box :: new ( projection_to_path_segment ( * self , cx) ) ,
389
- self_def_id : self_type . def_id ( & cx . cache ) ,
391
+ should_show_cast ,
390
392
self_type : box self_type,
391
393
trait_,
392
394
}
393
395
}
394
396
}
395
397
398
+ fn compute_should_show_cast ( self_def_id : Option < DefId > , trait_ : & Path , self_type : & Type ) -> bool {
399
+ !trait_. segments . is_empty ( )
400
+ && self_def_id
401
+ . zip ( Some ( trait_. def_id ( ) ) )
402
+ . map_or ( !self_type. is_self_type ( ) , |( id, trait_) | id != trait_)
403
+ }
404
+
396
405
fn projection_to_path_segment ( ty : ty:: ProjectionTy < ' _ > , cx : & mut DocContext < ' _ > ) -> PathSegment {
397
406
let item = cx. tcx . associated_item ( ty. item_def_id ) ;
398
407
let generics = cx. tcx . generics_of ( ty. item_def_id ) ;
@@ -421,8 +430,12 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
421
430
// the cleaning process of the type itself. To resolve this and have the
422
431
// `self_def_id` set, we override it here.
423
432
// See https://github.com/rust-lang/rust/issues/85454
424
- if let QPath { ref mut self_def_id, .. } = default {
425
- * self_def_id = Some ( cx. tcx . parent ( self . def_id ) ) ;
433
+ if let QPath { ref mut should_show_cast, ref trait_, ref self_type, .. } =
434
+ default
435
+ {
436
+ let self_def_id = cx. tcx . parent ( self . def_id ) ;
437
+ * should_show_cast =
438
+ compute_should_show_cast ( self_def_id, trait_, self_type) ;
426
439
}
427
440
428
441
Some ( default)
@@ -1309,10 +1322,13 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
1309
1322
segments : trait_segments. iter ( ) . map ( |x| x. clean ( cx) ) . collect ( ) ,
1310
1323
} ;
1311
1324
register_res ( cx, trait_. res ) ;
1325
+ let self_def_id = DefId :: local ( qself. hir_id . owner . local_def_index ) ;
1326
+ let self_type = qself. clean ( cx) ;
1327
+ let should_show_cast = compute_should_show_cast ( Some ( self_def_id) , & trait_, & self_type) ;
1312
1328
Type :: QPath {
1313
1329
assoc : Box :: new ( p. segments . last ( ) . expect ( "segments were empty" ) . clean ( cx) ) ,
1314
- self_def_id : Some ( DefId :: local ( qself . hir_id . owner . local_def_index ) ) ,
1315
- self_type : box qself . clean ( cx ) ,
1330
+ should_show_cast ,
1331
+ self_type : box self_type ,
1316
1332
trait_,
1317
1333
}
1318
1334
}
@@ -1326,10 +1342,13 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
1326
1342
} ;
1327
1343
let trait_ = hir:: Path { span, res, segments : & [ ] } . clean ( cx) ;
1328
1344
register_res ( cx, trait_. res ) ;
1345
+ let self_def_id = res. opt_def_id ( ) ;
1346
+ let self_type = qself. clean ( cx) ;
1347
+ let should_show_cast = compute_should_show_cast ( self_def_id, & trait_, & self_type) ;
1329
1348
Type :: QPath {
1330
1349
assoc : Box :: new ( segment. clean ( cx) ) ,
1331
- self_def_id : res . opt_def_id ( ) ,
1332
- self_type : box qself . clean ( cx ) ,
1350
+ should_show_cast ,
1351
+ self_type : box self_type ,
1333
1352
trait_,
1334
1353
}
1335
1354
}
0 commit comments