Skip to content

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

Open
@compiler-errors

Description

@compiler-errors

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

Metadata

Metadata

Labels

A-async-awaitArea: Async & AwaitA-inferenceArea: Type inferenceAsyncAwait-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