Skip to content

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

Open
@jackh726

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>

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-implied-boundsArea: Implied bounds / inferred outlives-boundsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.T-typesRelevant to the types 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