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

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

Closed
ibaryshnikov opened this issue Jun 20, 2019 · 2 comments

Comments

Projects
None yet
5 participants
@ibaryshnikov
Copy link

commented Jun 20, 2019

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

@Nemo157

This comment has been minimized.

Copy link
Contributor

commented Jun 20, 2019

For context join_all has the signature:

pub fn join_all<I>(i: I) -> JoinAll<<I as IntoIterator>::Item> where
    I: IntoIterator,
    <I as IntoIterator>::Item: Future, 

so doesn't provide enough constraints to allow inferring a type for collect. It seems bad that the inference failure is pointing to the join_all(...).await expression instead of the futures_to_await variable/collect call where the initial failure is.

@cramertj

This comment has been minimized.

Copy link
Member

commented Jun 24, 2019

This isn't a language issue, nor an inference bug-- there's simply not enough information here, similar to if a call to collect occurred without specifying Vec.

@cramertj cramertj closed this Jun 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.