@@ -449,7 +449,7 @@ pub trait PrettyPrinter<'tcx>:
449449
450450 p ! ( print( self_ty) ) ;
451451 if let Some ( trait_ref) = trait_ref {
452- p ! ( write( " as " ) , print( trait_ref) ) ;
452+ p ! ( write( " as " ) , print( trait_ref. print_only_trait_path ( ) ) ) ;
453453 }
454454 Ok ( cx)
455455 } )
@@ -468,7 +468,7 @@ pub trait PrettyPrinter<'tcx>:
468468
469469 p ! ( write( "impl " ) ) ;
470470 if let Some ( trait_ref) = trait_ref {
471- p ! ( print( trait_ref) , write( " for " ) ) ;
471+ p ! ( print( trait_ref. print_only_trait_path ( ) ) , write( " for " ) ) ;
472472 }
473473 p ! ( print( self_ty) ) ;
474474
@@ -619,7 +619,7 @@ pub trait PrettyPrinter<'tcx>:
619619
620620 p ! (
621621 write( "{}" , if first { " " } else { "+" } ) ,
622- print( trait_ref) ) ;
622+ print( trait_ref. print_only_trait_path ( ) ) ) ;
623623 first = false ;
624624 }
625625 }
@@ -1696,6 +1696,30 @@ impl fmt::Display for ty::RegionKind {
16961696 }
16971697}
16981698
1699+ /// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
1700+ /// the trait path. That is, it will print `Trait<U>` instead of
1701+ /// `<T as Trait<U>>`.
1702+ #[ derive( Copy , Clone , TypeFoldable , Lift ) ]
1703+ pub struct TraitRefPrintOnlyTraitPath < ' tcx > ( ty:: TraitRef < ' tcx > ) ;
1704+
1705+ impl fmt:: Debug for TraitRefPrintOnlyTraitPath < ' tcx > {
1706+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1707+ fmt:: Display :: fmt ( self , f)
1708+ }
1709+ }
1710+
1711+ impl ty:: TraitRef < ' tcx > {
1712+ pub fn print_only_trait_path ( self ) -> TraitRefPrintOnlyTraitPath < ' tcx > {
1713+ TraitRefPrintOnlyTraitPath ( self )
1714+ }
1715+ }
1716+
1717+ impl ty:: Binder < ty:: TraitRef < ' tcx > > {
1718+ pub fn print_only_trait_path ( self ) -> ty:: Binder < TraitRefPrintOnlyTraitPath < ' tcx > > {
1719+ self . map_bound ( |tr| tr. print_only_trait_path ( ) )
1720+ }
1721+ }
1722+
16991723forward_display_to_print ! {
17001724 Ty <' tcx>,
17011725 & ' tcx ty:: List <ty:: ExistentialPredicate <' tcx>>,
@@ -1705,6 +1729,7 @@ forward_display_to_print! {
17051729 // because `for<'tcx>` isn't possible yet.
17061730 ty:: Binder <& ' tcx ty:: List <ty:: ExistentialPredicate <' tcx>>>,
17071731 ty:: Binder <ty:: TraitRef <' tcx>>,
1732+ ty:: Binder <TraitRefPrintOnlyTraitPath <' tcx>>,
17081733 ty:: Binder <ty:: FnSig <' tcx>>,
17091734 ty:: Binder <ty:: TraitPredicate <' tcx>>,
17101735 ty:: Binder <ty:: SubtypePredicate <' tcx>>,
@@ -1739,7 +1764,7 @@ define_print_and_forward_display! {
17391764 // Use a type that can't appear in defaults of type parameters.
17401765 let dummy_self = cx. tcx( ) . mk_ty_infer( ty:: FreshTy ( 0 ) ) ;
17411766 let trait_ref = self . with_self_ty( cx. tcx( ) , dummy_self) ;
1742- p!( print( trait_ref) )
1767+ p!( print( trait_ref. print_only_trait_path ( ) ) )
17431768 }
17441769
17451770 ty:: ExistentialProjection <' tcx> {
@@ -1783,7 +1808,11 @@ define_print_and_forward_display! {
17831808 }
17841809
17851810 ty:: TraitRef <' tcx> {
1786- p!( print_def_path( self . def_id, self . substs) ) ;
1811+ p!( write( "<{} as {}>" , self . self_ty( ) , self . print_only_trait_path( ) ) )
1812+ }
1813+
1814+ TraitRefPrintOnlyTraitPath <' tcx> {
1815+ p!( print_def_path( self . 0 . def_id, self . 0 . substs) ) ;
17871816 }
17881817
17891818 ty:: ParamTy {
@@ -1799,7 +1828,8 @@ define_print_and_forward_display! {
17991828 }
18001829
18011830 ty:: TraitPredicate <' tcx> {
1802- p!( print( self . trait_ref. self_ty( ) ) , write( ": " ) , print( self . trait_ref) )
1831+ p!( print( self . trait_ref. self_ty( ) ) , write( ": " ) ,
1832+ print( self . trait_ref. print_only_trait_path( ) ) )
18031833 }
18041834
18051835 ty:: ProjectionPredicate <' tcx> {
0 commit comments