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

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

Open
jackh726 opened this issue Sep 27, 2024 · 2 comments
Labels
A-async-await Area: Async & Await A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-implied-bounds Area: Implied bounds / inferred outlives-bounds AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@jackh726
Copy link
Member

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>

@jackh726 jackh726 added C-bug Category: This is a bug. F-async_fn_in_trait Static async fn in traits F-return_position_impl_trait_in_trait `#![feature(return_position_impl_trait_in_trait)]` T-types Relevant to the types team, which will review and decide on the PR/issue. labels Sep 27, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 27, 2024
@jackh726 jackh726 removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 27, 2024
@compiler-errors
Copy link
Member

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

@compiler-errors compiler-errors added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-async-await Area: Async & Await needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. and removed F-async_fn_in_trait Static async fn in traits F-return_position_impl_trait_in_trait `#![feature(return_position_impl_trait_in_trait)]` needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 27, 2024
@compiler-errors compiler-errors 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 Sep 27, 2024
@compiler-errors
Copy link
Member

compiler-errors commented Sep 27, 2024

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.

@fmease fmease added the A-implied-bounds Area: Implied bounds / inferred outlives-bounds label Sep 27, 2024
@traviscross traviscross added the AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-implied-bounds Area: Implied bounds / inferred outlives-bounds AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants