Confusing message for inferred trait object lifetime
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
trait Foo {}
trait Bar {}
impl Bar for dyn Foo {}
struct ByVal<T: ?Sized>(T);
fn a<'a, T: Bar + ?Sized>(baz: &mut ByVal<&'a T>, bar: &'a T) {}
fn b<'a>(baz: &mut ByVal<&'a dyn Foo>, bar: &'a dyn Foo) {
a(baz, bar);
}
fn i<'a, T: Bar + ?Sized>(baz: &ByVal<&'a T>, bar: &'a T) {}
fn j<'a>(baz: &ByVal<&'a dyn Foo>, bar: &'a dyn Foo) {
i(baz, bar);
}
struct ByRef<'a, T: ?Sized>(&'a T);
fn x<'a, T: Bar + ?Sized>(baz: &mut ByRef<'a, T>, bar: &'a T) {}
fn y<'a>(baz: &mut ByRef<'a, dyn Foo>, bar: &'a dyn Foo) {
x(baz, bar);
}
Current output
error[E0521]: borrowed data escapes outside of function
--> src/lib.rs:9:5
|
8 | fn b<'a>(baz: &mut ByVal<&'a dyn Foo>, bar: &'a dyn Foo) {
| -- --- `baz` is a reference that is only valid in the function body
| |
| lifetime `'a` defined here
9 | a(baz, bar);
| ^^^^^^^^^^^
| |
| `baz` escapes the function body here
| argument requires that `'a` must outlive `'static`
|
= note: requirement occurs because of a mutable reference to `ByVal<&dyn Foo>`
= note: mutable references are invariant over their type parameter
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
error[E0521]: borrowed data escapes outside of function
--> src/lib.rs:14:5
|
13 | fn j<'a>(baz: &ByVal<&'a dyn Foo>, bar: &'a dyn Foo) {
| -- --- `baz` is a reference that is only valid in the function body
| |
| lifetime `'a` defined here
14 | i(baz, bar);
| ^^^^^^^^^^^
| |
| `baz` escapes the function body here
| argument requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> src/lib.rs:16:12
|
15 | fn y<'a>(baz: &mut ByRef<'a, dyn Foo>, bar: &'a dyn Foo) {
| -- lifetime `'a` defined here
16 | x(baz, bar);
| ^^^ cast requires that `'a` must outlive `'static`
Desired output
error: lifetime may not live long enough
--> src/lib.rs
|
| fn b<'a>(baz: &mut ByVal<&'a dyn Foo>, bar: &'a dyn Foo) {
| ------- let's call the lifetime of this trait object '1
| a(baz, bar);
| ^^^ cast requires that `'1` must outlive `'static`
= note: `impl Bar for dyn Foo { ... }` implicitly means `impl Bar for dyn Foo + 'static { ... }`
= note: but `fn b<'a>(..., bar: &'a dyn Foo)` implicitly means `fn b<'a, '1>(..., bar: &'a (dyn Foo + '1))` due to lifetime elision
help: you might have meant to impl `Bar` for non-'static versions of `dyn Foo`
|
| impl<'a> Bar for dyn Foo + 'a {
| ++++ +++++
|
help: or you might have meant `b` to accept only 'static versions of `dyn Foo`
|
| fn b<'a>(baz: &mut ByVal<&'a dyn Foo>, bar: &'a (dyn Foo + 'static)) {
| +++++++++++
|
Rationale and extra context
As you can see in the error, not only are some of the error messages talking about the wrong thing or linking to irrelevant concepts, but the lifetime they highlight is not the lifetime at issue.
Other cases
No response
Anything else?
I didn't even realize the trait object lifetime could be elided, and I consider myself a Rust veteran.
The original code involved HashMap, and attempting to impl Hash + Eq for a trait object so a ref to it could be used as a map key.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Use the Rust reproducer in src/lib.rs and compile the three calls in functions b, j, and y to inspect the current diagnostics. Trace the compiler diagnostic entry point for inferred trait-object lifetimes; done means the reported lifetime and explanation match the desired output, including relevant notes and suggestions for all three cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100