Skip to content

Confusing diagnostic - error[E0308]: mismatched types #84400

Open
@elichai

Description

@elichai

https://play.rust-lang.org/?gist=d912ef0e107be493dd10b72f973897ba

use serde::*;

#[derive(Serialize)]
struct Test;

fn main() {
    serialize_wrap(serialize, &Test);
}

fn serialize_wrap<F: FnOnce(&Test)>(f: F, ser: &Test) {
    f(ser)
}
fn serialize<T: Serialize>(x: T) {
    todo!()
}

The current output is:

error[E0308]: mismatched types
  --> src/main.rs:7:5
   |
7  |     serialize_wrap(serialize, &Test);
   |     ^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected type `FnOnce<(&Test,)>`
              found type `FnOnce<(&Test,)>`
note: the lifetime requirement is introduced here
  --> src/main.rs:10:22
   |
10 | fn serialize_wrap<F: FnOnce(&Test)>(f: F, ser: &Test) {
   |                      ^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

I'm not quite sure what's the right diagnostic here, maybe that it should be by value?

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