@@ -338,7 +338,7 @@ pub trait PrettyPrinter:
338
338
/// nested components in some larger context.
339
339
fn nest < ' a , ' gcx , ' tcx , E > (
340
340
self : PrintCx < ' a , ' gcx , ' tcx , Self > ,
341
- f : impl for < ' b > FnOnce ( PrintCx < ' b , ' gcx , ' tcx , Self > ) -> Result < Self , E > ,
341
+ f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , E > ,
342
342
) -> Result < PrintCx < ' a , ' gcx , ' tcx , Self > , E > {
343
343
let printer = f ( PrintCx {
344
344
tcx : self . tcx ,
@@ -352,6 +352,17 @@ pub trait PrettyPrinter:
352
352
} )
353
353
}
354
354
355
+ /// Print `<...>` around what `f` prints.
356
+ fn generic_delimiters < ' gcx , ' tcx > (
357
+ mut self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
358
+ f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , Self :: Error > ,
359
+ ) -> Result < Self , Self :: Error > {
360
+ write ! ( self . printer, "<" ) ?;
361
+ let mut printer = f ( self ) ?;
362
+ write ! ( printer, ">" ) ?;
363
+ Ok ( printer)
364
+ }
365
+
355
366
/// Return `true` if the region should be printed in path generic args
356
367
/// even when it's `'_`, such as in e.g. `Foo<'_, '_, '_>`.
357
368
fn always_print_region_in_paths (
@@ -750,7 +761,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
750
761
}
751
762
752
763
pub fn pretty_path_qualified (
753
- mut self ,
764
+ self ,
754
765
self_ty : Ty < ' tcx > ,
755
766
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
756
767
ns : Namespace ,
@@ -776,20 +787,19 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
776
787
}
777
788
}
778
789
779
- write ! ( self . printer, "<" ) ?;
780
- nest ! ( self , |cx| self_ty. print_display( cx) ) ;
781
- if let Some ( trait_ref) = trait_ref {
782
- write ! ( self . printer, " as " ) ?;
783
- nest ! ( self , |cx| cx. print_def_path(
784
- trait_ref. def_id,
785
- Some ( trait_ref. substs) ,
786
- Namespace :: TypeNS ,
787
- iter:: empty( ) ,
788
- ) ) ;
789
- }
790
- write ! ( self . printer, ">" ) ?;
791
-
792
- Ok ( self . printer )
790
+ self . generic_delimiters ( |mut cx| {
791
+ nest ! ( cx, |cx| self_ty. print_display( cx) ) ;
792
+ if let Some ( trait_ref) = trait_ref {
793
+ write ! ( cx. printer, " as " ) ?;
794
+ nest ! ( cx, |cx| cx. print_def_path(
795
+ trait_ref. def_id,
796
+ Some ( trait_ref. substs) ,
797
+ Namespace :: TypeNS ,
798
+ iter:: empty( ) ,
799
+ ) ) ;
800
+ }
801
+ Ok ( cx. printer )
802
+ } )
793
803
}
794
804
795
805
pub fn pretty_path_append_impl (
@@ -800,17 +810,18 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
800
810
self_ty : Ty < ' tcx > ,
801
811
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
802
812
) -> Result < P :: Path , P :: Error > {
803
- // HACK(eddyb) going through `path_append` means symbol name
804
- // computation gets to handle its equivalent of `::` correctly.
805
- nest ! ( self , |cx| cx. path_append( print_prefix, "<impl " ) ) ;
806
- if let Some ( trait_ref) = trait_ref {
807
- nest ! ( self , |cx| trait_ref. print_display( cx) ) ;
808
- write ! ( self . printer, " for " ) ?;
809
- }
810
- nest ! ( self , |cx| self_ty. print_display( cx) ) ;
811
- write ! ( self . printer, ">" ) ?;
813
+ nest ! ( self , print_prefix) ;
812
814
813
- Ok ( self . printer )
815
+ self . generic_delimiters ( |mut cx| {
816
+ write ! ( cx. printer, "impl " ) ?;
817
+ if let Some ( trait_ref) = trait_ref {
818
+ nest ! ( cx, |cx| trait_ref. print_display( cx) ) ;
819
+ write ! ( cx. printer, " for " ) ?;
820
+ }
821
+ nest ! ( cx, |cx| self_ty. print_display( cx) ) ;
822
+
823
+ Ok ( cx. printer )
824
+ } )
814
825
}
815
826
816
827
pub fn pretty_path_generic_args (
@@ -825,18 +836,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
825
836
) -> Result < P :: Path , P :: Error > {
826
837
nest ! ( self , |cx| print_prefix( cx) ) ;
827
838
828
- let mut empty = true ;
829
- let mut start_or_continue = |cx : & mut Self , start : & str , cont : & str | {
830
- write ! ( cx. printer, "{}" , if empty {
831
- empty = false ;
832
- start
833
- } else {
834
- cont
835
- } )
836
- } ;
837
-
838
- let start = if ns == Namespace :: ValueNS { "::<" } else { "<" } ;
839
-
840
839
// Don't print `'_` if there's no printed region.
841
840
let print_regions = params. iter ( ) . any ( |param| {
842
841
match substs[ param. index as usize ] . unpack ( ) {
@@ -864,41 +863,72 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
864
863
} ) . count ( )
865
864
} ;
866
865
867
- for param in & params[ ..params. len ( ) - num_supplied_defaults] {
868
- match substs[ param. index as usize ] . unpack ( ) {
869
- UnpackedKind :: Lifetime ( region) => {
870
- if !print_regions {
871
- continue ;
866
+ let params = & params[ ..params. len ( ) - num_supplied_defaults] ;
867
+ let mut args = params. iter ( ) . map ( |param| {
868
+ substs[ param. index as usize ] . unpack ( )
869
+ } ) . filter ( |arg| {
870
+ match arg {
871
+ UnpackedKind :: Lifetime ( _) => print_regions,
872
+ _ => true ,
873
+ }
874
+ } ) ;
875
+ let arg0 = args. next ( ) ;
876
+
877
+ let mut projections = projections;
878
+ let projection0 = projections. next ( ) ;
879
+
880
+ if arg0. is_none ( ) && projection0. is_none ( ) {
881
+ return Ok ( self . printer ) ;
882
+ }
883
+
884
+ // FIXME(eddyb) move this into `generic_delimiters`.
885
+ if ns == Namespace :: ValueNS {
886
+ write ! ( self . printer, "::" ) ?;
887
+ }
888
+
889
+ self . generic_delimiters ( |mut cx| {
890
+ let mut empty = true ;
891
+ let mut maybe_comma = |cx : & mut Self | {
892
+ if empty {
893
+ empty = false ;
894
+ Ok ( ( ) )
895
+ } else {
896
+ write ! ( cx. printer, ", " )
897
+ }
898
+ } ;
899
+
900
+ for arg in arg0. into_iter ( ) . chain ( args) {
901
+ maybe_comma ( & mut cx) ?;
902
+
903
+ match arg {
904
+ UnpackedKind :: Lifetime ( region) => {
905
+ if !cx. print_region_outputs_anything ( region) {
906
+ // This happens when the value of the region
907
+ // parameter is not easily serialized. This may be
908
+ // because the user omitted it in the first place,
909
+ // or because it refers to some block in the code,
910
+ // etc. I'm not sure how best to serialize this.
911
+ write ! ( cx. printer, "'_" ) ?;
912
+ } else {
913
+ nest ! ( cx, |cx| region. print_display( cx) ) ;
914
+ }
872
915
}
873
- start_or_continue ( & mut self , start, ", " ) ?;
874
- if !self . print_region_outputs_anything ( region) {
875
- // This happens when the value of the region
876
- // parameter is not easily serialized. This may be
877
- // because the user omitted it in the first place,
878
- // or because it refers to some block in the code,
879
- // etc. I'm not sure how best to serialize this.
880
- write ! ( self . printer, "'_" ) ?;
881
- } else {
882
- nest ! ( self , |cx| region. print_display( cx) ) ;
916
+ UnpackedKind :: Type ( ty) => {
917
+ nest ! ( cx, |cx| ty. print_display( cx) ) ;
883
918
}
884
919
}
885
- UnpackedKind :: Type ( ty) => {
886
- start_or_continue ( & mut self , start, ", " ) ?;
887
- nest ! ( self , |cx| ty. print_display( cx) ) ;
888
- }
889
920
}
890
- }
891
921
892
- for projection in projections {
893
- start_or_continue ( & mut self , start, ", " ) ?;
894
- write ! ( self . printer, "{}=" ,
895
- self . tcx. associated_item( projection. item_def_id) . ident) ?;
896
- nest ! ( self , |cx| projection. ty. print_display( cx) ) ;
897
- }
922
+ for projection in projection0. into_iter ( ) . chain ( projections) {
923
+ maybe_comma ( & mut cx) ?;
898
924
899
- start_or_continue ( & mut self , "" , ">" ) ?;
925
+ write ! ( cx. printer, "{}=" ,
926
+ cx. tcx. associated_item( projection. item_def_id) . ident) ?;
927
+ nest ! ( cx, |cx| projection. ty. print_display( cx) ) ;
928
+ }
900
929
901
- Ok ( self . printer )
930
+ Ok ( cx. printer )
931
+ } )
902
932
}
903
933
}
904
934
@@ -1086,7 +1116,15 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
1086
1116
self_ty : Ty < ' tcx > ,
1087
1117
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
1088
1118
) -> Result < Self :: Path , Self :: Error > {
1089
- self . pretty_path_append_impl ( print_prefix, self_ty, trait_ref)
1119
+ self . pretty_path_append_impl ( |cx| {
1120
+ let mut printer = print_prefix ( cx) ?;
1121
+
1122
+ if !printer. empty {
1123
+ write ! ( printer, "::" ) ?;
1124
+ }
1125
+
1126
+ Ok ( printer)
1127
+ } , self_ty, trait_ref)
1090
1128
}
1091
1129
fn path_append < ' gcx , ' tcx > (
1092
1130
self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
@@ -1125,7 +1163,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
1125
1163
impl < F : fmt:: Write > PrettyPrinter for FmtPrinter < F > {
1126
1164
fn nest < ' a , ' gcx , ' tcx , E > (
1127
1165
mut self : PrintCx < ' a , ' gcx , ' tcx , Self > ,
1128
- f : impl for < ' b > FnOnce ( PrintCx < ' b , ' gcx , ' tcx , Self > ) -> Result < Self , E > ,
1166
+ f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , E > ,
1129
1167
) -> Result < PrintCx < ' a , ' gcx , ' tcx , Self > , E > {
1130
1168
let was_empty = std:: mem:: replace ( & mut self . printer . empty , true ) ;
1131
1169
let mut printer = f ( PrintCx {
0 commit comments