Skip to content

"one type is more general than the other" error that appears to expect and find the same type #90656

Open
@Arc-blroth

Description

@Arc-blroth

Compiling this program:

use futures::stream::StreamExt;
use futures::{FutureExt, SinkExt};

fn main() {
    tokio::runtime::Runtime::new().unwrap().spawn(do_stuff());
}

async fn do_stuff() {
    let (mut tx, rx) = futures::channel::mpsc::channel::<u32>(10);
    tokio::spawn(async move {
        tx.send(1).await.unwrap();
    });
    rx.filter_map(|x| {
        println!("received a {}", x);
        std::future::ready(Some(Ok(x))).boxed()
    })
    .forward(futures::sink::drain())
    .await;
}

results in the confusing compiler error:

error[E0308]: mismatched types
 --> src/main.rs:5:45
  |
5 |     tokio::runtime::Runtime::new().unwrap().spawn(do_stuff());
  |                                             ^^^^^ one type is more general than the other
  |
  = note: expected struct `Pin<Box<dyn futures::Future<Output = Option<Result<u32, Infallible>>> + std::marker::Send>>`
             found struct `Pin<Box<dyn futures::Future<Output = Option<Result<u32, Infallible>>> + std::marker::Send>>`

I expected this program to compile and print received a 1 to stdout.

Doing any of the following will make the program pass compilation (maybe due to how type inference changes?):

  • changing .spawn() to .block_on() in the main function
  • removing the .boxed() call

Meta

rustc --version --verbose:

rustc 1.57.0-nightly (e1e9319d9 2021-10-14)
binary: rustc
commit-hash: e1e9319d93aea755c444c8f8ff863b0936d7a4b6
commit-date: 2021-10-14
host: x86_64-pc-windows-msvc
release: 1.57.0-nightly
LLVM version: 13.0.0

This error is also reproducible on the latest stable and latest nightly on the playground.

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=cd86e8ecc0289ad192a465214eae3f76

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsD-confusingDiagnostics: Confusing error or lint that should be reworked.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions