Skip to content

Can't infer type after collecting an iterator of futures #61991

@ibaryshnikov

Description

@ibaryshnikov

This code fails to compile:

#![feature(async_await)]

use futures::future::join_all;

async fn do_work(n: u32) -> u32 {
    n + 1
}

#[runtime::main]
async fn main() {
    let data = vec![1, 2, 3];
    let futures_to_wait = data
        .iter()
        .map(|value: &u32| do_work(*value))
        .collect();
    let results = join_all(futures_to_wait).await;
    println!("all work is done, results are {:?}", results);
}

Error message:

error[E0698]: type inside `async` object must be known in this context
  --> src/main.rs:16:19
   |
16 |     let results = join_all(futures_to_wait).await;
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
   |
note: the type is part of the `async` object because of this `await`
  --> src/main.rs:16:19
   |
16 |     let results = join_all(futures_to_wait).await;
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0698`.

More details:

rustc --version
rustc 1.37.0-nightly (2fe7b3383 2019-06-19)
edition = "2018"

[dependencies]
runtime = "0.3.0-alpha.5"
futures-preview = "0.3.0-alpha.16"

Annotating variable type fixes the error: let futures_to_wait: Vec<_> = ...

@Nemo157 kindly created a playground example with similar error (though a bit different, mentioning generators instead of async objects: type inside generator must be known in this context)
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=deaf09047f91d098705bcf90f894fd72

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-compilerRelevant to the compiler 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