Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
joshtriplett opened this issue Aug 23, 2020 · 0 comments · Fixed by #75867
Labels
A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

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>().

@joshtriplett joshtriplett added C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Aug 23, 2020
@jonas-schievink jonas-schievink added A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 23, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 30, 2020
…jasper

Account for async functions when suggesting new named lifetime

Fix rust-lang#75850.
@bors bors closed this as completed in 35e166e Aug 30, 2020
Mark-Simulacrum pushed a commit to Mark-Simulacrum/rust that referenced this issue Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants