[async block]: Future<Output = Ty> obligations don't provide inference guidance · Issue #106527 · rust-lang/rust (original) (raw)

I tried this code:

use std::future::Future;

trait Trait {} impl Trait for () {}

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

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 { 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).