@@ -207,21 +207,30 @@ pub trait PrettyPrinter:
207207 value. skip_binder ( ) . print ( self )
208208 }
209209
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+
210228 /// Print `<...>` around what `f` prints.
211229 fn generic_delimiters < ' gcx , ' tcx > (
212230 self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
213231 f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , Self :: Error > ,
214232 ) -> Result < Self , Self :: Error > ;
215233
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-
225234 /// Return `true` if the region should be printed in
226235 /// optional positions, e.g. `&'a T` or `dyn Tr + 'b`.
227236 /// This is typically the case for all non-`'_` regions.
@@ -482,66 +491,25 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
482491 print_prefix : impl FnOnce (
483492 PrintCx < ' _ , ' gcx , ' tcx , P > ,
484493 ) -> 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 > > ,
488496 ) -> Result < P :: Path , P :: Error > {
489497 self = self . nest ( print_prefix) ?;
490498
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- } ) ;
509499 let arg0 = args. next ( ) ;
510-
511- let mut projections = projections;
512500 let projection0 = projections. next ( ) ;
513-
514501 if arg0. is_none ( ) && projection0. is_none ( ) {
515502 return self . ok ( ) ;
516503 }
504+ let args = arg0. into_iter ( ) . chain ( args) ;
505+ let projections = projection0. into_iter ( ) . chain ( projections) ;
517506
518507 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, ", " ) ?;
535511 }
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, ", " )
545513 } )
546514 }
547515}
@@ -621,8 +589,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
621589 } ) ?;
622590 if visible_path_success {
623591 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)
626594 } else {
627595 self . ok ( )
628596 } ;
@@ -739,11 +707,23 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
739707 print_prefix : impl FnOnce (
740708 PrintCx < ' _ , ' gcx , ' tcx , Self > ,
741709 ) -> Result < Self :: Path , Self :: Error > ,
742- params : & [ ty:: GenericParamDef ] ,
743- substs : SubstsRef < ' tcx > ,
710+ args : impl Iterator < Item = Kind < ' tcx > > + Clone ,
744711 projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
745712 ) -> 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)
747727 }
748728}
749729
@@ -798,13 +778,6 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
798778 Ok ( inner)
799779 }
800780
801- fn always_print_region_in_paths (
802- self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
803- region : ty:: Region < ' _ > ,
804- ) -> bool {
805- * region != ty:: ReErased
806- }
807-
808781 fn region_should_not_be_omitted (
809782 self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
810783 region : ty:: Region < ' _ > ,
@@ -1498,6 +1471,11 @@ define_print_and_forward_display! {
14981471 }
14991472 }
15001473
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+
15011479 & ' tcx ty:: List <Ty <' tcx>> {
15021480 p!( write( "{{" ) ) ;
15031481 let mut tys = self . iter( ) ;
0 commit comments