@@ -2640,6 +2640,23 @@ impl<'tcx> fmt::Debug for TraitRefPrintOnlyTraitPath<'tcx> {
26402640 }
26412641}
26422642
2643+ /// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
2644+ /// the trait path, and additionally tries to "sugar" `Fn(...)` trait bounds.
2645+ #[ derive( Copy , Clone , TypeFoldable , TypeVisitable , Lift ) ]
2646+ pub struct TraitRefPrintSugared < ' tcx > ( ty:: TraitRef < ' tcx > ) ;
2647+
2648+ impl < ' tcx > rustc_errors:: IntoDiagnosticArg for TraitRefPrintSugared < ' tcx > {
2649+ fn into_diagnostic_arg ( self ) -> rustc_errors:: DiagnosticArgValue < ' static > {
2650+ self . to_string ( ) . into_diagnostic_arg ( )
2651+ }
2652+ }
2653+
2654+ impl < ' tcx > fmt:: Debug for TraitRefPrintSugared < ' tcx > {
2655+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2656+ fmt:: Display :: fmt ( self , f)
2657+ }
2658+ }
2659+
26432660/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
26442661/// the trait name. That is, it will print `Trait` instead of
26452662/// `<T as Trait<U>>`.
@@ -2657,6 +2674,10 @@ impl<'tcx> ty::TraitRef<'tcx> {
26572674 TraitRefPrintOnlyTraitPath ( self )
26582675 }
26592676
2677+ pub fn print_trait_sugared ( self ) -> TraitRefPrintSugared < ' tcx > {
2678+ TraitRefPrintSugared ( self )
2679+ }
2680+
26602681 pub fn print_only_trait_name ( self ) -> TraitRefPrintOnlyTraitName < ' tcx > {
26612682 TraitRefPrintOnlyTraitName ( self )
26622683 }
@@ -2666,6 +2687,10 @@ impl<'tcx> ty::Binder<'tcx, ty::TraitRef<'tcx>> {
26662687 pub fn print_only_trait_path ( self ) -> ty:: Binder < ' tcx , TraitRefPrintOnlyTraitPath < ' tcx > > {
26672688 self . map_bound ( |tr| tr. print_only_trait_path ( ) )
26682689 }
2690+
2691+ pub fn print_trait_sugared ( self ) -> ty:: Binder < ' tcx , TraitRefPrintSugared < ' tcx > > {
2692+ self . map_bound ( |tr| tr. print_trait_sugared ( ) )
2693+ }
26692694}
26702695
26712696#[ derive( Copy , Clone , TypeFoldable , TypeVisitable , Lift ) ]
@@ -2745,6 +2770,7 @@ forward_display_to_print! {
27452770 ty:: PolyExistentialTraitRef <' tcx>,
27462771 ty:: Binder <' tcx, ty:: TraitRef <' tcx>>,
27472772 ty:: Binder <' tcx, TraitRefPrintOnlyTraitPath <' tcx>>,
2773+ ty:: Binder <' tcx, TraitRefPrintSugared <' tcx>>,
27482774 ty:: Binder <' tcx, ty:: FnSig <' tcx>>,
27492775 ty:: Binder <' tcx, ty:: TraitPredicate <' tcx>>,
27502776 ty:: Binder <' tcx, TraitPredPrintModifiersAndPath <' tcx>>,
@@ -2844,6 +2870,24 @@ define_print_and_forward_display! {
28442870 p!( print_def_path( self . 0 . def_id, self . 0 . args) ) ;
28452871 }
28462872
2873+ TraitRefPrintSugared <' tcx> {
2874+ if !with_no_queries( )
2875+ && let Some ( kind) = cx. tcx( ) . fn_trait_kind_from_def_id( self . 0 . def_id)
2876+ && let ty:: Tuple ( args) = self . 0 . args. type_at( 1 ) . kind( )
2877+ {
2878+ p!( write( "{}" , kind. as_str( ) ) , "(" ) ;
2879+ for ( i, arg) in args. iter( ) . enumerate( ) {
2880+ if i > 0 {
2881+ p!( ", " ) ;
2882+ }
2883+ p!( print( arg) ) ;
2884+ }
2885+ p!( ")" ) ;
2886+ } else {
2887+ p!( print_def_path( self . 0 . def_id, self . 0 . args) ) ;
2888+ }
2889+ }
2890+
28472891 TraitRefPrintOnlyTraitName <' tcx> {
28482892 p!( print_def_path( self . 0 . def_id, & [ ] ) ) ;
28492893 }
@@ -2892,7 +2936,7 @@ define_print_and_forward_display! {
28922936 if let ty:: ImplPolarity :: Negative = self . polarity {
28932937 p!( "!" ) ;
28942938 }
2895- p!( print( self . trait_ref. print_only_trait_path ( ) ) )
2939+ p!( print( self . trait_ref. print_trait_sugared ( ) ) )
28962940 }
28972941
28982942 ty:: ProjectionPredicate <' tcx> {
0 commit comments