Skip to content

Async callback argument causes nonsensical error message. #121093

Open
@theemathas

Description

@theemathas

Code

use std::future::Future;

pub fn run<R: Future<Output = ()>, F: FnMut(&i32) -> R>(mut callback: F) {
    let x = 1;
    callback(&x);
}

async fn foo(_x: &i32) {}

fn main() {
    run(foo);
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/main.rs:11:5
   |
11 |     run(foo);
   |     ^^^^^^^^ one type is more general than the other
   |
   = note: expected opaque type `impl for<'a> Future<Output = ()>`
              found opaque type `impl Future<Output = ()>`
   = help: consider `await`ing on both `Future`s
   = note: distinct uses of `impl Trait` result in different opaque types
note: the lifetime requirement is introduced here
  --> src/main.rs:3:54
   |
3  | pub fn run<R: Future<Output = ()>, F: FnMut(&i32) -> R>(mut callback: F) {
   |                                                      ^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

No response

Rationale and extra context

The error does not make any sense whatsoever.

  • The error talks about two opaque types, although only one opaque type is involved (the returned future of foo).
  • The error talks about awaiting on "both Futures" even though there are no futures to await on.
  • The error talks about impl Trait even though that syntax isn't used.
  • The error has for<'a> and then doesn't use 'a anywhere.

I am unable to figure out if this code is supposed to compile or not, but I suspect that expressing the type I want requires HKT, and this code shouldn't compile.

Other cases

No response

Rust Version

Using https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2bd7af033754103ac8bb25c56fba28c2

Rust stable channel, version 1.76.0

Anything else?

No response

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.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & awaitWG-diagnosticsWorking group: Diagnosticsfixed-by-async-closuresFixed by `#![feature(async_closure)]`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions