Skip to content

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

Open

Description

@jackh726

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>

Activity

  1. added
    C-bugCategory: This is a bug.
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    on Sep 27, 2024
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 27, 2024
  3. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 27, 2024
  4. compiler-errors commented on Sep 27, 2024

    @compiler-errors
    Contributor

    I thought I mentioned in the issue that this has nothing to do with AFIT or RPITIT? This can be observed on regular RPIT and async fn:

    async fn foo<'a, T>() -> &'a T { todo!() }
    //~^ ERROR the parameter type `T` may not live long enough
    
  5. added
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 27, 2024
  6. changed the title [-]Outlives requirements are not implied in AFIT and RPITIT[/-] [+]Outlives requirements are not implied in the return type of `async fn`, and generally for RPIT[/+] on Sep 27, 2024
  7. compiler-errors commented on Sep 27, 2024

    @compiler-errors
    Contributor

    I've edited the PR title to reflect its more general nature. This is because we don't walk into RPIT's projections to gather outlives constraints, i.e. we don't deduce any implied outlives bounds from impl Future<Output = &'a T>.

    I'm not certain this is fixable in general. Deducing outlives from the return type of a function is only really doable because the implementation of the body serves as the witness of the outlive's validity, but this is obviously not true for all associated types, just the ones that are "output"-like.

  8. added
    AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
    on Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

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