Skip to content

Commit 2311ae9

Browse files
committed
rustc: print elided regions as '_ instead of nothing, and use a separate check when optional.
1 parent 60bbe39 commit 2311ae9

File tree

8 files changed

+42
-40
lines changed

8 files changed

+42
-40
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,11 +824,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
824824
mutbl: hir::Mutability,
825825
s: &mut DiagnosticStyledString,
826826
) {
827-
let r = &r.to_string();
827+
let mut r = r.to_string();
828+
if r == "'_" {
829+
r.clear();
830+
} else {
831+
r.push(' ');
832+
}
828833
s.push_highlighted(format!(
829-
"&{}{}{}",
834+
"&{}{}",
830835
r,
831-
if r == "" { "" } else { " " },
832836
if mutbl == hir::MutMutable { "mut " } else { "" }
833837
));
834838
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
@@ -171,7 +171,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
171171
let tymut_string = tymut.to_string();
172172
if tymut_string == "_" || //unknown type name,
173173
tymut_string.len() > 10 || //name longer than saying "reference",
174-
region.to_string() != "" //... or a complex type
174+
region.to_string() != "'_" //... or a complex type
175175
{
176176
format!("{}reference", match mutbl {
177177
hir::Mutability::MutMutable => "mutable ",

src/librustc/ty/print/pretty.rs

Lines changed: 28 additions & 30 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;
@@ -496,7 +492,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
496492
match substs[param.index as usize].unpack() {
497493
UnpackedKind::Lifetime(r) => {
498494
self.always_print_region_in_paths(r) ||
499-
self.print_region_outputs_anything(r)
495+
self.region_should_not_be_omitted(r)
500496
}
501497
_ => false,
502498
}
@@ -534,19 +530,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
534530
for arg in arg0.into_iter().chain(args) {
535531
maybe_comma(&mut cx)?;
536532

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

@@ -755,7 +738,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
755738
print_prefix: impl FnOnce(
756739
PrintCx<'_, 'gcx, 'tcx, Self>,
757740
) -> Result<Self::Path, Self::Error>,
758-
mut params: &[ty::GenericParamDef],
741+
params: &[ty::GenericParamDef],
759742
substs: &'tcx Substs<'tcx>,
760743
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
761744
) -> Result<Self::Path, Self::Error> {
@@ -821,7 +804,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
821804
*region != ty::ReErased
822805
}
823806

824-
fn print_region_outputs_anything(
807+
fn region_should_not_be_omitted(
825808
self: &PrintCx<'_, '_, '_, Self>,
826809
region: ty::Region<'_>,
827810
) -> bool {
@@ -901,8 +884,9 @@ impl<F: fmt::Write> FmtPrinter<F> {
901884
// `explain_region()` or `note_and_explain_region()`.
902885
match *region {
903886
ty::ReEarlyBound(ref data) => {
904-
if data.name != "'_" {
887+
if data.name != "" {
905888
p!(write("{}", data.name));
889+
return self.ok();
906890
}
907891
}
908892
ty::ReLateBound(_, br) |
@@ -918,6 +902,7 @@ impl<F: fmt::Write> FmtPrinter<F> {
918902
if let Some((region, counter)) = highlight.highlight_bound_region {
919903
if br == region {
920904
p!(write("'{}", counter));
905+
return self.ok();
921906
}
922907
}
923908
}
@@ -937,20 +922,33 @@ impl<F: fmt::Write> FmtPrinter<F> {
937922
first_statement_index.index()
938923
)),
939924
}
925+
return self.ok();
940926
}
941927
ty::ReVar(region_vid) if identify_regions => {
942928
p!(write("{:?}", region_vid));
929+
return self.ok();
943930
}
944931
ty::ReVar(_) => {}
945932
ty::ReScope(_) |
946933
ty::ReErased => {}
947-
ty::ReStatic => p!(write("'static")),
948-
ty::ReEmpty => p!(write("'<empty>")),
934+
ty::ReStatic => {
935+
p!(write("'static"));
936+
return self.ok();
937+
}
938+
ty::ReEmpty => {
939+
p!(write("'<empty>"));
940+
return self.ok();
941+
}
949942

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

950+
p!(write("'_"));
951+
954952
self.ok()
955953
}
956954
}
@@ -977,7 +975,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
977975
}
978976
ty::Ref(r, ty, mutbl) => {
979977
p!(write("&"));
980-
if self.print_region_outputs_anything(r) {
978+
if self.region_should_not_be_omitted(r) {
981979
p!(print(r), write(" "));
982980
}
983981
p!(print(ty::TypeAndMut { ty, mutbl }))
@@ -1026,7 +1024,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
10261024
nest!(|cx| cx.print_def_path(def.did, Some(substs), iter::empty()));
10271025
}
10281026
ty::Dynamic(data, r) => {
1029-
let print_r = self.print_region_outputs_anything(r);
1027+
let print_r = self.region_should_not_be_omitted(r);
10301028
if print_r {
10311029
p!(write("("));
10321030
}

src/librustc_codegen_utils/symbol_names.rs

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

515515
impl PrettyPrinter for SymbolPath {
516-
fn print_region_outputs_anything(
516+
fn region_should_not_be_omitted(
517517
self: &PrintCx<'_, '_, '_, Self>,
518518
_region: ty::Region<'_>,
519519
) -> 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; //~ ERROR cannot infer
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; //~ ERROR cannot infer
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; //~ ERROR cannot infer

0 commit comments

Comments
 (0)