@@ -207,21 +207,30 @@ pub trait PrettyPrinter:
207
207
value. skip_binder ( ) . print ( self )
208
208
}
209
209
210
+ /// Print comma-separated elements.
211
+ fn comma_sep < T > (
212
+ mut self : PrintCx < ' _ , ' _ , ' tcx , Self > ,
213
+ mut elems : impl Iterator < Item = T > ,
214
+ comma : & str ,
215
+ ) -> Result < Self , Self :: Error >
216
+ where T : Print < ' tcx , Self , Output = Self , Error = Self :: Error >
217
+ {
218
+ if let Some ( first) = elems. next ( ) {
219
+ self = self . nest ( |cx| first. print ( cx) ) ?;
220
+ for elem in elems {
221
+ self . write_str ( comma) ?;
222
+ self = self . nest ( |cx| elem. print ( cx) ) ?;
223
+ }
224
+ }
225
+ self . ok ( )
226
+ }
227
+
210
228
/// Print `<...>` around what `f` prints.
211
229
fn generic_delimiters < ' gcx , ' tcx > (
212
230
self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
213
231
f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , Self :: Error > ,
214
232
) -> Result < Self , Self :: Error > ;
215
233
216
- /// Return `true` if the region should be printed in path generic args
217
- /// even when it's `'_`, such as in e.g. `Foo<'_, '_, '_>`.
218
- fn always_print_region_in_paths (
219
- self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
220
- _region : ty:: Region < ' _ > ,
221
- ) -> bool {
222
- false
223
- }
224
-
225
234
/// Return `true` if the region should be printed in
226
235
/// optional positions, e.g. `&'a T` or `dyn Tr + 'b`.
227
236
/// This is typically the case for all non-`'_` regions.
@@ -482,66 +491,25 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
482
491
print_prefix : impl FnOnce (
483
492
PrintCx < ' _ , ' gcx , ' tcx , P > ,
484
493
) -> Result < P :: Path , P :: Error > ,
485
- params : & [ ty:: GenericParamDef ] ,
486
- substs : SubstsRef < ' tcx > ,
487
- projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
494
+ mut args : impl Iterator < Item = Kind < ' tcx > > ,
495
+ mut projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
488
496
) -> Result < P :: Path , P :: Error > {
489
497
self = self . nest ( print_prefix) ?;
490
498
491
- // Don't print `'_` if there's no printed region.
492
- let print_regions = params. iter ( ) . any ( |param| {
493
- match substs[ param. index as usize ] . unpack ( ) {
494
- UnpackedKind :: Lifetime ( r) => {
495
- self . always_print_region_in_paths ( r) ||
496
- self . region_should_not_be_omitted ( r)
497
- }
498
- _ => false ,
499
- }
500
- } ) ;
501
- let mut args = params. iter ( ) . map ( |param| {
502
- substs[ param. index as usize ]
503
- } ) . filter ( |arg| {
504
- match arg. unpack ( ) {
505
- UnpackedKind :: Lifetime ( _) => print_regions,
506
- _ => true ,
507
- }
508
- } ) ;
509
499
let arg0 = args. next ( ) ;
510
-
511
- let mut projections = projections;
512
500
let projection0 = projections. next ( ) ;
513
-
514
501
if arg0. is_none ( ) && projection0. is_none ( ) {
515
502
return self . ok ( ) ;
516
503
}
504
+ let args = arg0. into_iter ( ) . chain ( args) ;
505
+ let projections = projection0. into_iter ( ) . chain ( projections) ;
517
506
518
507
self . generic_delimiters ( |mut cx| {
519
- define_scoped_cx ! ( cx) ;
520
-
521
- let mut empty = true ;
522
- let mut maybe_comma = |cx : & mut Self | {
523
- if empty {
524
- empty = false ;
525
- Ok ( ( ) )
526
- } else {
527
- write ! ( cx, ", " )
528
- }
529
- } ;
530
-
531
- for arg in arg0. into_iter ( ) . chain ( args) {
532
- maybe_comma ( & mut cx) ?;
533
-
534
- p ! ( print( arg) ) ;
508
+ cx = cx. nest ( |cx| cx. comma_sep ( args, ", " ) ) ?;
509
+ if arg0. is_some ( ) && projection0. is_some ( ) {
510
+ write ! ( cx, ", " ) ?;
535
511
}
536
-
537
- for projection in projection0. into_iter ( ) . chain ( projections) {
538
- maybe_comma ( & mut cx) ?;
539
-
540
- p ! ( write( "{}=" , cx. tcx. associated_item( projection. item_def_id) . ident) ,
541
- print( projection. ty) ) ;
542
- }
543
-
544
- cx. ok ( )
512
+ cx. comma_sep ( projections, ", " )
545
513
} )
546
514
}
547
515
}
@@ -621,8 +589,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
621
589
} ) ?;
622
590
if visible_path_success {
623
591
return if let ( Some ( generics) , Some ( substs) ) = ( generics, substs) {
624
- let params = self . generic_params_to_print ( generics, substs) ;
625
- self . path_generic_args ( |cx| cx. ok ( ) , params , substs , projections)
592
+ let args = self . generic_args_to_print ( generics, substs) ;
593
+ self . path_generic_args ( |cx| cx. ok ( ) , args , projections)
626
594
} else {
627
595
self . ok ( )
628
596
} ;
@@ -739,11 +707,23 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
739
707
print_prefix : impl FnOnce (
740
708
PrintCx < ' _ , ' gcx , ' tcx , Self > ,
741
709
) -> Result < Self :: Path , Self :: Error > ,
742
- params : & [ ty:: GenericParamDef ] ,
743
- substs : SubstsRef < ' tcx > ,
710
+ args : impl Iterator < Item = Kind < ' tcx > > + Clone ,
744
711
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
745
712
) -> Result < Self :: Path , Self :: Error > {
746
- self . pretty_path_generic_args ( print_prefix, params, substs, projections)
713
+ // Don't print `'_` if there's no unerased regions.
714
+ let print_regions = args. clone ( ) . any ( |arg| {
715
+ match arg. unpack ( ) {
716
+ UnpackedKind :: Lifetime ( r) => * r != ty:: ReErased ,
717
+ _ => false ,
718
+ }
719
+ } ) ;
720
+ let args = args. filter ( |arg| {
721
+ match arg. unpack ( ) {
722
+ UnpackedKind :: Lifetime ( _) => print_regions,
723
+ _ => true ,
724
+ }
725
+ } ) ;
726
+ self . pretty_path_generic_args ( print_prefix, args, projections)
747
727
}
748
728
}
749
729
@@ -798,13 +778,6 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
798
778
Ok ( inner)
799
779
}
800
780
801
- fn always_print_region_in_paths (
802
- self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
803
- region : ty:: Region < ' _ > ,
804
- ) -> bool {
805
- * region != ty:: ReErased
806
- }
807
-
808
781
fn region_should_not_be_omitted (
809
782
self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
810
783
region : ty:: Region < ' _ > ,
@@ -1498,6 +1471,11 @@ define_print_and_forward_display! {
1498
1471
}
1499
1472
}
1500
1473
1474
+ ty:: ExistentialProjection <' tcx> {
1475
+ let name = cx. tcx. associated_item( self . item_def_id) . ident;
1476
+ p!( write( "{}=" , name) , print( self . ty) )
1477
+ }
1478
+
1501
1479
& ' tcx ty:: List <Ty <' tcx>> {
1502
1480
p!( write( "{{" ) ) ;
1503
1481
let mut tys = self . iter( ) ;
0 commit comments