Skip to content

Commit f8e4029

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

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
@@ -223,14 +223,10 @@ pub trait PrettyPrinter:
223223
false
224224
}
225225

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

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

@@ -759,7 +742,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
759742
print_prefix: impl FnOnce(
760743
PrintCx<'_, 'gcx, 'tcx, Self>,
761744
) -> Result<Self::Path, Self::Error>,
762-
mut params: &[ty::GenericParamDef],
745+
params: &[ty::GenericParamDef],
763746
substs: &'tcx Substs<'tcx>,
764747
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
765748
) -> Result<Self::Path, Self::Error> {
@@ -825,7 +808,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
825808
*region != ty::ReErased
826809
}
827810

828-
fn print_region_outputs_anything(
811+
fn region_should_not_be_omitted(
829812
self: &PrintCx<'_, '_, '_, Self>,
830813
region: ty::Region<'_>,
831814
) -> bool {
@@ -905,8 +888,9 @@ impl<F: fmt::Write> FmtPrinter<F> {
905888
// `explain_region()` or `note_and_explain_region()`.
906889
match *region {
907890
ty::ReEarlyBound(ref data) => {
908-
if data.name != "'_" {
891+
if data.name != "" {
909892
p!(write("{}", data.name));
893+
return self.ok();
910894
}
911895
}
912896
ty::ReLateBound(_, br) |
@@ -922,6 +906,7 @@ impl<F: fmt::Write> FmtPrinter<F> {
922906
if let Some((region, counter)) = highlight.highlight_bound_region {
923907
if br == region {
924908
p!(write("'{}", counter));
909+
return self.ok();
925910
}
926911
}
927912
}
@@ -941,20 +926,33 @@ impl<F: fmt::Write> FmtPrinter<F> {
941926
first_statement_index.index()
942927
)),
943928
}
929+
return self.ok();
944930
}
945931
ty::ReVar(region_vid) if identify_regions => {
946932
p!(write("{:?}", region_vid));
933+
return self.ok();
947934
}
948935
ty::ReVar(_) => {}
949936
ty::ReScope(_) |
950937
ty::ReErased => {}
951-
ty::ReStatic => p!(write("'static")),
952-
ty::ReEmpty => p!(write("'<empty>")),
938+
ty::ReStatic => {
939+
p!(write("'static"));
940+
return self.ok();
941+
}
942+
ty::ReEmpty => {
943+
p!(write("'<empty>"));
944+
return self.ok();
945+
}
953946

954947
// The user should never encounter these in unsubstituted form.
955-
ty::ReClosureBound(vid) => p!(write("{:?}", vid)),
948+
ty::ReClosureBound(vid) => {
949+
p!(write("{:?}", vid));
950+
return self.ok();
951+
}
956952
}
957953

954+
p!(write("'_"));
955+
958956
self.ok()
959957
}
960958
}
@@ -981,7 +979,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
981979
}
982980
ty::Ref(r, ty, mutbl) => {
983981
p!(write("&"));
984-
if self.print_region_outputs_anything(r) {
982+
if self.region_should_not_be_omitted(r) {
985983
p!(print(r), write(" "));
986984
}
987985
p!(print(ty::TypeAndMut { ty, mutbl }))
@@ -1030,7 +1028,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
10301028
nest!(|cx| cx.print_def_path(def.did, Some(substs), iter::empty()));
10311029
}
10321030
ty::Dynamic(data, r) => {
1033-
let print_r = self.print_region_outputs_anything(r);
1031+
let print_r = self.region_should_not_be_omitted(r);
10341032
if print_r {
10351033
p!(write("("));
10361034
}

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)