@@ -208,21 +208,30 @@ pub trait PrettyPrinter:
208208 value. skip_binder ( ) . print ( self )
209209 }
210210
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+
211229 /// Print `<...>` around what `f` prints.
212230 fn generic_delimiters < ' gcx , ' tcx > (
213231 self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
214232 f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , Self :: Error > ,
215233 ) -> Result < Self , Self :: Error > ;
216234
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-
226235 /// Return `true` if the region should be printed in
227236 /// optional positions, e.g. `&'a T` or `dyn Tr + 'b`.
228237 /// This is typically the case for all non-`'_` regions.
@@ -485,66 +494,25 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
485494 print_prefix : impl FnOnce (
486495 PrintCx < ' _ , ' gcx , ' tcx , P > ,
487496 ) -> 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 > > ,
491499 ) -> Result < P :: Path , P :: Error > {
492500 self = self . nest ( print_prefix) ?;
493501
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- } ) ;
512502 let arg0 = args. next ( ) ;
513-
514- let mut projections = projections;
515503 let projection0 = projections. next ( ) ;
516-
517504 if arg0. is_none ( ) && projection0. is_none ( ) {
518505 return self . ok ( ) ;
519506 }
507+ let args = arg0. into_iter ( ) . chain ( args) ;
508+ let projections = projection0. into_iter ( ) . chain ( projections) ;
520509
521510 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, ", " ) ?;
538514 }
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, ", " )
548516 } )
549517 }
550518}
@@ -624,8 +592,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
624592 } ) ?;
625593 if visible_path_success {
626594 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)
629597 } else {
630598 self . ok ( )
631599 } ;
@@ -742,11 +710,23 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
742710 print_prefix : impl FnOnce (
743711 PrintCx < ' _ , ' gcx , ' tcx , Self > ,
744712 ) -> Result < Self :: Path , Self :: Error > ,
745- params : & [ ty:: GenericParamDef ] ,
746- substs : & ' tcx Substs < ' tcx > ,
713+ args : impl Iterator < Item = Kind < ' tcx > > + Clone ,
747714 projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
748715 ) -> 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)
750730 }
751731}
752732
@@ -801,13 +781,6 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
801781 Ok ( inner)
802782 }
803783
804- fn always_print_region_in_paths (
805- self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
806- region : ty:: Region < ' _ > ,
807- ) -> bool {
808- * region != ty:: ReErased
809- }
810-
811784 fn region_should_not_be_omitted (
812785 self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
813786 region : ty:: Region < ' _ > ,
@@ -1488,6 +1461,11 @@ define_print_and_forward_display! {
14881461 }
14891462 }
14901463
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+
14911469 & ' tcx ty:: List <Ty <' tcx>> {
14921470 p!( write( "{{" ) ) ;
14931471 let mut tys = self . iter( ) ;
0 commit comments