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