Skip to content

Commit 0e8adf9

Browse files
committed
rustc: always rely on '_ to be not printed by ty::Region itself.
1 parent 1913272 commit 0e8adf9

File tree

8 files changed

+69
-79
lines changed

8 files changed

+69
-79
lines changed

src/librustc/traits/specialize/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ fn to_pretty_impl_header(tcx: TyCtxt<'_, '_, '_>, impl_def_id: DefId) -> Option<
411411
w.push('<');
412412
w.push_str(&substs.iter()
413413
.map(|k| k.to_string())
414-
.filter(|k| &k[..] != "'_")
414+
.filter(|k| !k.is_empty())
415415
.collect::<Vec<_>>().join(", "));
416416
w.push('>');
417417
}

src/librustc/ty/structural_impls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ CloneTypeFoldableAndLiftImpls! {
5353
::ty::InferTy,
5454
::ty::IntVarValue,
5555
::ty::ParamTy,
56+
::ty::RegionVid,
5657
::ty::UniverseIndex,
5758
::ty::Variance,
5859
::syntax_pos::Span,

src/librustc/util/ppaux.rs

Lines changed: 58 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,6 @@ impl RegionHighlightMode {
119119
Self::highlighting_region(&ty::ReVar(vid), number, op)
120120
}
121121

122-
/// Returns true if any placeholders are highlighted.
123-
fn any_region_vids_highlighted(&self) -> bool {
124-
Self::get()
125-
.highlight_regions
126-
.iter()
127-
.any(|h| match h {
128-
Some((ty::ReVar(_), _)) => true,
129-
_ => false,
130-
})
131-
}
132-
133122
/// Returns `Some(n)` with the number to use for the given region,
134123
/// if any.
135124
fn region_highlighted(&self, region: ty::Region<'_>) -> Option<usize> {
@@ -163,17 +152,6 @@ impl RegionHighlightMode {
163152
)
164153
}
165154

166-
/// Returns true if any placeholders are highlighted.
167-
pub fn any_placeholders_highlighted(&self) -> bool {
168-
Self::get()
169-
.highlight_regions
170-
.iter()
171-
.any(|h| match h {
172-
Some((ty::RePlaceholder(_), _)) => true,
173-
_ => false,
174-
})
175-
}
176-
177155
/// Returns `Some(N)` if the placeholder `p` is highlighted to print as `'N`.
178156
pub fn placeholder_highlight(&self, p: ty::PlaceholderRegion) -> Option<usize> {
179157
self.region_highlighted(&ty::RePlaceholder(p))
@@ -420,7 +398,7 @@ impl PrintCx<'a, 'gcx, 'tcx> {
420398
if self.is_verbose {
421399
write!(f, "{:?}", region)?;
422400
} else {
423-
let s = region.to_string();
401+
let s = region.print_display_to_string(self);
424402
if s.is_empty() {
425403
// This happens when the value of the region
426404
// parameter is not easily serialized. This may be
@@ -714,19 +692,20 @@ define_print! {
714692
return self.print_debug(f, cx);
715693
}
716694

717-
if let Some((region, counter)) = RegionHighlightMode::get().highlight_bound_region {
718-
if *self == region {
719-
return match *self {
720-
BrNamed(_, name) => write!(f, "{}", name),
721-
BrAnon(_) | BrFresh(_) | BrEnv => write!(f, "'{}", counter)
722-
};
695+
if let BrNamed(_, name) = *self {
696+
if name != "" && name != "'_" {
697+
return write!(f, "{}", name);
723698
}
724699
}
725700

726-
match *self {
727-
BrNamed(_, name) => write!(f, "{}", name),
728-
BrAnon(_) | BrFresh(_) | BrEnv => Ok(())
701+
let highlight = RegionHighlightMode::get();
702+
if let Some((region, counter)) = highlight.highlight_bound_region {
703+
if *self == region {
704+
return write!(f, "'{}", counter);
705+
}
729706
}
707+
708+
Ok(())
730709
}
731710
debug {
732711
return match *self {
@@ -751,12 +730,10 @@ define_print! {
751730

752731
let highlight = RegionHighlightMode::get();
753732
if let Some(counter) = highlight.placeholder_highlight(*self) {
754-
write!(f, "'{}", counter)
755-
} else if highlight.any_placeholders_highlighted() {
756-
write!(f, "'_")
757-
} else {
758-
write!(f, "{}", self.name)
733+
return write!(f, "'{}", counter);
759734
}
735+
736+
write!(f, "{}", self.name)
760737
}
761738
}
762739
}
@@ -779,7 +756,11 @@ define_print! {
779756
// `explain_region()` or `note_and_explain_region()`.
780757
match *self {
781758
ty::ReEarlyBound(ref data) => {
782-
write!(f, "{}", data.name)
759+
if data.name != "'_" {
760+
write!(f, "{}", data.name)
761+
} else {
762+
Ok(())
763+
}
783764
}
784765
ty::ReLateBound(_, br) |
785766
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => {
@@ -806,14 +787,11 @@ define_print! {
806787
),
807788
}
808789
}
790+
ty::ReVar(region_vid) if cx.identify_regions => {
791+
write!(f, "{:?}", region_vid)
792+
}
809793
ty::ReVar(region_vid) => {
810-
if RegionHighlightMode::get().any_region_vids_highlighted() {
811-
write!(f, "{:?}", region_vid)
812-
} else if cx.identify_regions {
813-
write!(f, "'{}rv", region_vid.index())
814-
} else {
815-
Ok(())
816-
}
794+
write!(f, "{}", region_vid)
817795
}
818796
ty::ReScope(_) |
819797
ty::ReErased => Ok(()),
@@ -926,32 +904,46 @@ impl fmt::Debug for ty::FloatVid {
926904
}
927905
}
928906

929-
impl fmt::Debug for ty::RegionVid {
930-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
931-
if let Some(counter) = RegionHighlightMode::get().region_highlighted(&ty::ReVar(*self)) {
932-
return write!(f, "'{:?}", counter);
933-
} else if RegionHighlightMode::get().any_region_vids_highlighted() {
934-
return write!(f, "'_");
907+
define_print! {
908+
() ty::RegionVid, (self, f, cx) {
909+
display {
910+
if cx.is_verbose {
911+
return self.print_debug(f, cx);
912+
}
913+
914+
let highlight = RegionHighlightMode::get();
915+
if let Some(counter) = highlight.region_highlighted(&ty::ReVar(*self)) {
916+
return write!(f, "'{:?}", counter);
917+
}
918+
919+
Ok(())
935920
}
921+
debug {
922+
// HACK(eddyb) this is duplicated from `display` printing,
923+
// to keep NLL borrowck working even with `-Zverbose`.
924+
let highlight = RegionHighlightMode::get();
925+
if let Some(counter) = highlight.region_highlighted(&ty::ReVar(*self)) {
926+
return write!(f, "'{:?}", counter);
927+
}
936928

937-
write!(f, "'_#{}r", self.index())
929+
write!(f, "'_#{}r", self.index())
930+
}
938931
}
939932
}
940933

941934
define_print! {
942935
() ty::InferTy, (self, f, cx) {
943936
display {
944937
if cx.is_verbose {
945-
print!(f, cx, print_debug(self))
946-
} else {
947-
match *self {
948-
ty::TyVar(_) => write!(f, "_"),
949-
ty::IntVar(_) => write!(f, "{}", "{integer}"),
950-
ty::FloatVar(_) => write!(f, "{}", "{float}"),
951-
ty::FreshTy(v) => write!(f, "FreshTy({})", v),
952-
ty::FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
953-
ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v)
954-
}
938+
return self.print_debug(f, cx);
939+
}
940+
match *self {
941+
ty::TyVar(_) => write!(f, "_"),
942+
ty::IntVar(_) => write!(f, "{}", "{integer}"),
943+
ty::FloatVar(_) => write!(f, "{}", "{float}"),
944+
ty::FreshTy(v) => write!(f, "FreshTy({})", v),
945+
ty::FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
946+
ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v)
955947
}
956948
}
957949
debug {
@@ -1049,12 +1041,9 @@ define_print! {
10491041
}
10501042
Ref(r, ty, mutbl) => {
10511043
write!(f, "&")?;
1052-
let s = r.print_to_string(cx);
1053-
if s != "'_" {
1054-
write!(f, "{}", s)?;
1055-
if !s.is_empty() {
1056-
write!(f, " ")?;
1057-
}
1044+
let s = r.print_display_to_string(cx);
1045+
if !s.is_empty() {
1046+
write!(f, "{} ", s)?;
10581047
}
10591048
ty::TypeAndMut { ty, mutbl }.print(f, cx)
10601049
}
@@ -1100,7 +1089,7 @@ define_print! {
11001089
}
11011090
Adt(def, substs) => cx.parameterized(f, def.did, substs, iter::empty()),
11021091
Dynamic(data, r) => {
1103-
let r = r.print_to_string(cx);
1092+
let r = r.print_display_to_string(cx);
11041093
if !r.is_empty() {
11051094
write!(f, "(")?;
11061095
}

src/test/ui/issues/issue-17905-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: mismatched method receiver
44
LL | fn say(self: &Pair<&str, isize>) {
55
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch
66
|
7-
= note: expected type `Pair<&'_ str, _>`
7+
= note: expected type `Pair<&str, _>`
88
found type `Pair<&str, _>`
99
note: the anonymous lifetime #2 defined on the method body at 8:5...
1010
--> $DIR/issue-17905-2.rs:8:5
@@ -27,7 +27,7 @@ error[E0308]: mismatched method receiver
2727
LL | fn say(self: &Pair<&str, isize>) {
2828
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch
2929
|
30-
= note: expected type `Pair<&'_ str, _>`
30+
= note: expected type `Pair<&str, _>`
3131
found type `Pair<&str, _>`
3232
note: the lifetime '_ as defined on the impl at 5:5...
3333
--> $DIR/issue-17905-2.rs:5:5

src/test/ui/nll/closure-requirements/escape-argument-callee.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | let mut closure = expect_sig(|p, y| *p = y);
1616
| - - ^^^^^^ assignment requires that `'1` must outlive `'2`
1717
| | |
1818
| | has type `&'1 i32`
19-
| has type `&mut &'2 i32`
19+
| has type `&'_#2r mut &'2 i32`
2020

2121
note: No external requirements
2222
--> $DIR/escape-argument-callee.rs:20:1

src/test/ui/nll/closure-requirements/propagate-approximated-fail-no-postdom.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ error: lifetime may not live long enough
2020
--> $DIR/propagate-approximated-fail-no-postdom.rs:46:13
2121
|
2222
LL | |_outlives1, _outlives2, _outlives3, x, y| {
23-
| ---------- ---------- has type `std::cell::Cell<&'2 &u32>`
23+
| ---------- ---------- has type `std::cell::Cell<&'2 &'_#3r u32>`
2424
| |
25-
| has type `std::cell::Cell<&&'1 u32>`
25+
| has type `std::cell::Cell<&'_#1r &'1 u32>`
2626
...
2727
LL | demand_y(x, y, p) //~ ERROR
2828
| ^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`

src/test/ui/nll/closure-requirements/propagate-fail-to-approximate-longer-no-bounds.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ error: lifetime may not live long enough
2020
--> $DIR/propagate-fail-to-approximate-longer-no-bounds.rs:37:9
2121
|
2222
LL | establish_relationships(&cell_a, &cell_b, |_outlives, x, y| {
23-
| --------- - has type `&std::cell::Cell<&'1 u32>`
23+
| --------- - has type `&'_#7r std::cell::Cell<&'1 u32>`
2424
| |
25-
| has type `&std::cell::Cell<&'2 &u32>`
25+
| has type `&'_#5r std::cell::Cell<&'2 &'_#1r u32>`
2626
LL | // Only works if 'x: 'y:
2727
LL | demand_y(x, y, x.get())
2828
| ^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`

src/test/ui/nll/closure-requirements/propagate-fail-to-approximate-longer-wrong-bounds.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ error: lifetime may not live long enough
2020
--> $DIR/propagate-fail-to-approximate-longer-wrong-bounds.rs:41:9
2121
|
2222
LL | establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
23-
| ---------- ---------- has type `&std::cell::Cell<&'2 &u32>`
23+
| ---------- ---------- has type `&'_#8r std::cell::Cell<&'2 &'_#2r u32>`
2424
| |
25-
| has type `&std::cell::Cell<&'1 &u32>`
25+
| has type `&'_#6r std::cell::Cell<&'1 &'_#1r u32>`
2626
LL | // Only works if 'x: 'y:
2727
LL | demand_y(x, y, x.get())
2828
| ^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`

0 commit comments

Comments
 (0)