rust
rust copied to clipboard
`[async block]: Future<Output = Ty>` obligations don't provide inference guidance
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).
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.
Ok, well I'll look into a fix... It might not be pretty
@rustbot claim
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.
@compiler-errors is there any update on this now that identity_future is fixed?
@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 https://github.com/rust-lang/rust/pull/109338#pullrequestreview-1352523244.
Is this affecting code that you would like to compile? If so, please file a bug and cc me.