Skip to content

Commit

Permalink
Don't suggest replacing region with 'static in NLL
Browse files Browse the repository at this point in the history
Fixes #73159

This is similar to #69350 - if the user didn't initially
write out a 'static lifetime, adding 'static in response to
a lifetime error is usually the wrong thing to do.
  • Loading branch information
Aaron1011 committed Oct 4, 2021
1 parent 08759c6 commit fdd8a0d
Show file tree
Hide file tree
Showing 62 changed files with 25 additions and 214 deletions.
Expand Up @@ -171,9 +171,7 @@ impl OutlivesSuggestionBuilder {
let outlived_fr_name = self.region_vid_to_name(mbcx, errci.outlived_fr);

if let (Some(fr_name), Some(outlived_fr_name)) = (fr_name, outlived_fr_name) {
if let RegionNameSource::Static = outlived_fr_name.source {
diag.help(&format!("consider replacing `{}` with `'static`", fr_name));
} else {
if !matches!(outlived_fr_name.source, RegionNameSource::Static) {
diag.help(&format!(
"consider adding the following bound: `{}: {}`",
fr_name, outlived_fr_name
Expand Down
Expand Up @@ -5,8 +5,6 @@ LL | fn baz<'a,'b>(x: &'a u32) -> &'static u32 {
| -- lifetime `'a` defined here
LL | bar(foo, x)
| ^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

Expand Up @@ -6,8 +6,6 @@ LL | fn baz<'a, 'b>(x: Type<'a>) -> Type<'static> {
...
LL | bar(foo, x)
| ^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

Expand Up @@ -18,8 +18,6 @@ LL | fn expect_free_supply_free_from_fn<'x>(x: &'x u32) {
...
LL | with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {});
| ^ requires that `'x` must outlive `'static`
|
= help: consider replacing `'x` with `'static`

error[E0308]: mismatched types
--> $DIR/expect-fn-supply-fn.rs:32:49
Expand Down
Expand Up @@ -17,8 +17,6 @@ LL | fn expect_bound_supply_named<'x>() {
...
LL | closure_expecting_bound(|x: &'x u32| {
| ^ requires that `'x` must outlive `'static`
|
= help: consider replacing `'x` with `'static`

error: aborting due to 2 previous errors

2 changes: 0 additions & 2 deletions src/test/ui/hrtb/hrtb-just-for-static.nll.stderr
Expand Up @@ -14,8 +14,6 @@ LL | fn give_some<'a>() {
| -- lifetime `'a` defined here
LL | want_hrtb::<&'a u32>()
| ^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: implementation of `Foo` is not general enough
--> $DIR/hrtb-just-for-static.rs:30:5
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/hrtb/hrtb-perfect-forwarding.nll.stderr
Expand Up @@ -54,8 +54,6 @@ LL | fn foo_hrtb_bar_not<'b, T>(mut t: T)
...
LL | foo_hrtb_bar_not(&mut t);
| ^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'b` must outlive `'static`
|
= help: consider replacing `'b` with `'static`

error: implementation of `Bar` is not general enough
--> $DIR/hrtb-perfect-forwarding.rs:43:5
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/impl-header-lifetime-elision/dyn-trait.nll.stderr
Expand Up @@ -5,8 +5,6 @@ LL | fn with_dyn_debug_static<'a>(x: Box<dyn Debug + 'a>) {
| - `x` is a reference that is only valid in the function body
LL | static_val(x);
| ^^^^^^^^^^^^^ `x` escapes the function body here
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

Expand Down
Expand Up @@ -19,7 +19,6 @@ LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
| |
| lifetime `'a` defined here
|
= help: consider replacing `'a` with `'static`
help: to allow this `impl Trait` to capture borrowed data with lifetime `'a`, add `'a` as a bound
|
LL | fn explicit<'a>(x: &'a i32) -> impl Copy + 'a { x }
Expand All @@ -42,7 +41,6 @@ LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
| -- lifetime `'a` defined here ^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`
= help: consider replacing `'a` with `'static`

error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/must_outlive_least_region_or_bound.rs:11:41
Expand All @@ -67,7 +65,6 @@ LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
| -- lifetime `'a` defined here ^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`
= help: consider replacing `'a` with `'static`

error: lifetime may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:32:61
Expand Down
Expand Up @@ -19,7 +19,6 @@ LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
| |
| lifetime `'a` defined here
|
= help: consider replacing `'a` with `'static`
help: to allow this `impl Trait` to capture borrowed data with lifetime `'a`, add `'a` as a bound
|
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-10291.nll.stderr
Expand Up @@ -6,8 +6,6 @@ LL | fn test<'x>(x: &'x isize) {
LL | drop::<Box<dyn for<'z> FnMut(&'z isize) -> &'z isize>>(Box::new(|z| {
LL | x
| ^ returning this value requires that `'x` must outlive `'static`
|
= help: consider replacing `'x` with `'static`

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-26217.nll.stderr
Expand Up @@ -5,8 +5,6 @@ LL | fn bar<'a>() {
| -- lifetime `'a` defined here
LL | foo::<&'a i32>();
| ^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-54943.nll.stderr
Expand Up @@ -6,8 +6,6 @@ LL | fn boo<'a>() {
...
LL | let x = foo::<&'a u32>();
| ^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

4 changes: 0 additions & 4 deletions src/test/ui/issues/issue-55796.nll.stderr
Expand Up @@ -6,8 +6,6 @@ LL | pub trait Graph<'a> {
...
LL | Box::new(self.out_edges(u).map(|e| e.target()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: lifetime may not live long enough
--> $DIR/issue-55796.rs:23:9
Expand All @@ -17,8 +15,6 @@ LL | pub trait Graph<'a> {
...
LL | Box::new(self.in_edges(u).map(|e| e.target()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to 2 previous errors

2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-75777.nll.stderr
Expand Up @@ -6,8 +6,6 @@ LL | fn inject<'a, Env: 'a, A: 'a + Send>(v: A) -> Box<dyn FnOnce(&'a Env) -> Bo
LL | let fut: BoxFuture<'a, A> = Box::pin(future::ready(v));
LL | Box::new(move |_| fut)
| ^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

Expand Up @@ -6,8 +6,6 @@ LL | fn test2<'a>(x: &'a Box<dyn Fn() + 'a>) {
LL | // but ref_obj will not, so warn.
LL | ref_obj(x)
| ^^^^^^^^^^ `x` escapes the function body here
|
= help: consider replacing `'a` with `'static`

error[E0521]: borrowed data escapes outside of function
--> $DIR/lifetime-bound-will-change-warning.rs:39:5
Expand All @@ -17,8 +15,6 @@ LL | fn test2cc<'a>(x: &'a Box<dyn Fn() + 'a>) {
LL | // same as test2, but cross crate
LL | lib::ref_obj(x)
| ^^^^^^^^^^^^^^^ `x` escapes the function body here
|
= help: consider replacing `'a` with `'static`

error: aborting due to 2 previous errors

Expand Down
4 changes: 0 additions & 4 deletions src/test/ui/lub-if.nll.stderr
Expand Up @@ -6,8 +6,6 @@ LL | pub fn opt_str2<'a>(maybestr: &'a Option<String>) -> &'static str {
...
LL | s
| ^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: lifetime may not live long enough
--> $DIR/lub-if.rs:35:9
Expand All @@ -17,8 +15,6 @@ LL | pub fn opt_str3<'a>(maybestr: &'a Option<String>) -> &'static str {
...
LL | s
| ^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to 2 previous errors

4 changes: 0 additions & 4 deletions src/test/ui/lub-match.nll.stderr
Expand Up @@ -6,8 +6,6 @@ LL | pub fn opt_str2<'a>(maybestr: &'a Option<String>) -> &'static str {
...
LL | s
| ^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: lifetime may not live long enough
--> $DIR/lub-match.rs:39:13
Expand All @@ -17,8 +15,6 @@ LL | pub fn opt_str3<'a>(maybestr: &'a Option<String>) -> &'static str {
...
LL | s
| ^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to 2 previous errors

Expand Up @@ -46,8 +46,6 @@ LL | | // Only works if 'x: 'y:
LL | | demand_y(x, y, x.get())
LL | | });
| |______^ `cell_a` escapes the function body here
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

Expand Down
Expand Up @@ -46,8 +46,6 @@ LL | | // Only works if 'x: 'y:
LL | | demand_y(x, y, x.get())
LL | | });
| |______^ `cell_a` escapes the function body here
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

Expand Down
Expand Up @@ -5,8 +5,6 @@ LL | fn foo<'a>(x: &'a u32) -> &'static u32 {
| -- lifetime `'a` defined here
LL | &*x
| ^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/ui/nll/issue-50716.nll.stderr
Expand Up @@ -6,8 +6,6 @@ LL | fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>)
...
LL | let _x = *s;
| ^^ proving this value is `Sized` requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/ui/nll/issue-55401.nll.stderr
Expand Up @@ -6,8 +6,6 @@ LL | fn static_to_a_to_static_through_ref_in_tuple<'a>(x: &'a u32) -> &'static u
LL | let (ref y, _z): (&'a u32, u32) = (&22, 44);
LL | *y
| ^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

4 changes: 0 additions & 4 deletions src/test/ui/nll/issue-58299.stderr
Expand Up @@ -6,8 +6,6 @@ LL | fn foo<'a>(x: i32) {
...
LL | A::<'a>::X..=A::<'static>::X => (),
| ^^^^^^^^^^ requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: lifetime may not live long enough
--> $DIR/issue-58299.rs:24:27
Expand All @@ -17,8 +15,6 @@ LL | fn bar<'a>(x: i32) {
...
LL | A::<'static>::X..=A::<'a>::X => (),
| ^^^^^^^^^^ requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to 2 previous errors

14 changes: 14 additions & 0 deletions src/test/ui/nll/issue-73159-rpit-static.rs
@@ -0,0 +1,14 @@
// Regression test for issue #73159
// Tests thar we don't suggest replacing 'a with 'static'

#![feature(nll)]

struct Foo<'a>(&'a [u8]);

impl<'a> Foo<'a> {
fn make_it(&self) -> impl Iterator<Item = u8> { //~ ERROR lifetime may not live
self.0.iter().copied()
}
}

fn main() {}
10 changes: 10 additions & 0 deletions src/test/ui/nll/issue-73159-rpit-static.stderr
@@ -0,0 +1,10 @@
error: lifetime may not live long enough
--> $DIR/issue-73159-rpit-static.rs:9:26
|
LL | impl<'a> Foo<'a> {
| -- lifetime `'a` defined here
LL | fn make_it(&self) -> impl Iterator<Item = u8> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ opaque type requires that `'a` must outlive `'static`

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/ui/nll/mir_check_cast_reify.stderr
Expand Up @@ -6,8 +6,6 @@ LL | fn bar<'a>(x: &'a u32) -> &'static u32 {
...
LL | f(x)
| ^^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/ui/nll/mir_check_cast_unsafe_fn.stderr
Expand Up @@ -6,8 +6,6 @@ LL | fn bar<'a>(input: &'a u32, f: fn(&'a u32) -> &'a u32) -> &'static u32 {
...
LL | unsafe { g(input) }
| ^^^^^^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/ui/nll/mir_check_cast_unsize.stderr
Expand Up @@ -5,8 +5,6 @@ LL | fn bar<'a>(x: &'a u32) -> &'static dyn Debug {
| -- lifetime `'a` defined here
LL | x
| ^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: aborting due to previous error

