@@ -223,14 +223,10 @@ pub trait PrettyPrinter:
223223 false
224224 }
225225
226- // HACK(eddyb) Trying to print a lifetime might not print anything, which
227- // may need special handling in the caller (of `ty::RegionKind::print`).
228- // To avoid printing to a temporary string (which isn't even supported),
229- // the `print_region_outputs_anything` method can instead be used to
230- // determine this, ahead of time.
231- //
232- // NB: this must be kept in sync with the implementation of `print_region`.
233- fn print_region_outputs_anything (
226+ /// Return `true` if the region should be printed in
227+ /// optional positions, e.g. `&'a T` or `dyn Tr + 'b`.
228+ /// This is typically the case for all non-`'_` regions.
229+ fn region_should_not_be_omitted (
234230 self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
235231 region : ty:: Region < ' _ > ,
236232 ) -> bool ;
@@ -500,7 +496,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
500496 match substs[ param. index as usize ] . unpack ( ) {
501497 UnpackedKind :: Lifetime ( r) => {
502498 self . always_print_region_in_paths ( r) ||
503- self . print_region_outputs_anything ( r)
499+ self . region_should_not_be_omitted ( r)
504500 }
505501 _ => false ,
506502 }
@@ -538,19 +534,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
538534 for arg in arg0. into_iter ( ) . chain ( args) {
539535 maybe_comma ( & mut cx) ?;
540536
541- if let UnpackedKind :: Lifetime ( region) = arg. unpack ( ) {
542- if !cx. print_region_outputs_anything ( region) {
543- // This happens when the value of the region
544- // parameter is not easily serialized. This may be
545- // because the user omitted it in the first place,
546- // or because it refers to some block in the code,
547- // etc. I'm not sure how best to serialize this.
548- p ! ( write( "'_" ) ) ;
549-
550- continue ;
551- }
552- }
553-
554537 p ! ( print( arg) ) ;
555538 }
556539
@@ -759,7 +742,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
759742 print_prefix : impl FnOnce (
760743 PrintCx < ' _ , ' gcx , ' tcx , Self > ,
761744 ) -> Result < Self :: Path , Self :: Error > ,
762- mut params : & [ ty:: GenericParamDef ] ,
745+ params : & [ ty:: GenericParamDef ] ,
763746 substs : & ' tcx Substs < ' tcx > ,
764747 projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
765748 ) -> Result < Self :: Path , Self :: Error > {
@@ -825,7 +808,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
825808 * region != ty:: ReErased
826809 }
827810
828- fn print_region_outputs_anything (
811+ fn region_should_not_be_omitted (
829812 self : & PrintCx < ' _ , ' _ , ' _ , Self > ,
830813 region : ty:: Region < ' _ > ,
831814 ) -> bool {
@@ -905,8 +888,9 @@ impl<F: fmt::Write> FmtPrinter<F> {
905888 // `explain_region()` or `note_and_explain_region()`.
906889 match * region {
907890 ty:: ReEarlyBound ( ref data) => {
908- if data. name != "'_ " {
891+ if data. name != "" {
909892 p ! ( write( "{}" , data. name) ) ;
893+ return self . ok ( ) ;
910894 }
911895 }
912896 ty:: ReLateBound ( _, br) |
@@ -922,6 +906,7 @@ impl<F: fmt::Write> FmtPrinter<F> {
922906 if let Some ( ( region, counter) ) = highlight. highlight_bound_region {
923907 if br == region {
924908 p ! ( write( "'{}" , counter) ) ;
909+ return self . ok ( ) ;
925910 }
926911 }
927912 }
@@ -941,20 +926,33 @@ impl<F: fmt::Write> FmtPrinter<F> {
941926 first_statement_index. index( )
942927 ) ) ,
943928 }
929+ return self . ok ( ) ;
944930 }
945931 ty:: ReVar ( region_vid) if identify_regions => {
946932 p ! ( write( "{:?}" , region_vid) ) ;
933+ return self . ok ( ) ;
947934 }
948935 ty:: ReVar ( _) => { }
949936 ty:: ReScope ( _) |
950937 ty:: ReErased => { }
951- ty:: ReStatic => p ! ( write( "'static" ) ) ,
952- ty:: ReEmpty => p ! ( write( "'<empty>" ) ) ,
938+ ty:: ReStatic => {
939+ p ! ( write( "'static" ) ) ;
940+ return self . ok ( ) ;
941+ }
942+ ty:: ReEmpty => {
943+ p ! ( write( "'<empty>" ) ) ;
944+ return self . ok ( ) ;
945+ }
953946
954947 // The user should never encounter these in unsubstituted form.
955- ty:: ReClosureBound ( vid) => p ! ( write( "{:?}" , vid) ) ,
948+ ty:: ReClosureBound ( vid) => {
949+ p ! ( write( "{:?}" , vid) ) ;
950+ return self . ok ( ) ;
951+ }
956952 }
957953
954+ p ! ( write( "'_" ) ) ;
955+
958956 self . ok ( )
959957 }
960958}
@@ -981,7 +979,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
981979 }
982980 ty:: Ref ( r, ty, mutbl) => {
983981 p ! ( write( "&" ) ) ;
984- if self . print_region_outputs_anything ( r) {
982+ if self . region_should_not_be_omitted ( r) {
985983 p ! ( print( r) , write( " " ) ) ;
986984 }
987985 p ! ( print( ty:: TypeAndMut { ty, mutbl } ) )
@@ -1030,7 +1028,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
10301028 nest ! ( |cx| cx. print_def_path( def. did, Some ( substs) , iter:: empty( ) ) ) ;
10311029 }
10321030 ty:: Dynamic ( data, r) => {
1033- let print_r = self . print_region_outputs_anything ( r) ;
1031+ let print_r = self . region_should_not_be_omitted ( r) ;
10341032 if print_r {
10351033 p ! ( write( "(" ) ) ;
10361034 }
0 commit comments