@@ -449,7 +449,7 @@ pub trait PrettyPrinter<'tcx>:
449
449
450
450
p ! ( print( self_ty) ) ;
451
451
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 ( ) ) ) ;
453
453
}
454
454
Ok ( cx)
455
455
} )
@@ -468,7 +468,7 @@ pub trait PrettyPrinter<'tcx>:
468
468
469
469
p ! ( write( "impl " ) ) ;
470
470
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 " ) ) ;
472
472
}
473
473
p ! ( print( self_ty) ) ;
474
474
@@ -619,7 +619,7 @@ pub trait PrettyPrinter<'tcx>:
619
619
620
620
p ! (
621
621
write( "{}" , if first { " " } else { "+" } ) ,
622
- print( trait_ref) ) ;
622
+ print( trait_ref. print_only_trait_path ( ) ) ) ;
623
623
first = false ;
624
624
}
625
625
}
@@ -1696,6 +1696,30 @@ impl fmt::Display for ty::RegionKind {
1696
1696
}
1697
1697
}
1698
1698
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
+
1699
1723
forward_display_to_print ! {
1700
1724
Ty <' tcx>,
1701
1725
& ' tcx ty:: List <ty:: ExistentialPredicate <' tcx>>,
@@ -1705,6 +1729,7 @@ forward_display_to_print! {
1705
1729
// because `for<'tcx>` isn't possible yet.
1706
1730
ty:: Binder <& ' tcx ty:: List <ty:: ExistentialPredicate <' tcx>>>,
1707
1731
ty:: Binder <ty:: TraitRef <' tcx>>,
1732
+ ty:: Binder <TraitRefPrintOnlyTraitPath <' tcx>>,
1708
1733
ty:: Binder <ty:: FnSig <' tcx>>,
1709
1734
ty:: Binder <ty:: TraitPredicate <' tcx>>,
1710
1735
ty:: Binder <ty:: SubtypePredicate <' tcx>>,
@@ -1739,7 +1764,7 @@ define_print_and_forward_display! {
1739
1764
// Use a type that can't appear in defaults of type parameters.
1740
1765
let dummy_self = cx. tcx( ) . mk_ty_infer( ty:: FreshTy ( 0 ) ) ;
1741
1766
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 ( ) ) )
1743
1768
}
1744
1769
1745
1770
ty:: ExistentialProjection <' tcx> {
@@ -1783,7 +1808,11 @@ define_print_and_forward_display! {
1783
1808
}
1784
1809
1785
1810
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) ) ;
1787
1816
}
1788
1817
1789
1818
ty:: ParamTy {
@@ -1799,7 +1828,8 @@ define_print_and_forward_display! {
1799
1828
}
1800
1829
1801
1830
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( ) ) )
1803
1833
}
1804
1834
1805
1835
ty:: ProjectionPredicate <' tcx> {
0 commit comments