Skip to content

"use of undeclared lifetime" error on async method has invalid suggestion (lifetime in parameter position) #75850

Closed
@joshtriplett

Description

@joshtriplett

Minimal reproducer:

struct Bug {}
impl Bug {
    async fn buggy(&self) -> &'somelifetime str {
        todo!()
    }
}

In both stable (rustc 1.45.2 (d3fb005a3 2020-07-31)) and nightly (rustc 1.47.0-nightly (663d2f5cd 2020-08-22)) I get this error:

error[E0261]: use of undeclared lifetime name `'somelifetime`
 --> src/lib.rs:3:31
  |
3 |     async fn buggy(&self) -> &'somelifetime str {
  |                               ^^^^^^^^^^^^^ undeclared lifetime
  |
help: consider introducing lifetime `'somelifetime` here
  |
2 | impl<'somelifetime> Bug {
  |     ^^^^^^^^^^^^^^^
help: consider introducing lifetime `'somelifetime` here
  |
3 |     async fn buggy('somelifetime, &self) -> &'somelifetime str {
  |                    ^^^^^^^^^^^^^^

If I make the method a top-level function, or if I drop the &self parameter to make it an associated function, the issue goes away and rustc correctly suggests buggy<'somelifetime>().

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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