Increase accuracy of lifetime bound on trait object impl suggestion

This commit is contained in:
Esteban Küber 2020-06-28 15:26:12 -07:00
parent 6513148c14
commit 4e08bab87d
18 changed files with 321 additions and 114 deletions

View file

@ -1,14 +0,0 @@
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 mismatched types
}
fn main() {}

View file

@ -1,18 +0,0 @@
error[E0308]: mismatched types
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-without-suggestion.rs:11:9
|
LL | val.use_self()
| ^^^^^^^^ lifetime mismatch
|
= note: expected reference `&(dyn ObjectTrait + 'static)`
found reference `&(dyn ObjectTrait + 'a)`
note: the lifetime `'a` as defined on the function body at 10:11...
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-without-suggestion.rs:10:11
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
| ^^
= note: ...does not necessarily outlive the static lifetime
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.

View file

@ -9,10 +9,12 @@ mod foo {
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) -> impl OtherTrait<'a> + 'a {
val.use_self() //~ ERROR cannot infer an appropriate lifetime
@ -24,14 +26,48 @@ mod bar {
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 cannot infer an appropriate lifetime
}
}
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 cannot infer an appropriate lifetime
}
}
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 cannot infer an appropriate lifetime
}
}
fn main() {}

View file

@ -0,0 +1,22 @@
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>(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: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: aborting due to 2 previous errors

View file

@ -9,10 +9,12 @@ mod foo {
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) -> impl OtherTrait<'a> + 'a {
val.use_self() //~ ERROR cannot infer an appropriate lifetime
@ -24,14 +26,48 @@ mod bar {
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 cannot infer an appropriate lifetime
}
}
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 cannot infer an appropriate lifetime
}
}
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 cannot infer an appropriate lifetime
}
}
fn main() {}

View file

@ -1,5 +1,5 @@
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:18:13
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:20:13
|
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
| ------------------- this data with lifetime `'a`...
@ -12,7 +12,20 @@ LL | impl MyTrait for dyn ObjectTrait + '_ {
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:33:13
--> $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
|
help: this `impl` introduces an implicit `'static` requirement, consider changing it
|
LL | impl dyn ObjectTrait + '_ {
| ^^^^
error[E0759]: cannot infer an appropriate lifetime
--> $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`...
@ -24,6 +37,19 @@ help: this `impl` introduces an implicit `'static` requirement, consider changin
LL | impl MyTrait for dyn ObjectTrait + '_ {
| ^^^^
error: aborting due to 2 previous errors
error[E0759]: cannot infer an appropriate lifetime
--> $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
|
help: this `impl` introduces an implicit `'static` requirement, consider changing it
|
LL | impl MyTrait for Box<dyn ObjectTrait + '_> {
| ^^^^
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0759`.