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

[async block]: Future<Output = Ty> obligations don't provide inference guidance #106527

Open
compiler-errors opened this issue Jan 6, 2023 · 5 comments
Assignees
Labels
A-async-await Area: Async & Await A-inference Area: Type inference 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

@compiler-errors
Copy link
Member

compiler-errors commented Jan 6, 2023

I tried this code:

use std::future::Future;

trait Trait {}
impl Trait for () {}

fn foo(_: impl Future<Output = Box<dyn Trait>>) {}

fn main() {
    foo(async { Box::new(()) })
}

I expected to see the code compile successfully. Instead, it fails with:

error[[E0271]](https://doc.rust-lang.org/nightly/error-index.html#E0271): expected `[async block@src/main.rs:10:9: 10:31]` to be a future that resolves to `Box<(dyn Trait + 'static)>`, but it resolves to `Box<()>`
  --> src/main.rs:10:9
   |
10 |     foo(async { Box::new(()) })
   |     --- ^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Trait`, found `()`
   |     |
   |     required by a bound introduced by this call
   |
   = note: expected struct `Box<(dyn Trait + 'static)>`
              found struct `Box<()>`
note: required by a bound in `foo`
  --> src/main.rs:7:23
   |
7  | fn foo(_: impl Future<Output = Box<dyn Trait>>) {}
   |                       ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `foo`

... should this work? Similar code works for closures: playground

I've got an experimental branch with some changes underway that could make this work (they don't work currently, but for somewhat orthogonal reasons) -- but first, I'm curious whether @rust-lang/types thinks this should work. Not going to keep on wasting my time if not.

~~

Ironically, this function compiles:

async fn bar() -> Box<dyn Trait> {
    Box::new(())
}

... because we do use the expectation impl Future<Output = Box<dyn Trait>> in the desugared async function. This only happens for async functions though, and not for async blocks (or presumably async closures).

@compiler-errors compiler-errors added A-inference Area: Type inference C-bug Category: This is a bug. A-async-await Area: Async & Await T-types Relevant to the types team, which will review and decide on the PR/issue. labels Jan 6, 2023
@compiler-errors compiler-errors changed the title [async block]: Future<Output = Ty> expectations don't provide inference guidance [async block]: Future<Output = Ty> obligations don't provide inference guidance Jan 6, 2023
@eholk
Copy link
Contributor

eholk commented Jan 30, 2023

We discussed this in @rust-lang/wg-async triage today and we all agree that this code should work. We were honestly surprised to find out it doesn't.

@eholk eholk added the AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. label Jan 30, 2023
@compiler-errors
Copy link
Member Author

Ok, well I'll look into a fix... It might not be pretty

@rustbot claim

@compiler-errors
Copy link
Member Author

I have a PR to fix this, but it's blocked on #104826, since with identity_future in place, we don't actually get any useful up to the async generator. I'll hold off on putting it up for review until then.

@TheDan64
Copy link
Contributor

@compiler-errors is there any update on this now that identity_future is fixed?

@compiler-errors
Copy link
Member Author

@TheDan64: No, since this affects stable code, I've put this on pause until we have a good story about how to do closure signature inference in the new trait solver (-Znext-solver). See #109338 (review).

Is this affecting code that you would like to compile? If so, please file a bug and cc me.

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-inference Area: Type inference 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

Successfully merging a pull request may close this issue.

3 participants