@@ -2637,6 +2637,23 @@ impl<'tcx> fmt::Debug for TraitRefPrintOnlyTraitPath<'tcx> {
26372637 }
26382638}
26392639
2640+ /// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
2641+ /// the trait path, and additionally tries to "sugar" `Fn(...)` trait bounds.
2642+ #[ derive( Copy , Clone , TypeFoldable , TypeVisitable , Lift ) ]
2643+ pub struct TraitRefPrintSugared < ' tcx > ( ty:: TraitRef < ' tcx > ) ;
2644+
2645+ impl < ' tcx > rustc_errors:: IntoDiagnosticArg for TraitRefPrintSugared < ' tcx > {
2646+ fn into_diagnostic_arg ( self ) -> rustc_errors:: DiagnosticArgValue < ' static > {
2647+ self . to_string ( ) . into_diagnostic_arg ( )
2648+ }
2649+ }
2650+
2651+ impl < ' tcx > fmt:: Debug for TraitRefPrintSugared < ' tcx > {
2652+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2653+ fmt:: Display :: fmt ( self , f)
2654+ }
2655+ }
2656+
26402657/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
26412658/// the trait name. That is, it will print `Trait` instead of
26422659/// `<T as Trait<U>>`.
@@ -2654,6 +2671,10 @@ impl<'tcx> ty::TraitRef<'tcx> {
26542671 TraitRefPrintOnlyTraitPath ( self )
26552672 }
26562673
2674+ pub fn print_trait_sugared ( self ) -> TraitRefPrintSugared < ' tcx > {
2675+ TraitRefPrintSugared ( self )
2676+ }
2677+
26572678 pub fn print_only_trait_name ( self ) -> TraitRefPrintOnlyTraitName < ' tcx > {
26582679 TraitRefPrintOnlyTraitName ( self )
26592680 }
@@ -2663,6 +2684,10 @@ impl<'tcx> ty::Binder<'tcx, ty::TraitRef<'tcx>> {
26632684 pub fn print_only_trait_path ( self ) -> ty:: Binder < ' tcx , TraitRefPrintOnlyTraitPath < ' tcx > > {
26642685 self . map_bound ( |tr| tr. print_only_trait_path ( ) )
26652686 }
2687+
2688+ pub fn print_trait_sugared ( self ) -> ty:: Binder < ' tcx , TraitRefPrintSugared < ' tcx > > {
2689+ self . map_bound ( |tr| tr. print_trait_sugared ( ) )
2690+ }
26662691}
26672692
26682693#[ derive( Copy , Clone , TypeFoldable , TypeVisitable , Lift ) ]
@@ -2742,6 +2767,7 @@ forward_display_to_print! {
27422767 ty:: PolyExistentialTraitRef <' tcx>,
27432768 ty:: Binder <' tcx, ty:: TraitRef <' tcx>>,
27442769 ty:: Binder <' tcx, TraitRefPrintOnlyTraitPath <' tcx>>,
2770+ ty:: Binder <' tcx, TraitRefPrintSugared <' tcx>>,
27452771 ty:: Binder <' tcx, ty:: FnSig <' tcx>>,
27462772 ty:: Binder <' tcx, ty:: TraitPredicate <' tcx>>,
27472773 ty:: Binder <' tcx, TraitPredPrintModifiersAndPath <' tcx>>,
@@ -2841,6 +2867,24 @@ define_print_and_forward_display! {
28412867 p!( print_def_path( self . 0 . def_id, self . 0 . args) ) ;
28422868 }
28432869
2870+ TraitRefPrintSugared <' tcx> {
2871+ if !with_no_queries( )
2872+ && let Some ( kind) = cx. tcx( ) . fn_trait_kind_from_def_id( self . 0 . def_id)
2873+ && let ty:: Tuple ( args) = self . 0 . args. type_at( 1 ) . kind( )
2874+ {
2875+ p!( write( "{}" , kind. as_str( ) ) , "(" ) ;
2876+ for ( i, arg) in args. iter( ) . enumerate( ) {
2877+ if i > 0 {
2878+ p!( ", " ) ;
2879+ }
2880+ p!( print( arg) ) ;
2881+ }
2882+ p!( ")" ) ;
2883+ } else {
2884+ p!( print_def_path( self . 0 . def_id, self . 0 . args) ) ;
2885+ }
2886+ }
2887+
28442888 TraitRefPrintOnlyTraitName <' tcx> {
28452889 p!( print_def_path( self . 0 . def_id, & [ ] ) ) ;
28462890 }
@@ -2889,7 +2933,7 @@ define_print_and_forward_display! {
28892933 if let ty:: ImplPolarity :: Negative = self . polarity {
28902934 p!( "!" ) ;
28912935 }
2892- p!( print( self . trait_ref. print_only_trait_path ( ) ) )
2936+ p!( print( self . trait_ref. print_trait_sugared ( ) ) )
28932937 }
28942938
28952939 ty:: ProjectionPredicate <' tcx> {
0 commit comments