Skip to content

Unhelpful error "one type is more general than the other" in async code #64650

Open
@Seeker14491

Description

@Seeker14491

The following code does not compile:

// Crate versions:
// futures-preview v0.3.0-alpha.18
// runtime v0.3.0-alpha.7

use futures::{prelude::*, stream::FuturesUnordered};

#[runtime::main]
async fn main() {
    let entries: Vec<_> = vec![()]
        .into_iter()
        .map(|x| async { vec![()].into_iter().map(|_| x) })
        .collect::<FuturesUnordered<_>>()
        .map(stream::iter)
        .flatten()
        .collect()
        .await;
}

It produces this error message:

error[E0308]: mismatched types
 --> src\main.rs:3:1
  |
3 | #[runtime::main]
  | ^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected type `std::ops::FnOnce<(std::iter::Map<std::vec::IntoIter<()>, [closure@src\main.rs:7:51: 7:56 x:&()]>,)>`
             found type `std::ops::FnOnce<(std::iter::Map<std::vec::IntoIter<()>, [closure@src\main.rs:7:51: 7:56 x:&()]>,)>`

In this case, the code can be made to compile by adding move in two places on this line:

// ...
        .map(|x| async move { vec![()].into_iter().map(move |_| x) })
// ...

Some possibly related issues: #57362, #60658

rustc version: 1.39.0-nightly (97e58c0d3 2019-09-20)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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