Skip to content

Rust complains about mismatched types, but the types are identical #89923

Open
@jimblandy

Description

@jimblandy

When I compile the following code:

use std::future::Future;

pub async fn async_magic<F, R>(f: F) -> Vec<u8>
where
    F: FnOnce(&mut Vec<u8>) -> R,
    R: Future,
{
    todo!()
}

async fn push_zero(v: &mut Vec<u8>) {
    todo!()
}

pub async fn test_async_magic() {
    async_magic(push_zero).await;
}

async fn sleep() {}

I get the error message:

error[E0308]: mismatched types
  --> src/lib.rs:16:5
   |
16 |     async_magic(push_zero).await;
   |     ^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected associated type `<for<'_> fn(&mut Vec<u8>) -> impl for<'_> Future {push_zero} as FnOnce<(&mut Vec<u8>,)>>::Output`
              found associated type `<for<'_> fn(&mut Vec<u8>) -> impl for<'_> Future {push_zero} as FnOnce<(&mut Vec<u8>,)>>::Output`
   = note: the required lifetime does not necessarily outlive the empty lifetime
note: the lifetime requirement is introduced here
  --> src/lib.rs:5:32
   |
5  |     F: FnOnce(&mut Vec<u8>) -> R,
   |                                ^

I'm sure the 'expected' and 'found' types are indeed different somehow, but there's nothing in the output that would allow us to distinguish them.

EDIT: Output from rustc -vV:

binary: rustc
commit-hash: c8dfcfe046a7680554bf4eb612bad840e7631c4b
commit-date: 2021-09-06
host: x86_64-unknown-linux-gnu
release: 1.55.0
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.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