Skip to content

E0308 expected and found are identical #97010

Open

Description

@jendrikw

Given the following code: play

fn rec_clone<'a>(v: &'a str, rec: &dyn Fn(&'a str) -> String) -> String {
    todo!()
}

pub fn clone() -> impl Fn(&str) -> String {
    fix::<&str, String>(rec_clone)
}

// https://stackoverflow.com/a/42182841
fn fix<A, B>(func: fn(A, &dyn Fn(A) -> B) -> B) -> impl Fn(A) -> B {
    move |val: A| func(val, &fix(func))
}

The current output is:

error[E0308]: mismatched types
  --> src/lib.rs:5:19
   |
5  | pub fn clone() -> impl Fn(&str) -> String {
   |                   ^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
...
10 | fn fix<A, B>(func: fn(A, &dyn Fn(A) -> B) -> B) -> impl Fn(A) -> B {
   |                                                    ---------------
   |                                                    |
   |                                                    the expected opaque type
   |                                                    the found opaque type
   |
   = note: expected associated type `<impl Fn(&str)-> String as FnOnce<(&str,)>>::Output`
              found associated type `<impl Fn(&str)-> String as FnOnce<(&str,)>>::Output`

It's irritating that expected and found are the same. Also "one type is more general than the other" could give an explanation why one is more general (like, what parameter). And, I don't know how FnOnce::Output is relevant.

One more thing: When you delete the return type from the signature of clone, it says expected unit type `()` found opaque type `impl Fn(&Fix<FC>)-> Fix<FC>` , which when implemented, doesn't work.

Activity

  1. added
    A-diagnostics
    T-compiler
    on May 13, 2022
  2. compiler-errors commented on Jun 19, 2022

    @compiler-errors
    Contributor

    This error message has improved with NLL on nightly, it still says this message above, but it also gives a more detailed message about the FnOnce implementation not being general enough.

  3. estebank commented on Oct 12, 2026

    @estebank
    Contributor

    Current output:

    error[E0277]: expected an `Fn(&str)` closure, found `impl Fn(&str) -> String`
     --> src/lib.rs:5:19
      |
    5 | pub fn clone() -> impl Fn(&str) -> String {
      |                   ^^^^^^^^^^^^^^^^^^^^^^^ expected an `Fn(&str)` closure, found `impl Fn(&str) -> String`
      |
      = help: the trait `for<'a> Fn(&'a str)` is not implemented for `impl Fn(&str) -> String`
      = note: expected a closure with signature `for<'a> fn(&'a _)`
                 found a closure with signature `fn(&_)`
    

    We should point at fix as the "expected closure" (the message clearly doesn't account for the error happening on non-closures either).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions