From 60767b8bcdbb5c30c0993376265beba166a64697 Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Sun, 7 Nov 2021 18:18:16 +1100 Subject: [PATCH] Improve diagnostics when a static lifetime is expected --- .../nice_region_error/named_anon_conflict.rs | 5 +++++ ...sure-bounds-static-cant-capture-borrowed.stderr | 2 ++ .../generator/generator-region-requirements.stderr | 3 +++ .../projection-type-lifetime-mismatch.stderr | 6 ++++++ .../issue-46983-expected-return-static.rs} | 0 .../issue-46983-expected-return-static.stderr} | 4 +++- .../issue-90600-expected-return-static-indirect.rs | 14 ++++++++++++++ ...ue-90600-expected-return-static-indirect.stderr | 12 ++++++++++++ .../region-lbr-anon-does-not-outlive-static.stderr | 2 ++ src/test/ui/nll/guarantor-issue-46974.stderr | 3 +++ .../ui/regions/regions-static-bound.migrate.stderr | 5 +++++ .../ui/regions/regions-static-bound.nll.stderr | 5 +++++ 12 files changed, 60 insertions(+), 1 deletion(-) rename src/test/ui/{issues/issue-46983.rs => lifetimes/issue-46983-expected-return-static.rs} (100%) rename src/test/ui/{issues/issue-46983.stderr => lifetimes/issue-46983-expected-return-static.stderr} (58%) create mode 100644 src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs create mode 100644 src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs index 0878f8550da35..98aab32763aaf 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs @@ -123,6 +123,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { new_ty.to_string(), Applicability::Unspecified, ); + } else { + diag.span_label( + new_ty_span, + &format!("{} does not have lifetime `'static'`", span_label_var), + ); } Some(diag) diff --git a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr index a9add6184f13e..2ef7b367e3191 100644 --- a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr +++ b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr @@ -1,6 +1,8 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5 | +LL | fn foo(x: &()) { + | --- the type of `x` does not have lifetime `'static'` LL | bar(|| { | ^^^ lifetime `'static` required diff --git a/src/test/ui/generator/generator-region-requirements.stderr b/src/test/ui/generator/generator-region-requirements.stderr index de90a599e76b7..5603b6f960605 100644 --- a/src/test/ui/generator/generator-region-requirements.stderr +++ b/src/test/ui/generator/generator-region-requirements.stderr @@ -1,6 +1,9 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/generator-region-requirements.rs:12:51 | +LL | fn dangle(x: &mut i32) -> &'static mut i32 { + | -------- the type of `x` does not have lifetime `'static'` +... LL | GeneratorState::Complete(c) => return c, | ^ lifetime `'static` required diff --git a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr index 315bef16c5f13..00feb465786c1 100644 --- a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr +++ b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr @@ -1,18 +1,24 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/projection-type-lifetime-mismatch.rs:17:5 | +LL | fn f(x: &impl for<'a> X = &'a ()>) -> &'static () { + | ------------------------------- the type of `x` does not have lifetime `'static'` LL | x.m() | ^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `x` --> $DIR/projection-type-lifetime-mismatch.rs:22:5 | +LL | fn g X = &'a ()>>(x: &T) -> &'static () { + | -- the type of `x` does not have lifetime `'static'` LL | x.m() | ^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `x` --> $DIR/projection-type-lifetime-mismatch.rs:27:5 | +LL | fn h(x: &()) -> &'static () { + | --- the type of `x` does not have lifetime `'static'` LL | x.m() | ^^^^^ lifetime `'static` required diff --git a/src/test/ui/issues/issue-46983.rs b/src/test/ui/lifetimes/issue-46983-expected-return-static.rs similarity index 100% rename from src/test/ui/issues/issue-46983.rs rename to src/test/ui/lifetimes/issue-46983-expected-return-static.rs diff --git a/src/test/ui/issues/issue-46983.stderr b/src/test/ui/lifetimes/issue-46983-expected-return-static.stderr similarity index 58% rename from src/test/ui/issues/issue-46983.stderr rename to src/test/ui/lifetimes/issue-46983-expected-return-static.stderr index d328329edad22..1b58e3b06353e 100644 --- a/src/test/ui/issues/issue-46983.stderr +++ b/src/test/ui/lifetimes/issue-46983-expected-return-static.stderr @@ -1,6 +1,8 @@ error[E0621]: explicit lifetime required in the type of `x` - --> $DIR/issue-46983.rs:2:5 + --> $DIR/issue-46983-expected-return-static.rs:2:5 | +LL | fn foo(x: &u32) -> &'static u32 { + | ---- the type of `x` does not have lifetime `'static'` LL | &*x | ^^^ lifetime `'static` required diff --git a/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs new file mode 100644 index 0000000000000..fc3911efeb1ed --- /dev/null +++ b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs @@ -0,0 +1,14 @@ +use std::cell::RefCell; +use std::io::Read; + +fn main() {} + +fn inner(mut foo: &[u8]) { + let refcell = RefCell::new(&mut foo); + let read = &refcell as &RefCell; + + read_thing(read); + //~^ ERROR explicit lifetime required in the type of `foo` [E0621] +} + +fn read_thing(refcell: &RefCell) {} diff --git a/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr new file mode 100644 index 0000000000000..eb51f5c747b46 --- /dev/null +++ b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr @@ -0,0 +1,12 @@ +error[E0621]: explicit lifetime required in the type of `foo` + --> $DIR/issue-90600-expected-return-static-indirect.rs:10:16 + | +LL | fn inner(mut foo: &[u8]) { + | ----- the type of `foo` does not have lifetime `'static'` +... +LL | read_thing(read); + | ^^^^ lifetime `'static` required + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0621`. diff --git a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr index 4c302d935db96..a055f18783119 100644 --- a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr +++ b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr @@ -1,6 +1,8 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/region-lbr-anon-does-not-outlive-static.rs:9:5 | +LL | fn foo(x: &u32) -> &'static u32 { + | ---- the type of `x` does not have lifetime `'static'` LL | &*x | ^^^ lifetime `ReStatic` required diff --git a/src/test/ui/nll/guarantor-issue-46974.stderr b/src/test/ui/nll/guarantor-issue-46974.stderr index eabc3105c0293..e1772a55b518e 100644 --- a/src/test/ui/nll/guarantor-issue-46974.stderr +++ b/src/test/ui/nll/guarantor-issue-46974.stderr @@ -12,6 +12,9 @@ LL | *x error[E0621]: explicit lifetime required in the type of `s` --> $DIR/guarantor-issue-46974.rs:15:5 | +LL | fn bar(s: &Box<(i32,)>) -> &'static i32 { + | ------------ the type of `s` does not have lifetime `'static'` +LL | // FIXME(#46983): error message should be better LL | &s.0 | ^^^^ lifetime `'static` required diff --git a/src/test/ui/regions/regions-static-bound.migrate.stderr b/src/test/ui/regions/regions-static-bound.migrate.stderr index 8f11e148220d6..956ebd8cac239 100644 --- a/src/test/ui/regions/regions-static-bound.migrate.stderr +++ b/src/test/ui/regions/regions-static-bound.migrate.stderr @@ -14,12 +14,17 @@ LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a { error[E0621]: explicit lifetime required in the type of `u` --> $DIR/regions-static-bound.rs:14:5 | +LL | fn error(u: &(), v: &()) { + | --- the type of `u` does not have lifetime `'static'` LL | static_id(&u); | ^^^^^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `v` --> $DIR/regions-static-bound.rs:16:5 | +LL | fn error(u: &(), v: &()) { + | --- the type of `v` does not have lifetime `'static'` +... LL | static_id_indirect(&v); | ^^^^^^^^^^^^^^^^^^ lifetime `'static` required diff --git a/src/test/ui/regions/regions-static-bound.nll.stderr b/src/test/ui/regions/regions-static-bound.nll.stderr index a280c6f0a02d2..b2797b30e4856 100644 --- a/src/test/ui/regions/regions-static-bound.nll.stderr +++ b/src/test/ui/regions/regions-static-bound.nll.stderr @@ -9,12 +9,17 @@ LL | t error[E0621]: explicit lifetime required in the type of `u` --> $DIR/regions-static-bound.rs:14:5 | +LL | fn error(u: &(), v: &()) { + | --- the type of `u` does not have lifetime `'static'` LL | static_id(&u); | ^^^^^^^^^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `v` --> $DIR/regions-static-bound.rs:16:5 | +LL | fn error(u: &(), v: &()) { + | --- the type of `v` does not have lifetime `'static'` +... LL | static_id_indirect(&v); | ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required