Skip to content

Unhelpful error messages from borrow checker with async/await #67765

Closed

Description

This code:

fn main() {}

async fn func<'a>() -> Result<(), &'a str> {
    let s = String::new();

    let b = &s[..];

    Err(b)?;

    Ok(())
}

Returns this error:

error[E0597]: `s` does not live long enough
  --> src/main.rs:6:14
   |
6  |     let b = &s[..];
   |              ^ borrowed value does not live long enough
...
11 | }
   | - `s` dropped here while still borrowed

Which is very unhelpful; it should at least in some way point to the line with the ?. This happens on rustc 1.40.0 (73528e3 2019-12-16).

For reference, this is the equivalent non-async error message (much more helpful):

error[E0515]: cannot return value referencing local variable `s`
 --> src/main.rs:8:11
  |
6 |     let b = &s[..];
  |              - `s` is borrowed here
7 |
8 |     Err(b)?;
  |           ^ returns a value referencing data owned by the current function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsAsyncAwait-PolishAsync-await issues that are part of the "polish" areaAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

  • Status

    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions