Skip to content

Commit 4deaa69

Browse files
committed
rustc: print elided regions as '_ instead of nothing, and use a separate check when optional.
1 parent 72690d2 commit 4deaa69

File tree

8 files changed

+41
-39
lines changed

8 files changed

+41
-39
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
826826
mutbl: hir::Mutability,
827827
s: &mut DiagnosticStyledString,
828828
) {
829-
let r = &r.to_string();
829+
let mut r = r.to_string();
830+
if r == "'_" {
831+
r.clear();
832+
} else {
833+
r.push(' ');
834+
}
830835
s.push_highlighted(format!(
831-
"&{}{}{}",
836+
"&{}{}",
832837
r,
833-
if r == "" { "" } else { " " },
834838
if mutbl == hir::MutMutable { "mut " } else { "" }
835839
));
836840
s.push_normal(ty.to_string());

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.is_empty())
414+
.filter(|k| k != "'_")
415415
.collect::<Vec<_>>().join(", "));
416416
w.push('>');
417417
}

src/librustc/ty/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
190190
let tymut_string = tymut.to_string();
191191
if tymut_string == "_" || //unknown type name,
192192
tymut_string.len() > 10 || //name longer than saying "reference",
193-
region.to_string() != "" //... or a complex type
193+
region.to_string() != "'_" //... or a complex type
194194
{
195195
format!("{}reference", match mutbl {
196196
hir::Mutability::MutMutable => "mutable ",

src/librustc/ty/print/pretty.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,10 @@ pub trait PrettyPrinter:
222222
false
223223
}
224224

225-
// HACK(eddyb) Trying to print a lifetime might not print anything, which
226-
// may need special handling in the caller (of `ty::RegionKind::print`).
227-
// To avoid printing to a temporary string (which isn't even supported),
228-
// the `print_region_outputs_anything` method can instead be used to
229-
// determine this, ahead of time.
230-
//
231-
// NB: this must be kept in sync with the implementation of `print_region`.
232-
fn print_region_outputs_anything(
225+
/// Return `true` if the region should be printed in
226+
/// optional positions, e.g. `&'a T` or `dyn Tr + 'b`.
227+
/// This is typically the case for all non-`'_` regions.
228+
fn region_should_not_be_omitted(
233229
self: &PrintCx<'_, '_, '_, Self>,
234230
region: ty::Region<'_>,
235231
) -> bool;
@@ -497,7 +493,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
497493
match substs[param.index as usize].unpack() {
498494
UnpackedKind::Lifetime(r) => {
499495
self.always_print_region_in_paths(r) ||
500-
self.print_region_outputs_anything(r)
496+
self.region_should_not_be_omitted(r)
501497
}
502498
_ => false,
503499
}
@@ -535,19 +531,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
535531
for arg in arg0.into_iter().chain(args) {
536532
maybe_comma(&mut cx)?;
537533

538-
if let UnpackedKind::Lifetime(region) = arg.unpack() {
539-
if !cx.print_region_outputs_anything(region) {
540-
// This happens when the value of the region
541-
// parameter is not easily serialized. This may be
542-
// because the user omitted it in the first place,
543-
// or because it refers to some block in the code,
544-
// etc. I'm not sure how best to serialize this.
545-
p!(write("'_"));
546-
547-
continue;
548-
}
549-
}
550-
551534
p!(print(arg));
552535
}
553536

@@ -822,7 +805,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
822805
*region != ty::ReErased
823806
}
824807

825-
fn print_region_outputs_anything(
808+
fn region_should_not_be_omitted(
826809
self: &PrintCx<'_, '_, '_, Self>,
827810
region: ty::Region<'_>,
828811
) -> bool {
@@ -902,8 +885,9 @@ impl<F: fmt::Write> FmtPrinter<F> {
902885
// `explain_region()` or `note_and_explain_region()`.
903886
match *region {
904887
ty::ReEarlyBound(ref data) => {
905-
if data.name != "'_" {
888+
if data.name != "" {
906889
p!(write("{}", data.name));
890+
return self.ok();
907891
}
908892
}
909893
ty::ReLateBound(_, br) |
@@ -919,6 +903,7 @@ impl<F: fmt::Write> FmtPrinter<F> {
919903
if let Some((region, counter)) = highlight.highlight_bound_region {
920904
if br == region {
921905
p!(write("'{}", counter));
906+
return self.ok();
922907
}
923908
}
924909
}
@@ -938,20 +923,33 @@ impl<F: fmt::Write> FmtPrinter<F> {
938923
first_statement_index.index()
939924
)),
940925
}
926+
return self.ok();
941927
}
942928
ty::ReVar(region_vid) if identify_regions => {
943929
p!(write("{:?}", region_vid));
930+
return self.ok();
944931
}
945932
ty::ReVar(_) => {}
946933
ty::ReScope(_) |
947934
ty::ReErased => {}
948-
ty::ReStatic => p!(write("'static")),
949-
ty::ReEmpty => p!(write("'<empty>")),
935+
ty::ReStatic => {
936+
p!(write("'static"));
937+
return self.ok();
938+
}
939+
ty::ReEmpty => {
940+
p!(write("'<empty>"));
941+
return self.ok();
942+
}
950943

951944
// The user should never encounter these in unsubstituted form.
952-
ty::ReClosureBound(vid) => p!(write("{:?}", vid)),
945+
ty::ReClosureBound(vid) => {
946+
p!(write("{:?}", vid));
947+
return self.ok();
948+
}
953949
}
954950

951+
p!(write("'_"));
952+
955953
self.ok()
956954
}
957955
}
@@ -978,7 +976,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
978976
}
979977
ty::Ref(r, ty, mutbl) => {
980978
p!(write("&"));
981-
if self.print_region_outputs_anything(r) {
979+
if self.region_should_not_be_omitted(r) {
982980
p!(print(r), write(" "));
983981
}
984982
p!(print(ty::TypeAndMut { ty, mutbl }))
@@ -1027,7 +1025,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
10271025
nest!(|cx| cx.print_def_path(def.did, Some(substs), iter::empty()));
10281026
}
10291027
ty::Dynamic(data, r) => {
1030-
let print_r = self.print_region_outputs_anything(r);
1028+
let print_r = self.region_should_not_be_omitted(r);
10311029
if print_r {
10321030
p!(write("("));
10331031
}

src/librustc_codegen_utils/symbol_names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl Printer for SymbolPath {
512512
}
513513

514514
impl PrettyPrinter for SymbolPath {
515-
fn print_region_outputs_anything(
515+
fn region_should_not_be_omitted(
516516
self: &PrintCx<'_, '_, '_, Self>,
517517
_region: ty::Region<'_>,
518518
) -> bool {

src/test/ui/issues/issue-20831-debruijn.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | | }
1111
| |_____^ lifetime mismatch
1212
|
1313
= note: expected type `'a`
14-
found type ``
14+
found type `'_`
1515
note: the anonymous lifetime #2 defined on the method body at 28:5...
1616
--> $DIR/issue-20831-debruijn.rs:28:5
1717
|
@@ -42,7 +42,7 @@ LL | | }
4242
| |_____^ lifetime mismatch
4343
|
4444
= note: expected type `'a`
45-
found type ``
45+
found type `'_`
4646
note: the lifetime 'a as defined on the impl at 26:6...
4747
--> $DIR/issue-20831-debruijn.rs:26:6
4848
|

src/test/ui/regions/regions-addr-of-upvar-self.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0495]: cannot infer an appropriate lifetime for borrow expression due to
44
LL | let p: &'static mut usize = &mut self.food;
55
| ^^^^^^^^^^^^^^
66
|
7-
note: first, the lifetime cannot outlive the lifetime as defined on the body at 9:18...
7+
note: first, the lifetime cannot outlive the lifetime '_ as defined on the body at 9:18...
88
--> $DIR/regions-addr-of-upvar-self.rs:9:18
99
|
1010
LL | let _f = || {

src/test/ui/regions/regions-return-ref-to-upvar-issue-17403.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0495]: cannot infer an appropriate lifetime for borrow expression due to
44
LL | let mut f = || &mut x;
55
| ^^^^^^
66
|
7-
note: first, the lifetime cannot outlive the lifetime as defined on the body at 7:21...
7+
note: first, the lifetime cannot outlive the lifetime '_ as defined on the body at 7:21...
88
--> $DIR/regions-return-ref-to-upvar-issue-17403.rs:7:21
99
|
1010
LL | let mut f = || &mut x;

0 commit comments

Comments
 (0)