@@ -395,20 +395,15 @@ impl PrintCx<'a, 'gcx, 'tcx> {
395
395
continue ;
396
396
}
397
397
start_or_continue ( f, start, ", " ) ?;
398
- if self . is_verbose {
399
- write ! ( f, "{:?}" , region) ?;
398
+ if !region. display_outputs_anything ( self ) {
399
+ // This happens when the value of the region
400
+ // parameter is not easily serialized. This may be
401
+ // because the user omitted it in the first place,
402
+ // or because it refers to some block in the code,
403
+ // etc. I'm not sure how best to serialize this.
404
+ write ! ( f, "'_" ) ?;
400
405
} else {
401
- let s = region. print_display_to_string ( self ) ;
402
- if s. is_empty ( ) {
403
- // This happens when the value of the region
404
- // parameter is not easily serialized. This may be
405
- // because the user omitted it in the first place,
406
- // or because it refers to some block in the code,
407
- // etc. I'm not sure how best to serialize this.
408
- write ! ( f, "'_" ) ?;
409
- } else {
410
- write ! ( f, "{}" , s) ?;
411
- }
406
+ region. print_display ( f, self ) ?;
412
407
}
413
408
}
414
409
UnpackedKind :: Type ( ty) => {
@@ -721,6 +716,32 @@ define_print! {
721
716
}
722
717
}
723
718
719
+ // HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`)
720
+ //
721
+ // NB: this must be kept in sync with the printing logic above.
722
+ impl ty:: BoundRegion {
723
+ fn display_outputs_anything ( & self , cx : & mut PrintCx < ' _ , ' _ , ' _ > ) -> bool {
724
+ if cx. is_verbose {
725
+ return true ;
726
+ }
727
+
728
+ if let BrNamed ( _, name) = * self {
729
+ if name != "" && name != "'_" {
730
+ return true ;
731
+ }
732
+ }
733
+
734
+ let highlight = RegionHighlightMode :: get ( ) ;
735
+ if let Some ( ( region, _) ) = highlight. highlight_bound_region {
736
+ if * self == region {
737
+ return true ;
738
+ }
739
+ }
740
+
741
+ false
742
+ }
743
+ }
744
+
724
745
define_print ! {
725
746
( ) ty:: PlaceholderRegion , ( self , f, cx) {
726
747
display {
@@ -738,6 +759,24 @@ define_print! {
738
759
}
739
760
}
740
761
762
+ // HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`)
763
+ //
764
+ // NB: this must be kept in sync with the printing logic above.
765
+ impl ty:: PlaceholderRegion {
766
+ fn display_outputs_anything ( & self , cx : & mut PrintCx < ' _ , ' _ , ' _ > ) -> bool {
767
+ if cx. is_verbose {
768
+ return true ;
769
+ }
770
+
771
+ let highlight = RegionHighlightMode :: get ( ) ;
772
+ if highlight. placeholder_highlight ( * self ) . is_some ( ) {
773
+ return true ;
774
+ }
775
+
776
+ self . name . display_outputs_anything ( cx)
777
+ }
778
+ }
779
+
741
780
define_print ! {
742
781
( ) ty:: RegionKind , ( self , f, cx) {
743
782
display {
@@ -845,6 +884,49 @@ define_print! {
845
884
}
846
885
}
847
886
887
+ // HACK(eddyb) Trying to print a lifetime might not print anything, which
888
+ // may need special handling in the caller (of `ty::RegionKind::print`).
889
+ // To avoid printing to a temporary string, the `display_outputs_anything`
890
+ // method can instead be used to determine this, ahead of time.
891
+ //
892
+ // NB: this must be kept in sync with the printing logic above.
893
+ impl ty:: RegionKind {
894
+ fn display_outputs_anything ( & self , cx : & mut PrintCx < ' _ , ' _ , ' _ > ) -> bool {
895
+ if cx. is_verbose {
896
+ return true ;
897
+ }
898
+
899
+ if RegionHighlightMode :: get ( ) . region_highlighted ( self ) . is_some ( ) {
900
+ return true ;
901
+ }
902
+
903
+ match * self {
904
+ ty:: ReEarlyBound ( ref data) => {
905
+ data. name != "" && data. name != "'_"
906
+ }
907
+
908
+ ty:: ReLateBound ( _, br) |
909
+ ty:: ReFree ( ty:: FreeRegion { bound_region : br, .. } ) => {
910
+ br. display_outputs_anything ( cx)
911
+ }
912
+
913
+ ty:: RePlaceholder ( p) => p. display_outputs_anything ( cx) ,
914
+
915
+ ty:: ReScope ( _) |
916
+ ty:: ReVar ( _) if cx. identify_regions => true ,
917
+
918
+ ty:: ReVar ( region_vid) => region_vid. display_outputs_anything ( cx) ,
919
+
920
+ ty:: ReScope ( _) |
921
+ ty:: ReErased => false ,
922
+
923
+ ty:: ReStatic |
924
+ ty:: ReEmpty |
925
+ ty:: ReClosureBound ( _) => true ,
926
+ }
927
+ }
928
+ }
929
+
848
930
define_print ! {
849
931
( ) ty:: FreeRegion , ( self , f, cx) {
850
932
debug {
@@ -931,6 +1013,24 @@ define_print! {
931
1013
}
932
1014
}
933
1015
1016
+ // HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`)
1017
+ //
1018
+ // NB: this must be kept in sync with the printing logic above.
1019
+ impl ty:: RegionVid {
1020
+ fn display_outputs_anything ( & self , cx : & mut PrintCx < ' _ , ' _ , ' _ > ) -> bool {
1021
+ if cx. is_verbose {
1022
+ return true ;
1023
+ }
1024
+
1025
+ let highlight = RegionHighlightMode :: get ( ) ;
1026
+ if highlight. region_highlighted ( & ty:: ReVar ( * self ) ) . is_some ( ) {
1027
+ return true ;
1028
+ }
1029
+
1030
+ false
1031
+ }
1032
+ }
1033
+
934
1034
define_print ! {
935
1035
( ) ty:: InferTy , ( self , f, cx) {
936
1036
display {
@@ -1041,9 +1141,8 @@ define_print! {
1041
1141
}
1042
1142
Ref ( r, ty, mutbl) => {
1043
1143
write!( f, "&" ) ?;
1044
- let s = r. print_display_to_string( cx) ;
1045
- if !s. is_empty( ) {
1046
- write!( f, "{} " , s) ?;
1144
+ if r. display_outputs_anything( cx) {
1145
+ print!( f, cx, print_display( r) , write( " " ) ) ?;
1047
1146
}
1048
1147
ty:: TypeAndMut { ty, mutbl } . print( f, cx)
1049
1148
}
@@ -1089,17 +1188,16 @@ define_print! {
1089
1188
}
1090
1189
Adt ( def, substs) => cx. parameterized( f, def. did, substs, iter:: empty( ) ) ,
1091
1190
Dynamic ( data, r) => {
1092
- let r = r. print_display_to_string ( cx) ;
1093
- if !r . is_empty ( ) {
1191
+ let print_r = r. display_outputs_anything ( cx) ;
1192
+ if print_r {
1094
1193
write!( f, "(" ) ?;
1095
1194
}
1096
1195
write!( f, "dyn " ) ?;
1097
1196
data. print( f, cx) ?;
1098
- if !r. is_empty( ) {
1099
- write!( f, " + {})" , r)
1100
- } else {
1101
- Ok ( ( ) )
1197
+ if print_r {
1198
+ print!( f, cx, write( " + " ) , print_display( r) , write( ")" ) ) ?;
1102
1199
}
1200
+ Ok ( ( ) )
1103
1201
}
1104
1202
Foreign ( def_id) => cx. parameterized( f, def_id, Substs :: empty( ) , iter:: empty( ) ) ,
1105
1203
Projection ( ref data) => data. print( f, cx) ,
0 commit comments