@@ -2640,6 +2640,23 @@ impl<'tcx> fmt::Debug for TraitRefPrintOnlyTraitPath<'tcx> {
2640
2640
}
2641
2641
}
2642
2642
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
+
2643
2660
/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
2644
2661
/// the trait name. That is, it will print `Trait` instead of
2645
2662
/// `<T as Trait<U>>`.
@@ -2657,6 +2674,10 @@ impl<'tcx> ty::TraitRef<'tcx> {
2657
2674
TraitRefPrintOnlyTraitPath ( self )
2658
2675
}
2659
2676
2677
+ pub fn print_trait_sugared ( self ) -> TraitRefPrintSugared < ' tcx > {
2678
+ TraitRefPrintSugared ( self )
2679
+ }
2680
+
2660
2681
pub fn print_only_trait_name ( self ) -> TraitRefPrintOnlyTraitName < ' tcx > {
2661
2682
TraitRefPrintOnlyTraitName ( self )
2662
2683
}
@@ -2666,6 +2687,10 @@ impl<'tcx> ty::Binder<'tcx, ty::TraitRef<'tcx>> {
2666
2687
pub fn print_only_trait_path ( self ) -> ty:: Binder < ' tcx , TraitRefPrintOnlyTraitPath < ' tcx > > {
2667
2688
self . map_bound ( |tr| tr. print_only_trait_path ( ) )
2668
2689
}
2690
+
2691
+ pub fn print_trait_sugared ( self ) -> ty:: Binder < ' tcx , TraitRefPrintSugared < ' tcx > > {
2692
+ self . map_bound ( |tr| tr. print_trait_sugared ( ) )
2693
+ }
2669
2694
}
2670
2695
2671
2696
#[ derive( Copy , Clone , TypeFoldable , TypeVisitable , Lift ) ]
@@ -2745,6 +2770,7 @@ forward_display_to_print! {
2745
2770
ty:: PolyExistentialTraitRef <' tcx>,
2746
2771
ty:: Binder <' tcx, ty:: TraitRef <' tcx>>,
2747
2772
ty:: Binder <' tcx, TraitRefPrintOnlyTraitPath <' tcx>>,
2773
+ ty:: Binder <' tcx, TraitRefPrintSugared <' tcx>>,
2748
2774
ty:: Binder <' tcx, ty:: FnSig <' tcx>>,
2749
2775
ty:: Binder <' tcx, ty:: TraitPredicate <' tcx>>,
2750
2776
ty:: Binder <' tcx, TraitPredPrintModifiersAndPath <' tcx>>,
@@ -2844,6 +2870,24 @@ define_print_and_forward_display! {
2844
2870
p!( print_def_path( self . 0 . def_id, self . 0 . args) ) ;
2845
2871
}
2846
2872
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
+
2847
2891
TraitRefPrintOnlyTraitName <' tcx> {
2848
2892
p!( print_def_path( self . 0 . def_id, & [ ] ) ) ;
2849
2893
}
@@ -2892,7 +2936,7 @@ define_print_and_forward_display! {
2892
2936
if let ty:: ImplPolarity :: Negative = self . polarity {
2893
2937
p!( "!" ) ;
2894
2938
}
2895
- p!( print( self . trait_ref. print_only_trait_path ( ) ) )
2939
+ p!( print( self . trait_ref. print_trait_sugared ( ) ) )
2896
2940
}
2897
2941
2898
2942
ty:: ProjectionPredicate <' tcx> {
0 commit comments