Rollup merge of #73783 - estebank:impl-dyn-trait-static-lifetime, r=nikomatsakis

Detect when `'static` obligation might come from an `impl`

Partly address #71341.
This commit is contained in:
Manish Goregaokar 2020-07-22 16:34:36 -07:00 committed by GitHub
commit fe9babbaed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
60 changed files with 1308 additions and 446 deletions

View file

@ -35,7 +35,7 @@ fn baz<'a,'b>(x: &'a u32, y: &'b u32) -> (&'a u32, &'b u32) {
#[cfg(transmute)] // one instantiations: BAD
fn baz<'a,'b>(x: &'a u32) -> &'static u32 {
bar(foo, x) //[transmute]~ ERROR E0495
bar(foo, x) //[transmute]~ ERROR E0759
}
#[cfg(krisskross)] // two instantiations, mixing and matching: BAD

View file

@ -1,26 +1,11 @@
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/project-fn-ret-contravariant.rs:38:8
|
LL | bar(foo, x)
| ^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 37:8...
--> $DIR/project-fn-ret-contravariant.rs:37:8
|
LL | fn baz<'a,'b>(x: &'a u32) -> &'static u32 {
| ^^
note: ...so that reference does not outlive borrowed content
--> $DIR/project-fn-ret-contravariant.rs:38:13
|
| ------- this data with lifetime `'a`...
LL | bar(foo, x)
| ^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that reference does not outlive borrowed content
--> $DIR/project-fn-ret-contravariant.rs:38:4
|
LL | bar(foo, x)
| ^^^^^^^^^^^
| ----^^^---- ...is captured and required to live as long as `'static` here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0759`.

View file

@ -46,7 +46,7 @@ fn baz<'a, 'b>(x: Type<'a>) -> Type<'static> {
// Cannot instantiate `foo` with any lifetime other than `'a`,
// since it is provided as input.
bar(foo, x) //[transmute]~ ERROR E0495
bar(foo, x) //[transmute]~ ERROR E0759
}
#[cfg(krisskross)] // two instantiations, mixing and matching: BAD

View file

@ -1,30 +1,12 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/project-fn-ret-invariant.rs:49:9
|
LL | bar(foo, x)
| ^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 45:8...
--> $DIR/project-fn-ret-invariant.rs:45:8
|
LL | fn baz<'a, 'b>(x: Type<'a>) -> Type<'static> {
| ^^
note: ...so that the expression is assignable
--> $DIR/project-fn-ret-invariant.rs:49:14
|
| -------- this data with lifetime `'a`...
...
LL | bar(foo, x)
| ^
= note: expected `Type<'_>`
found `Type<'a>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
--> $DIR/project-fn-ret-invariant.rs:49:5
|
LL | bar(foo, x)
| ^^^^^^^^^^^
= note: expected `Type<'static>`
found `Type<'_>`
| ----^^^---- ...is captured and required to live as long as `'static` here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0759`.

View file

@ -9,7 +9,7 @@ where
struct Struct;
impl Struct {
pub async fn run_dummy_fn(&self) { //~ ERROR cannot infer
pub async fn run_dummy_fn(&self) { //~ ERROR E0759
foo(|| self.bar()).await;
}

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/issue-62097.rs:12:31
|
LL | pub async fn run_dummy_fn(&self) {

View file

@ -17,7 +17,7 @@ fn static_val<T: StaticTrait>(_: T) {
}
fn with_dyn_debug_static<'a>(x: Box<dyn Debug + 'a>) {
static_val(x); //~ ERROR cannot infer
static_val(x); //~ ERROR E0759
}
fn not_static_val<T: NotStaticTrait>(_: T) {

View file

@ -1,30 +1,17 @@
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/dyn-trait.rs:20:16
|
LL | static_val(x);
| ^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 19:26...
--> $DIR/dyn-trait.rs:19:26
|
LL | fn with_dyn_debug_static<'a>(x: Box<dyn Debug + 'a>) {
| ^^
note: ...so that the expression is assignable
--> $DIR/dyn-trait.rs:20:16
|
| ------------------- this data with lifetime `'a`...
LL | static_val(x);
| ^
= note: expected `std::boxed::Box<dyn std::fmt::Debug>`
found `std::boxed::Box<(dyn std::fmt::Debug + 'a)>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
| ^ ...is captured here...
|
note: ...and is required to live as long as `'static` here
--> $DIR/dyn-trait.rs:20:5
|
LL | static_val(x);
| ^^^^^^^^^^
= note: expected `StaticTrait`
found `StaticTrait`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0759`.

View file

@ -12,7 +12,7 @@ LL | fn elided(x: &i32) -> impl Copy + '_ { x }
| ^^^^^^^^^^^^^^
error: lifetime may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:6:32
--> $DIR/must_outlive_least_region_or_bound.rs:5:32
|
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
| -- ^^^^^^^^^ opaque type requires that `'a` must outlive `'static`
@ -26,7 +26,7 @@ LL | fn explicit<'a>(x: &'a i32) -> impl Copy + 'a { x }
| ^^^^^^^^^^^^^^
error: lifetime may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:9:46
--> $DIR/must_outlive_least_region_or_bound.rs:7:46
|
LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
| - ^ returning this value requires that `'1` must outlive `'static`
@ -36,7 +36,7 @@ LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
= help: consider replacing `'1` with `'static`
error: lifetime may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:12:55
--> $DIR/must_outlive_least_region_or_bound.rs:9:55
|
LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
| -- lifetime `'a` defined here ^ returning this value requires that `'a` must outlive `'static`
@ -45,7 +45,7 @@ LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
= help: consider replacing `'a` with `'static`
error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/must_outlive_least_region_or_bound.rs:15:41
--> $DIR/must_outlive_least_region_or_bound.rs:11:41
|
LL | fn foo<'a>(x: &i32) -> impl Copy + 'a { x }
| ---- ^ lifetime `'a` required
@ -53,7 +53,7 @@ LL | fn foo<'a>(x: &i32) -> impl Copy + 'a { x }
| help: add explicit lifetime `'a` to the type of `x`: `&'a i32`
error: lifetime may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:30:24
--> $DIR/must_outlive_least_region_or_bound.rs:22:24
|
LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
| - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ opaque type requires that `'1` must outlive `'static`
@ -61,7 +61,7 @@ LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
| let's call the lifetime of this reference `'1`
error: lifetime may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:37:69
--> $DIR/must_outlive_least_region_or_bound.rs:28:69
|
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`
@ -70,7 +70,7 @@ LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
= help: consider replacing `'a` with `'static`
error: lifetime may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:42:61
--> $DIR/must_outlive_least_region_or_bound.rs:32:61
|
LL | fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) {
| -- -- lifetime `'b` defined here ^^^^^^^^^^^^^^^^ opaque type requires that `'b` must outlive `'a`
@ -80,7 +80,7 @@ LL | fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32
= help: consider adding the following bound: `'b: 'a`
error[E0310]: the parameter type `T` may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:47:51
--> $DIR/must_outlive_least_region_or_bound.rs:37:51
|
LL | fn ty_param_wont_outlive_static<T:Debug>(x: T) -> impl Debug + 'static {
| ^^^^^^^^^^^^^^^^^^^^

View file

@ -1,41 +1,31 @@
use std::fmt::Debug;
fn elided(x: &i32) -> impl Copy { x }
//~^ ERROR cannot infer an appropriate lifetime
fn elided(x: &i32) -> impl Copy { x } //~ ERROR E0759
fn explicit<'a>(x: &'a i32) -> impl Copy { x }
//~^ ERROR cannot infer an appropriate lifetime
fn explicit<'a>(x: &'a i32) -> impl Copy { x } //~ ERROR E0759
fn elided2(x: &i32) -> impl Copy + 'static { x }
//~^ ERROR cannot infer an appropriate lifetime
fn elided2(x: &i32) -> impl Copy + 'static { x } //~ ERROR E0759
fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
//~^ ERROR cannot infer an appropriate lifetime
fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x } //~ ERROR E0759
fn foo<'a>(x: &i32) -> impl Copy + 'a { x }
//~^ ERROR explicit lifetime required in the type of `x`
fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
//~^ ERROR cannot infer an appropriate lifetime
fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) } //~ ERROR E0759
fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }
//~^ ERROR cannot infer an appropriate lifetime
fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) } //~ ERROR E0759
fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }
//~^ ERROR cannot infer an appropriate lifetime
fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) } //~ ERROR E0759
fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }
//~^ ERROR cannot infer an appropriate lifetime
fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) } //~ ERROR E0759
fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
//~^ ERROR cannot infer an appropriate lifetime
//~| ERROR cannot infer an appropriate lifetime
fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) } //~ ERROR E0759
//~^ ERROR E0759
trait LifetimeTrait<'a> {}
impl<'a> LifetimeTrait<'a> for &'a i32 {}
fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
//~^ ERROR cannot infer an appropriate lifetime
fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x } //~ ERROR E0759
// Tests that a closure type containing 'b cannot be returned from a type where
// only 'a was expected.

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:3:35
|
LL | fn elided(x: &i32) -> impl Copy { x }
@ -16,8 +16,8 @@ help: to declare that the `impl Trait` captures data from argument `x`, you can
LL | fn elided(x: &i32) -> impl Copy + '_ { x }
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:6:44
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:5:44
|
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
| ------- ^ ...is captured here...
@ -25,7 +25,7 @@ LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
| this data with lifetime `'a`...
|
note: ...and is required to live as long as `'static` here
--> $DIR/must_outlive_least_region_or_bound.rs:6:32
--> $DIR/must_outlive_least_region_or_bound.rs:5:32
|
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
| ^^^^^^^^^
@ -34,8 +34,8 @@ help: to declare that the `impl Trait` captures data from argument `x`, you can
LL | fn explicit<'a>(x: &'a i32) -> impl Copy + 'a { x }
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:9:46
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:7:46
|
LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
| ---- ^ ...is captured here...
@ -43,7 +43,7 @@ LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
| this data with an anonymous lifetime `'_`...
|
note: ...and is required to live as long as `'static` here
--> $DIR/must_outlive_least_region_or_bound.rs:9:24
--> $DIR/must_outlive_least_region_or_bound.rs:7:24
|
LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
| ^^^^^^^^^^^^^^^^^^^
@ -56,8 +56,8 @@ help: alternatively, add an explicit `'static` bound to this reference
LL | fn elided2(x: &'static i32) -> impl Copy + 'static { x }
| ^^^^^^^^^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:12:55
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:9:55
|
LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
| ------- ^ ...is captured here...
@ -65,7 +65,7 @@ LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
| this data with lifetime `'a`...
|
note: ...and is required to live as long as `'static` here
--> $DIR/must_outlive_least_region_or_bound.rs:12:33
--> $DIR/must_outlive_least_region_or_bound.rs:9:33
|
LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
| ^^^^^^^^^^^^^^^^^^^
@ -79,15 +79,15 @@ LL | fn explicit2<'a>(x: &'static i32) -> impl Copy + 'static { x }
| ^^^^^^^^^^^^
error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/must_outlive_least_region_or_bound.rs:15:24
--> $DIR/must_outlive_least_region_or_bound.rs:11:24
|
LL | fn foo<'a>(x: &i32) -> impl Copy + 'a { x }
| ---- ^^^^^^^^^^^^^^ lifetime `'a` required
| |
| help: add explicit lifetime `'a` to the type of `x`: `&'a i32`
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:30:65
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:22:65
|
LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
| ---- this data with an anonymous lifetime `'_`... ^ ...is captured here, requiring it to live as long as `'static`
@ -101,14 +101,14 @@ help: to declare that the `impl Trait` captures data from argument `x`, you can
LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug + '_) { (Box::new(x), x) }
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:30:69
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:22:69
|
LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
| ---- this data with an anonymous lifetime `'_`... ^ ...is captured here...
|
note: ...and is required to live as long as `'static` here
--> $DIR/must_outlive_least_region_or_bound.rs:30:41
--> $DIR/must_outlive_least_region_or_bound.rs:22:41
|
LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
| ^^^^^^^^^^
@ -121,14 +121,14 @@ help: to declare that the `impl Trait` captures data from argument `x`, you can
LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug + '_) { (Box::new(x), x) }
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:37:69
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:28:69
|
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
| ------- this data with lifetime `'a`... ^ ...is captured here...
|
note: ...and is required to live as long as `'static` here
--> $DIR/must_outlive_least_region_or_bound.rs:37:34
--> $DIR/must_outlive_least_region_or_bound.rs:28:34
|
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -142,7 +142,7 @@ LL | fn with_bound<'a>(x: &'static i32) -> impl LifetimeTrait<'a> + 'static { x
| ^^^^^^^^^^^^
error[E0623]: lifetime mismatch
--> $DIR/must_outlive_least_region_or_bound.rs:42:61
--> $DIR/must_outlive_least_region_or_bound.rs:32:61
|
LL | fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) {
| ------- ^^^^^^^^^^^^^^^^
@ -151,15 +151,15 @@ LL | fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32
| this parameter and the return type are declared with different lifetimes...
error[E0310]: the parameter type `T` may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:47:51
--> $DIR/must_outlive_least_region_or_bound.rs:37:51
|
LL | fn ty_param_wont_outlive_static<T:Debug>(x: T) -> impl Debug + 'static {
| -- ^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| |
| help: consider adding an explicit lifetime bound...: `T: 'static +`
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:18:50
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:14:50
|
LL | fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
| ---- ^ ...is captured here, requiring it to live as long as `'static`
@ -171,8 +171,8 @@ help: to declare that the trait object captures data from argument `x`, you can
LL | fn elided3(x: &i32) -> Box<dyn Debug + '_> { Box::new(x) }
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:21:59
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:16:59
|
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }
| ------- ^ ...is captured here, requiring it to live as long as `'static`
@ -184,8 +184,8 @@ help: to declare that the trait object captures data from argument `x`, you can
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug + 'a> { Box::new(x) }
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:24:60
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:18:60
|
LL | fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }
| ---- ^ ...is captured here, requiring it to live as long as `'static`
@ -201,8 +201,8 @@ help: alternatively, add an explicit `'static` bound to this reference
LL | fn elided4(x: &'static i32) -> Box<dyn Debug + 'static> { Box::new(x) }
| ^^^^^^^^^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:27:69
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/must_outlive_least_region_or_bound.rs:20:69
|
LL | fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }
| ------- this data with lifetime `'a`... ^ ...is captured here, requiring it to live as long as `'static`

View file

@ -12,7 +12,7 @@ LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: lifetime may not live long enough
--> $DIR/static-return-lifetime-infered.rs:10:37
--> $DIR/static-return-lifetime-infered.rs:9:37
|
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
| -- ^^^^^^^^^^^^^^^^^^^^^^^ opaque type requires that `'a` must outlive `'static`

View file

@ -4,13 +4,11 @@ struct A {
impl A {
fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
self.x.iter().map(|a| a.0)
self.x.iter().map(|a| a.0) //~ ERROR E0759
}
//~^^ ERROR cannot infer an appropriate lifetime
fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
self.x.iter().map(|a| a.0)
self.x.iter().map(|a| a.0) //~ ERROR E0759
}
//~^^ ERROR cannot infer an appropriate lifetime
}
fn main() {}

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/static-return-lifetime-infered.rs:7:16
|
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
@ -18,8 +18,8 @@ help: to declare that the `impl Trait` captures data from argument `self`, you c
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/static-return-lifetime-infered.rs:11:16
error[E0759]: `self` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/static-return-lifetime-infered.rs:10:16
|
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
| -------- this data with lifetime `'a`...
@ -29,7 +29,7 @@ LL | self.x.iter().map(|a| a.0)
| ...is captured here...
|
note: ...and is required to live as long as `'static` here
--> $DIR/static-return-lifetime-infered.rs:10:37
--> $DIR/static-return-lifetime-infered.rs:9:37
|
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
| ^^^^^^^^^^^^^^^^^^^^^^^

