@@ -384,15 +384,24 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
384384 let lifted = self . lift_to_tcx ( cx. tcx ) . unwrap ( ) ;
385385 let trait_ = lifted. trait_ref ( cx. tcx ) . clean ( cx) ;
386386 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) ;
387389 Type :: QPath {
388390 assoc : Box :: new ( projection_to_path_segment ( * self , cx) ) ,
389- self_def_id : self_type . def_id ( & cx . cache ) ,
391+ should_show_cast ,
390392 self_type : box self_type,
391393 trait_,
392394 }
393395 }
394396}
395397
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+
396405fn projection_to_path_segment ( ty : ty:: ProjectionTy < ' _ > , cx : & mut DocContext < ' _ > ) -> PathSegment {
397406 let item = cx. tcx . associated_item ( ty. item_def_id ) ;
398407 let generics = cx. tcx . generics_of ( ty. item_def_id ) ;
@@ -421,8 +430,12 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
421430 // the cleaning process of the type itself. To resolve this and have the
422431 // `self_def_id` set, we override it here.
423432 // 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) ;
426439 }
427440
428441 Some ( default)
@@ -1309,10 +1322,13 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
13091322 segments : trait_segments. iter ( ) . map ( |x| x. clean ( cx) ) . collect ( ) ,
13101323 } ;
13111324 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) ;
13121328 Type :: QPath {
13131329 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 ,
13161332 trait_,
13171333 }
13181334 }
@@ -1326,10 +1342,13 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
13261342 } ;
13271343 let trait_ = hir:: Path { span, res, segments : & [ ] } . clean ( cx) ;
13281344 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) ;
13291348 Type :: QPath {
13301349 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 ,
13331352 trait_,
13341353 }
13351354 }
0 commit comments