Skip to content

Confusing message for inferred trait object lifetime #117600

Open
@pie-flavor

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-trait-objectsArea: trait objects, vtable layoutT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions