@@ -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.
@@ -481,66 +490,25 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
481
490
print_prefix : impl FnOnce (
482
491
PrintCx < ' _ , ' gcx , ' tcx , P > ,
483
492
) -> Result < P :: Path , P :: Error > ,
484
- params : & [ ty:: GenericParamDef ] ,
485
- substs : & ' tcx Substs < ' tcx > ,
486
- projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
493
+ mut args : impl Iterator < Item = Kind < ' tcx > > ,
494
+ mut projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
487
495
) -> Result < P :: Path , P :: Error > {
488
496
self = self . nest ( print_prefix) ?;
489
497
490
- // Don't print `'_` if there's no printed region.
491
- let print_regions = params. iter ( ) . any ( |param| {
492
- match substs[ param. index as usize ] . unpack ( ) {
493
- UnpackedKind :: Lifetime ( r) => {
494
- self . always_print_region_in_paths ( r) ||
495
- self . region_should_not_be_omitted ( r)
496
- }
497
- _ => false ,
498
- }
499
- } ) ;
500
- let mut args = params. iter ( ) . map ( |param| {
501
- substs[ param. index as usize ]
502
- } ) . filter ( |arg| {
503
- match arg. unpack ( ) {
504
- UnpackedKind :: Lifetime ( _) => print_regions,
505
- _ => true ,
506
- }
507
- } ) ;
508
498
let arg0 = args. next ( ) ;
509
-
510
- let mut projections = projections;
511
499
let projection0 = projections. next ( ) ;
512
-
513
500
if arg0. is_none ( ) && projection0. is_none ( ) {
514
501
return self . ok ( ) ;
515
502
}
503
+ let args = arg0. into_iter ( ) . chain ( args) ;
504
+ let projections = projection0. into_iter ( ) . chain ( projections) ;
516
505
517
506
self . generic_delimiters ( |mut cx| {
518
- define_scoped_cx ! ( cx) ;
519
-
520
- let mut empty = true ;
521
- let mut maybe_comma = |cx : & mut Self | {
522
- if empty {
523
- empty = false ;
524
- Ok ( ( ) )
525
- } else {
526
- write ! ( cx, ", " )
527
- }
528
- } ;
529
-
530
- for arg in arg0. into_iter ( ) . chain ( args) {
531
- maybe_comma ( & mut cx) ?;
532
-
533
- p ! ( print( arg) ) ;
507
+ cx = cx. nest ( |cx| cx. comma_sep ( args, ", " ) ) ?;
508
+ if arg0. is_some ( ) && projection0. is_some ( ) {
509
+ write ! ( cx, ", " ) ?;
534
510
}
535
-
536
- for projection in projection0. into_iter ( ) . chain ( projections) {
537
- maybe_comma ( & mut cx) ?;
538
-
539
- p ! ( write( "{}=" , cx. tcx. associated_item( projection. item_def_id) . ident) ,
540
- print( projection. ty) ) ;
541
- }
542
-
543
- cx. ok ( )
511
+ cx. comma_sep ( projections, ", " )
544
512
} )
545
513
}
546
514
}
@@ -620,8 +588,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
620
588
} ) ?;
621
589
if visible_path_success {
622
590
return if let ( Some ( generics) , Some ( substs) ) = ( generics, substs) {
623
- let params = self . generic_params_to_print ( generics, substs) ;
624
- self . path_generic_args ( |cx| cx. ok ( ) , params , substs , projections)
591
+ let args = self . generic_args_to_print ( generics, substs) ;
592
+ self . path_generic_args ( |cx| cx. ok ( ) , args , projections)
625
593
} else {
626
594
self . ok ( )
627
595
} ;
@@ -738,11 +706,23 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
738
706
print_prefix : impl FnOnce (
739
707
PrintCx < ' _ , ' gcx , ' tcx , Self > ,
740
708
) -> Result < Self :: Path , Self :: Error > ,
741
- params : & [ ty:: GenericParamDef ] ,
742
- substs : & ' tcx Substs < ' tcx > ,
709
+ args : impl Iterator < Item = Kind < ' tcx > > + Clone ,
743
710
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
744
711
) -> Result < Self :: Path , Self :: Error > {
745
- self . pretty_path_generic_args ( print_prefix, params, substs, projections)
712
+ // Don't print `'_` if there's no unerased regions.
713
+ let print_regions = args. clone ( ) . any ( |arg| {
714
+ match arg. unpack ( ) {
715
+ UnpackedKind :: Lifetime ( r) => * r != ty:: ReErased ,
716
+ _ => false ,
717
+ }
718
+ } ) ;
719
+ let args = args. filter ( |arg| {
720
+ match arg. unpack ( ) {
721
+ UnpackedKind :: Lifetime ( _) => print_regions,
722
+ _ => true ,
723
+ }
724
+ } ) ;
725
+ self . pretty_path_generic_args ( print_prefix, args, projections)
746
726
}
747
727
}
748
728
@@ -797,13 +777,6 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
797
777
Ok ( inner)
798
778
}
799
779
800
- fn always_print_region_in_paths (
801
- self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
802
- region : ty:: Region < ' _ > ,
803
- ) -> bool {
804
- * region != ty:: ReErased
805
- }
806
-
807
780
fn region_should_not_be_omitted (
808
781
self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
809
782
region : ty:: Region < ' _ > ,
@@ -1484,6 +1457,11 @@ define_print_and_forward_display! {
1484
1457
}
1485
1458
}
1486
1459
1460
+ ty:: ExistentialProjection <' tcx> {
1461
+ let name = cx. tcx. associated_item( self . item_def_id) . ident;
1462
+ p!( write( "{}=" , name) , print( self . ty) )
1463
+ }
1464
+
1487
1465
& ' tcx ty:: List <Ty <' tcx>> {
1488
1466
p!( write( "{{" ) ) ;
1489
1467
let mut tys = self . iter( ) ;
0 commit comments