4 changes: 0 additions & 4 deletions src/test/ui/nll/outlives-suggestion-more.stderr
Expand Up @@ -46,8 +46,6 @@ LL | fn foo2<'a, 'b, 'c>(x: &'a usize, y: &'b usize) -> (&'c usize, &'static usi
| -- lifetime `'b` defined here
LL | (x, y)
| ^^^^^^ returning this value requires that `'b` must outlive `'static`
|
= help: consider replacing `'b` with `'static`

help: the following changes may resolve your lifetime errors
|
Expand Down Expand Up @@ -88,8 +86,6 @@ LL | fn foo3<'a, 'b, 'c, 'd, 'e>(
...
LL | (x, y, z)
| ^^^^^^^^^ returning this value requires that `'c` must outlive `'static`
|
= help: consider replacing `'c` with `'static`

help: the following changes may resolve your lifetime errors
|
Expand Down
4 changes: 0 additions & 4 deletions src/test/ui/nll/outlives-suggestion-simple.stderr
Expand Up @@ -17,8 +17,6 @@ LL | fn foo2<'a>(x: &'a usize) -> &'static usize {
| -- lifetime `'a` defined here
LL | x
| ^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: lifetime may not live long enough
--> $DIR/outlives-suggestion-simple.rs:14:5
Expand Down Expand Up @@ -66,8 +64,6 @@ LL | pub fn foo<'a>(x: &'a usize) -> Self {
| -- lifetime `'a` defined here
LL | Foo { x }
| ^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
|
= help: consider replacing `'a` with `'static`

error: lifetime may not live long enough
--> $DIR/outlives-suggestion-simple.rs:41:9
Expand Down

0 comments on commit fdd8a0d

Please sign in to comment.