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
Closed

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

ibaryshnikov opened this issue Jun 20, 2019 · 2 comments
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ibaryshnikov
Copy link

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

@jonas-schievink jonas-schievink added A-async-await Area: Async & Await A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 20, 2019
@Nemo157
Copy link
Member

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

@Centril Centril added A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Unclear labels Jun 20, 2019
@cramertj
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants