Skip to content

Incomprehensible error message involving lifetimes adding extension methods to dyn Trait #71341

Closed
@rocallahan

Description

@rocallahan

I'm defining a trait that adds extension methods to dyn <another-trait>:

impl GeneralControlCursorRendering for dyn GeneralControlCursor {
    fn render_frame_container_view<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
        Box::pin(async move {})
    }
}

Complete example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9918f3c4541601415f65606b4bfa0790

Rust stable and nightly both give

error: cannot infer an appropriate lifetime
  --> src/main.rs:25:16
   |
25 |         cursor.render_frame_container_view().await;
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                |
   |                this return type evaluates to the `'static` lifetime...
   |                ...but this borrow...
   |
note: ...can't outlive the lifetime `'a` as defined on the function body at 21:17
  --> src/main.rs:21:17
   |
21 | fn report_frame<'a>(
   |                 ^^
help: you can add a bound to the return type to make it last less than `'static` and match the lifetime `'a` as defined on the function body at 21:17
   |
23 | ) -> impl Future<Output = ()> + Send + 'a + 'a {
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is confusing. Obviously, adding a lifetime parameter identical to one already present doesn't seem sensible. Furthermore there is no clue here indicating what the error actually is. I spent over an hour thrashing around and ended up with https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fda63d48e1652dcfd0923002f723163c --- i.e. I need to add a lifetime bound to the dyn type I'm extending, not just the method(s) on the extension.

I'm assuming that the compiler is correct here and it just needs a better error message. I'm not 100% sure about that though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-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