Skip to content

Commit a5dcd6d

Browse files
committed
rustc: add a ty::RegionKind::display_outputs_anything method to avoid printing to a string.
1 parent 0e8adf9 commit a5dcd6d

File tree

2 files changed

+120
-37
lines changed

2 files changed

+120
-37
lines changed

src/librustc/ty/print.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ impl PrintCx<'a, 'gcx, 'tcx> {
5959

6060
pub trait Print<'tcx> {
6161
fn print<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, 'tcx>) -> fmt::Result;
62-
fn print_to_string(&self, cx: &mut PrintCx<'_, '_, 'tcx>) -> String {
63-
let mut result = String::new();
64-
let _ = self.print(&mut result, cx);
65-
result
66-
}
6762
fn print_display<F: fmt::Write>(
6863
&self,
6964
f: &mut F,
@@ -75,21 +70,11 @@ pub trait Print<'tcx> {
7570
cx.is_debug = old_debug;
7671
result
7772
}
78-
fn print_display_to_string(&self, cx: &mut PrintCx<'_, '_, 'tcx>) -> String {
79-
let mut result = String::new();
80-
let _ = self.print_display(&mut result, cx);
81-
result
82-
}
8373
fn print_debug<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, 'tcx>) -> fmt::Result {
8474
let old_debug = cx.is_debug;
8575
cx.is_debug = true;
8676
let result = self.print(f, cx);
8777
cx.is_debug = old_debug;
8878
result
8979
}
90-
fn print_debug_to_string(&self, cx: &mut PrintCx<'_, '_, 'tcx>) -> String {
91-
let mut result = String::new();
92-
let _ = self.print_debug(&mut result, cx);
93-
result
94-
}
9580
}

src/librustc/util/ppaux.rs

Lines changed: 120 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -395,20 +395,15 @@ impl PrintCx<'a, 'gcx, 'tcx> {
395395
continue;
396396
}
397397
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, "'_")?;
400405
} 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)?;
412407
}
413408
}
414409
UnpackedKind::Type(ty) => {
@@ -721,6 +716,32 @@ define_print! {
721716
}
722717
}
723718

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+
724745
define_print! {
725746
() ty::PlaceholderRegion, (self, f, cx) {
726747
display {
@@ -738,6 +759,24 @@ define_print! {
738759
}
739760
}
740761

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+
741780
define_print! {
742781
() ty::RegionKind, (self, f, cx) {
743782
display {
@@ -845,6 +884,49 @@ define_print! {
845884
}
846885
}
847886

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+
848930
define_print! {
849931
() ty::FreeRegion, (self, f, cx) {
850932
debug {
@@ -931,6 +1013,24 @@ define_print! {
9311013
}
9321014
}
9331015

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+
9341034
define_print! {
9351035
() ty::InferTy, (self, f, cx) {
9361036
display {
@@ -1041,9 +1141,8 @@ define_print! {
10411141
}
10421142
Ref(r, ty, mutbl) => {
10431143
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(" "))?;
10471146
}
10481147
ty::TypeAndMut { ty, mutbl }.print(f, cx)
10491148
}
@@ -1089,17 +1188,16 @@ define_print! {
10891188
}
10901189
Adt(def, substs) => cx.parameterized(f, def.did, substs, iter::empty()),
10911190
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 {
10941193
write!(f, "(")?;
10951194
}
10961195
write!(f, "dyn ")?;
10971196
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(")"))?;
11021199
}
1200+
Ok(())
11031201
}
11041202
Foreign(def_id) => cx.parameterized(f, def_id, Substs::empty(), iter::empty()),
11051203
Projection(ref data) => data.print(f, cx),

0 commit comments

Comments
 (0)