rust-lang / rust-lang/rust

Outlives requirements are not implied in the return type of `async fn`, and generally for RPIT

Open
#130,935 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await A-impl-trait A-implied-bounds AsyncAwait-Triaged C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Split from #102682

The following doesn't compile:

trait MyTrait<T> {
    async fn foo(&self) -> &T;
}

it gives the following:

error[E0311]: the parameter type `T` may not live long enough
 --> <source>:2:5
  |
2 |     async fn foo(&self) -> &T;
  |     ^^^^^^^^^^^^^-^^^^^^^^^^^^
  |     |            |
  |     |            the parameter type `T` must be valid for the anonymous lifetime as defined here...
  |     ...so that the reference type `&T` does not outlive the data it points at
  |
help: consider adding an explicit lifetime bound
  |
2 |     async fn foo<'a>(&'a self) -> &'a T where T: 'a;
  |                 ++++  ++           ++   +++++++++++

of course, a normal function is fine. We just don't imply that T: '_ here because it lowers to impl Future<Output = &'_ T>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the async trait method example from the issue and comparing it with the equivalent normal function, which compiles. Investigate how async fn lowers to impl Future<Output = &'_ T> and how RPIT handles implied outlives requirements. Done means the example compiles without the explicit lifetime bound and has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.