View file

@ -1,8 +1,7 @@
use std::any::Any;
fn foo<T: Any>(value: &T) -> Box<dyn Any> {
Box::new(value) as Box<dyn Any>
//~^ ERROR cannot infer an appropriate lifetime
Box::new(value) as Box<dyn Any> //~ ERROR E0759
}
fn main() {

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `value` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/issue-16922.rs:4:14
|
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> {

View file

@ -1,28 +1,11 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
error[E0759]: `fn` parameter has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/constant-in-expr-inherent-1.rs:8:5
|
LL | <Foo<'a>>::C
| ^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 7:8...
--> $DIR/constant-in-expr-inherent-1.rs:7:8
|
LL | fn foo<'a>(_: &'a u32) -> &'static u32 {
| ^^
note: ...so that the types are compatible
--> $DIR/constant-in-expr-inherent-1.rs:8:5
|
| ------- this data with lifetime `'a`...
LL | <Foo<'a>>::C
| ^^^^^^^^^^^^
= note: expected `Foo<'_>`
found `Foo<'a>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that reference does not outlive borrowed content
--> $DIR/constant-in-expr-inherent-1.rs:8:5
|
LL | <Foo<'a>>::C
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^ ...is captured and required to live as long as `'static` here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0759`.

View file

@ -15,7 +15,7 @@ fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait> {
// `Box<SomeTrait>` defaults to a `'static` bound, so this return
// is illegal.
ss.r //~ ERROR cannot infer an appropriate lifetime
ss.r //~ ERROR E0759
}
fn store(ss: &mut SomeStruct, b: Box<dyn SomeTrait>) {

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `ss` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/object-lifetime-default-from-box-error.rs:18:5
|
LL | fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait> {

View file

@ -5,18 +5,18 @@ trait Foo {}
impl<'a> Foo for &'a [u8] {}
fn a(v: &[u8]) -> Box<dyn Foo + 'static> {
let x: Box<dyn Foo + 'static> = Box::new(v); //~ ERROR cannot infer an appropriate lifetime
let x: Box<dyn Foo + 'static> = Box::new(v); //~ ERROR E0759
x
}
fn b(v: &[u8]) -> Box<dyn Foo + 'static> {
Box::new(v) //~ ERROR cannot infer an appropriate lifetime
Box::new(v) //~ ERROR E0759
}
fn c(v: &[u8]) -> Box<dyn Foo> {
// same as previous case due to RFC 599
Box::new(v) //~ ERROR cannot infer an appropriate lifetime
Box::new(v) //~ ERROR E0759
}
fn d<'a,'b>(v: &'a [u8]) -> Box<dyn Foo+'b> {

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `v` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/region-object-lifetime-in-coercion.rs:8:46
|
LL | fn a(v: &[u8]) -> Box<dyn Foo + 'static> {
@ -15,7 +15,7 @@ help: alternatively, add an explicit `'static` bound to this reference
LL | fn a(v: &'static [u8]) -> Box<dyn Foo + 'static> {
| ^^^^^^^^^^^^^
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `v` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/region-object-lifetime-in-coercion.rs:13:14
|
LL | fn b(v: &[u8]) -> Box<dyn Foo + 'static> {
@ -32,7 +32,7 @@ help: alternatively, add an explicit `'static` bound to this reference
LL | fn b(v: &'static [u8]) -> Box<dyn Foo + 'static> {
| ^^^^^^^^^^^^^
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `v` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/region-object-lifetime-in-coercion.rs:19:14
|
LL | fn c(v: &[u8]) -> Box<dyn Foo> {

View file

@ -4,7 +4,7 @@ struct Dog {
impl Dog {
pub fn chase_cat(&mut self) {
let p: &'static mut usize = &mut self.cats_chased; //~ ERROR cannot infer
let p: &'static mut usize = &mut self.cats_chased; //~ ERROR E0759
*p += 1;
}

View file

@ -1,29 +1,11 @@
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/regions-addr-of-self.rs:7:37
|
LL | pub fn chase_cat(&mut self) {
| --------- this data with an anonymous lifetime `'_`...
LL | let p: &'static mut usize = &mut self.cats_chased;
| ^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 6:5...
--> $DIR/regions-addr-of-self.rs:6:5
|
LL | / pub fn chase_cat(&mut self) {
LL | | let p: &'static mut usize = &mut self.cats_chased;
LL | | *p += 1;
LL | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> $DIR/regions-addr-of-self.rs:7:37
|
LL | let p: &'static mut usize = &mut self.cats_chased;
| ^^^^^^^^^^^^^^^^^^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that reference does not outlive borrowed content
--> $DIR/regions-addr-of-self.rs:7:37
|
LL | let p: &'static mut usize = &mut self.cats_chased;
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^ ...is captured and required to live as long as `'static` here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
For more information about this error, try `rustc --explain E0759`.

View file

@ -7,7 +7,7 @@ trait X { }
impl<'a, T> X for B<'a, T> {}
fn g<'a, T: 'static>(v: Box<dyn A<T> + 'a>) -> Box<dyn X + 'static> {
box B(&*v) as Box<dyn X> //~ ERROR cannot infer
box B(&*v) as Box<dyn X> //~ ERROR E0759
}
fn main() { }

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `v` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/regions-close-object-into-object-2.rs:10:11
|
LL | fn g<'a, T: 'static>(v: Box<dyn A<T> + 'a>) -> Box<dyn X + 'static> {

View file

@ -7,7 +7,7 @@ trait X { }
impl<'a, T> X for B<'a, T> {}
fn i<'a, T, U>(v: Box<dyn A<U>+'a>) -> Box<dyn X + 'static> {
box B(&*v) as Box<dyn X> //~ ERROR cannot infer
box B(&*v) as Box<dyn X> //~ ERROR E0759
}
fn main() {}

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `v` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/regions-close-object-into-object-4.rs:10:11
|
LL | fn i<'a, T, U>(v: Box<dyn A<U>+'a>) -> Box<dyn X + 'static> {

View file

@ -6,7 +6,7 @@ fn borrowed_proc<'a>(x: &'a isize) -> Box<dyn FnMut()->(isize) + 'a> {
fn static_proc(x: &isize) -> Box<dyn FnMut() -> (isize) + 'static> {
// This is illegal, because the region bound on `proc` is 'static.
Box::new(move || { *x }) //~ ERROR cannot infer an appropriate lifetime
Box::new(move || { *x }) //~ ERROR E0759
}
fn main() { }

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/regions-proc-bound-capture.rs:9:14
|
LL | fn static_proc(x: &isize) -> Box<dyn FnMut() -> (isize) + 'static> {

View file

@ -6,7 +6,7 @@ struct Foo;
impl Foo {
async fn f(self: Pin<&Self>) -> impl Clone { self }
//~^ ERROR cannot infer an appropriate lifetime
//~^ ERROR E0759
}
fn main() {

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:16
|
LL | async fn f(self: Pin<&Self>) -> impl Clone { self }

View file

@ -3,7 +3,7 @@ use std::pin::Pin;
struct Foo;
impl Foo {
fn f(self: Pin<&Self>) -> impl Clone { self } //~ ERROR cannot infer an appropriate lifetime
fn f(self: Pin<&Self>) -> impl Clone { self } //~ ERROR E0759
}
fn main() {

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:6:44
|
LL | fn f(self: Pin<&Self>) -> impl Clone { self }

View file

@ -0,0 +1,37 @@
error[E0597]: `val` does not live long enough
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-needing-more-suggestions.rs:21:9
|
LL | fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32>>) -> impl OtherTrait<'a> {
| -- lifetime `'a` defined here ------------------- opaque type requires that `val` is borrowed for `'a`
LL | val.use_self()
| ^^^ borrowed value does not live long enough
LL | }
| - `val` dropped here while still borrowed
|
help: you can add a bound to the opaque type to make it last less than `'static` and match `'a`
|
LL | fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32>>) -> impl OtherTrait<'a> + 'a {
| ^^^^
error[E0515]: cannot return value referencing function parameter `val`
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-needing-more-suggestions.rs:43:9
|
LL | val.use_self()
| ---^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `val` is borrowed here
error[E0515]: cannot return value referencing function parameter `val`
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-needing-more-suggestions.rs:109:9
|
LL | val.use_self()
| ---^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `val` is borrowed here
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0515, E0597.
For more information about an error, try `rustc --explain E0515`.

View file

@ -0,0 +1,113 @@
// FIXME: the following cases need to suggest more things to make users reach a working end state.
mod bav {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {
type Assoc: Bar;
}
trait MyTrait {
fn use_self(&self) -> &() { panic!() }
}
trait Bar {}
impl MyTrait for Box<dyn ObjectTrait<Assoc = i32>> {
fn use_self(&self) -> &() { panic!() }
}
impl Bar for i32 {}
fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32>>) -> impl OtherTrait<'a> {
val.use_self() //~ ERROR E0597
}
}
mod bap {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {
type Assoc: Bar;
}
trait MyTrait {
fn use_self(&self) -> &() { panic!() }
}
trait Bar {}
impl MyTrait for Box<dyn ObjectTrait<Assoc = i32>> {
fn use_self(&self) -> &() { panic!() }
}
impl Bar for i32 {}
fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32>>) -> impl OtherTrait<'a> + 'a {
val.use_self() //~ ERROR E0515
}
}
// This case in particular requires the user to write all of the bounds we have in `mod bax`.
mod bay {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {
type Assoc: Bar;
}
trait MyTrait {
fn use_self(&self) -> &() { panic!() }
}
trait Bar {}
impl MyTrait for Box<dyn ObjectTrait<Assoc = i32>> {
fn use_self(&self) -> &() { panic!() }
}
impl Bar for i32 {}
fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32> + 'a>) -> &'a () {
val.use_self() //~ ERROR E0772
}
}
mod bax {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {
type Assoc: Bar;
}
trait MyTrait<'a> {
fn use_self(&'a self) -> &'a () { panic!() }
}
trait Bar {}
impl<'a> MyTrait<'a> for Box<dyn ObjectTrait<Assoc = i32> + 'a> {
fn use_self(&'a self) -> &'a () { panic!() }
}
impl Bar for i32 {}
fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32> + 'a>) -> &'a () {
val.use_self()
}
}
mod baw {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {
type Assoc: Bar;
}
trait MyTrait {
fn use_self(&self) -> &() { panic!() }
}
trait Bar {}
impl<'a> MyTrait for Box<dyn ObjectTrait<Assoc = Box<dyn Bar>>> {
fn use_self(&self) -> &() { panic!() }
}
fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = Box<dyn Bar>>>) -> impl OtherTrait<'a> + 'a{
val.use_self() //~ ERROR E0515
}
}
fn main() {}

View file

@ -0,0 +1,57 @@
error[E0597]: `val` does not live long enough
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-needing-more-suggestions.rs:21:9
|
LL | fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32>>) -> impl OtherTrait<'a> {
| -- lifetime `'a` defined here ------------------- opaque type requires that `val` is borrowed for `'a`
LL | val.use_self()
| ^^^ borrowed value does not live long enough
LL | }
| - `val` dropped here while still borrowed
|
help: you can add a bound to the opaque type to make it last less than `'static` and match `'a`
|
LL | fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32>>) -> impl OtherTrait<'a> + 'a {
| ^^^^
error[E0515]: cannot return value referencing function parameter `val`
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-needing-more-suggestions.rs:43:9
|
LL | val.use_self()
| ---^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `val` is borrowed here
error[E0515]: cannot return value referencing function parameter `val`
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-needing-more-suggestions.rs:109:9
|
LL | val.use_self()
| ---^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `val` is borrowed here
error[E0772]: `val` has lifetime `'a` but calling `use_self` introduces an implicit `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-needing-more-suggestions.rs:66:13
|
LL | fn use_it<'a>(val: Box<dyn ObjectTrait<Assoc = i32> + 'a>) -> &'a () {
| -------------------------------------- this data with lifetime `'a`...
LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here
|
note: the used `impl` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-needing-more-suggestions.rs:60:30
|
LL | impl MyTrait for Box<dyn ObjectTrait<Assoc = i32>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() }
| -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement
|
LL | impl MyTrait for Box<dyn ObjectTrait<Assoc = i32> + '_> {
| ^^^^
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0515, E0597.
For more information about an error, try `rustc --explain E0515`.

View file

@ -0,0 +1,112 @@
// run-rustfix
#![allow(dead_code)]
mod foo {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait<T> {}
trait MyTrait<T> {
fn use_self<K>(&self) -> &();
}
trait Irrelevant {}
impl<T> MyTrait<T> for dyn ObjectTrait<T> + '_ {
fn use_self<K>(&self) -> &() { panic!() }
}
impl<T> Irrelevant for dyn ObjectTrait<T> {}
fn use_it<'a, T>(val: &'a dyn ObjectTrait<T>) -> impl OtherTrait<'a> + 'a {
val.use_self::<T>() //~ ERROR E0759
}
}
mod bar {
trait ObjectTrait {}
trait MyTrait {
fn use_self(&self) -> &();
}
trait Irrelevant {}
impl MyTrait for dyn ObjectTrait + '_ {
fn use_self(&self) -> &() { panic!() }
}
impl Irrelevant for dyn ObjectTrait {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> &'a () {
val.use_self() //~ ERROR E0772
}
}
mod baz {
trait ObjectTrait {}
trait MyTrait {
fn use_self(&self) -> &();
}
trait Irrelevant {}
impl MyTrait for Box<dyn ObjectTrait + '_> {
fn use_self(&self) -> &() { panic!() }
}
impl Irrelevant for Box<dyn ObjectTrait> {}
fn use_it<'a>(val: &'a Box<dyn ObjectTrait + 'a>) -> &'a () {
val.use_self() //~ ERROR E0772
}
}
mod bat {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {}
impl dyn ObjectTrait + '_ {
fn use_self(&self) -> &() { panic!() }
}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
val.use_self() //~ ERROR E0772
}
}
mod ban {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {}
trait MyTrait {
fn use_self(&self) -> &() { panic!() }
}
trait Irrelevant {
fn use_self(&self) -> &() { panic!() }
}
impl MyTrait for dyn ObjectTrait + '_ {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
val.use_self() //~ ERROR E0759
}
}
mod bal {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {}
trait MyTrait {
fn use_self(&self) -> &() { panic!() }
}
trait Irrelevant {
fn use_self(&self) -> &() { panic!() }
}
impl MyTrait for dyn ObjectTrait + '_ {}
impl Irrelevant for dyn ObjectTrait {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
MyTrait::use_self(val) //~ ERROR E0759
}
}
fn main() {}

View file

@ -0,0 +1,42 @@
error[E0521]: borrowed data escapes outside of function
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:20:9
|
LL | fn use_it<'a, T>(val: &'a dyn ObjectTrait<T>) -> impl OtherTrait<'a> + 'a {
| --- `val` is a reference that is only valid in the function body
LL | val.use_self::<T>()
| ^^^^^^^^^^^^^^^^^^^ `val` escapes the function body here
|
= help: consider replacing `'a` with `'static`
error[E0521]: borrowed data escapes outside of function
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:69:9
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
| --- `val` is a reference that is only valid in the function body
LL | val.use_self()
| ^^^^^^^^^^^^^^ `val` escapes the function body here
|
= help: consider replacing `'a` with `'static`
error[E0521]: borrowed data escapes outside of function
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:88:9
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> {
| --- `val` is a reference that is only valid in the function body
LL | val.use_self()
| ^^^^^^^^^^^^^^ `val` escapes the function body here
|
= help: consider replacing `'a` with `'static`
error[E0521]: borrowed data escapes outside of function
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:108:9
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
| --- `val` is a reference that is only valid in the function body
LL | MyTrait::use_self(val)
| ^^^^^^^^^^^^^^^^^^^^^^ `val` escapes the function body here
|
= help: consider replacing `'a` with `'static`
error: aborting due to 4 previous errors

View file

@ -0,0 +1,112 @@
// run-rustfix
#![allow(dead_code)]
mod foo {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait<T> {}
trait MyTrait<T> {
fn use_self<K>(&self) -> &();
}
trait Irrelevant {}
impl<T> MyTrait<T> for dyn ObjectTrait<T> {
fn use_self<K>(&self) -> &() { panic!() }
}
impl<T> Irrelevant for dyn ObjectTrait<T> {}
fn use_it<'a, T>(val: &'a dyn ObjectTrait<T>) -> impl OtherTrait<'a> + 'a {
val.use_self::<T>() //~ ERROR E0759
}
}
mod bar {
trait ObjectTrait {}
trait MyTrait {
fn use_self(&self) -> &();
}
trait Irrelevant {}
impl MyTrait for dyn ObjectTrait {
fn use_self(&self) -> &() { panic!() }
}
impl Irrelevant for dyn ObjectTrait {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> &'a () {
val.use_self() //~ ERROR E0772
}
}
mod baz {
trait ObjectTrait {}
trait MyTrait {
fn use_self(&self) -> &();
}
trait Irrelevant {}
impl MyTrait for Box<dyn ObjectTrait> {
fn use_self(&self) -> &() { panic!() }
}
impl Irrelevant for Box<dyn ObjectTrait> {}
fn use_it<'a>(val: &'a Box<dyn ObjectTrait + 'a>) -> &'a () {
val.use_self() //~ ERROR E0772
}
}
mod bat {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {}
impl dyn ObjectTrait {
fn use_self(&self) -> &() { panic!() }
}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
val.use_self() //~ ERROR E0772
}
}
mod ban {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {}
trait MyTrait {
fn use_self(&self) -> &() { panic!() }
}
trait Irrelevant {
fn use_self(&self) -> &() { panic!() }
}
impl MyTrait for dyn ObjectTrait {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> {
val.use_self() //~ ERROR E0759
}
}
mod bal {
trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {}
trait MyTrait {
fn use_self(&self) -> &() { panic!() }
}
trait Irrelevant {
fn use_self(&self) -> &() { panic!() }
}
impl MyTrait for dyn ObjectTrait {}
impl Irrelevant for dyn ObjectTrait {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
MyTrait::use_self(val) //~ ERROR E0759
}
}
fn main() {}

View file

@ -0,0 +1,134 @@
error[E0759]: `val` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:20:13
|
LL | fn use_it<'a, T>(val: &'a dyn ObjectTrait<T>) -> impl OtherTrait<'a> + 'a {
| ---------------------- this data with lifetime `'a`...
LL | val.use_self::<T>()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here
|
note: the used `impl` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:14:32
|
LL | impl<T> MyTrait<T> for dyn ObjectTrait<T> {
| ^^^^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self<K>(&self) -> &() { panic!() }
| -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement
|
LL | impl<T> MyTrait<T> for dyn ObjectTrait<T> + '_ {
| ^^^^
error[E0772]: `val` has lifetime `'a` but calling `use_self` introduces an implicit `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:69:13
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
| ------------------- this data with lifetime `'a`...
LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here because of an implicit lifetime bound on the inherent `impl`
|
note: the used `impl` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:64:14
|
LL | impl dyn ObjectTrait {
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() }
| -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement
|
LL | impl dyn ObjectTrait + '_ {
| ^^^^
error[E0759]: `val` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:88:13
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> {
| ------------------- this data with lifetime `'a`...
LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here
|
note: the used `impl` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:85:26
|
LL | fn use_self(&self) -> &() { panic!() }
| -------- calling this method introduces the `impl`'s 'static` requirement
...
LL | impl MyTrait for dyn ObjectTrait {}
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
help: consider relaxing the implicit `'static` requirement
|
LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^^^
help: to declare that the `impl Trait` captures data from argument `val`, you can add an explicit `'a` lifetime bound
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
| ^^^^
error[E0759]: `val` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:108:27
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
| ------------------- this data with lifetime `'a`...
LL | MyTrait::use_self(val)
| ^^^ ...is captured here...
|
note: ...and is required to live as long as `'static` here
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:108:9
|
LL | MyTrait::use_self(val)
| ^^^^^^^^^^^^^^^^^
note: the used `impl` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:104:26
|
LL | fn use_self(&self) -> &() { panic!() }
| -------- calling this method introduces the `impl`'s 'static` requirement
...
LL | impl MyTrait for dyn ObjectTrait {}
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
help: consider relaxing the implicit `'static` requirement
|
LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^^^
error[E0772]: `val` has lifetime `'a` but calling `use_self` introduces an implicit `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:37:13
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> &'a () {
| ------------------- this data with lifetime `'a`...
LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here
|
note: the used `impl` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:31:26
|
LL | impl MyTrait for dyn ObjectTrait {
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() }
| -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement
|
LL | impl MyTrait for dyn ObjectTrait + '_ {
| ^^^^
error[E0772]: `val` has lifetime `'a` but calling `use_self` introduces an implicit `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:54:13
|
LL | fn use_it<'a>(val: &'a Box<dyn ObjectTrait + 'a>) -> &'a () {
| ----------------------------- this data with lifetime `'a`...
LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here
|
note: the used `impl` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:48:30
|
LL | impl MyTrait for Box<dyn ObjectTrait> {
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() }
| -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement
|
LL | impl MyTrait for Box<dyn ObjectTrait + '_> {
| ^^^^
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0759`.

View file

@ -16,7 +16,7 @@ fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
where
G: Get<T>
{
move || { //~ ERROR cannot infer an appropriate lifetime
move || { //~ ERROR `dest`
*dest = g.get();
}
}

View file

@ -6,7 +6,7 @@ LL | fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| |
| help: consider introducing lifetime `'a` here: `'a,`
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `dest` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/missing-lifetimes-in-signature.rs:19:5
|
LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()

View file

@ -27,7 +27,7 @@ impl Bar {
fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> {
Iter {
current: None,
remaining: self.0.iter(), //~ ERROR cannot infer an appropriate lifetime
remaining: self.0.iter(), //~ ERROR E0759
}
}
}
@ -38,7 +38,7 @@ impl Baz {
fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> + '_ {
Iter {
current: None,
remaining: self.0.iter(), //~ ERROR cannot infer an appropriate lifetime
remaining: self.0.iter(), //~ ERROR E0759
}
}
}
@ -49,7 +49,7 @@ impl Bat {
fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> + 'a {
Iter {
current: None,
remaining: self.0.iter(), //~ ERROR cannot infer an appropriate lifetime
remaining: self.0.iter(), //~ ERROR E0759
}
}
}
@ -60,7 +60,7 @@ impl Ban {
fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> {
Iter {
current: None,
remaining: self.0.iter(), //~ ERROR cannot infer an appropriate lifetime
remaining: self.0.iter(), //~ ERROR E0759
}
}
}

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/trait-object-nested-in-impl-trait.rs:30:31
|
LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> {
@ -23,7 +23,7 @@ help: to declare that the trait object captures data from argument `self`, you c
LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo + '_>> {
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/trait-object-nested-in-impl-trait.rs:41:31
|
LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> + '_ {
@ -44,7 +44,7 @@ help: to declare that the trait object captures data from argument `self`, you c
LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo + '_>> + '_ {
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `self` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/trait-object-nested-in-impl-trait.rs:52:31
|
LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> + 'a {
@ -65,7 +65,7 @@ help: to declare that the trait object captures data from argument `self`, you c
LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo + 'a>> + 'a {
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `self` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/trait-object-nested-in-impl-trait.rs:63:31
|
LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> {

View file

@ -5,7 +5,7 @@
fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
// ^^^^^^^^^^^^^^^^^^^^^ bound *here* defaults to `'static`
Box::new(items.iter()) //~ ERROR cannot infer an appropriate lifetime
Box::new(items.iter()) //~ ERROR E0759
}
fn b<T>(items: &[T]) -> Box<dyn Iterator<Item=&T> + '_> {

View file

@ -1,4 +1,4 @@
error[E0759]: cannot infer an appropriate lifetime
error[E0759]: `items` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/dyn-trait-underscore.rs:8:20
|
LL | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {