Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing error message about mismatched but equal types #80915

Closed
FabianWolff opened this issue Jan 11, 2021 · 2 comments
Closed

Confusing error message about mismatched but equal types #80915

FabianWolff opened this issue Jan 11, 2021 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@FabianWolff
Copy link
Contributor

Consider this code:

fn foo<T> (f: impl Fn (&T) -> ()) {}

fn main () {
    let cl = |c| -> () { c % 2; };
    foo (cl);
}

This produces the following error message:

error[E0308]: mismatched types
 --> r.rs:5:5
  |
5 |     foo (cl);
  |     ^^^ one type is more general than the other
  |
  = note: expected type `FnOnce<(&i32,)>`
             found type `FnOnce<(&i32,)>`

I fail to see the difference between the expected and actual type here. Also, which types is rustc even comparing? Where does the FnOnce come from?

Expected behavior: The code should compile, with the closure argument type being inferred as &i32. In fact, this is what happens as soon as I change the closure definition to

let cl = |c: &_| -> () { c % 2; };

i.e. by adding a hint that c has to be a reference. But this is obvious from foo's signature, isn't it?

At least, there should be a less confusing error message.

Meta

rustc --version --verbose:

rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-unknown-linux-gnu
release: 1.49.0

rustc +nightly --version --verbose:

rustc 1.51.0-nightly (c97f11af7 2021-01-10)
binary: rustc
commit-hash: c97f11af7bc4a6d3578f6a953be04ab2449a5728
commit-date: 2021-01-10
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
@FabianWolff FabianWolff added the C-bug Category: This is a bug. label Jan 11, 2021
@dylni
Copy link
Contributor

dylni commented Jan 11, 2021

This might be the same issue as #75791.

@camelid
Copy link
Member

camelid commented Jan 12, 2021

Yep, almost certainly a duplicate of #75791.

@camelid camelid closed this as completed Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants