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

'Expected <function pointer>, found <function item>' diagnostic could explicitly suggest casting #132648

Closed
clubby789 opened this issue Nov 5, 2024 · 1 comment · Fixed by #133382
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@clubby789
Copy link
Contributor

clubby789 commented Nov 5, 2024

Code

fn foo() -> usize {
    0
}

fn bar() -> usize {
    1
}

fn main() {
    let _x: Vec<(&str, fn() -> usize)> = [("foo", foo)].into_iter().collect();
}

Current output

error[E0277]: a value of type `Vec<(&str, fn() -> usize)>` cannot be built from an iterator over elements of type `(&str, fn() -> usize {foo})`
    --> src/main.rs:10:69
     |
10   |     let _x: Vec<(&str, fn() -> usize)> = [("foo", foo)].into_iter().collect();
     |                                                                     ^^^^^^^ value of type `Vec<(&str, fn() -> usize)>` cannot be built from `std::iter::Iterator<Item=(&str, fn() -> usize {foo})>`
     |
     = help: the trait `FromIterator<(&str, fn() -> usize {foo})>` is not implemented for `Vec<(&str, fn() -> usize)>`
     = help: the trait `FromIterator<(&str, fn() -> usize)>` is implemented for `Vec<(&str, fn() -> usize)>`
     = help: for that trait implementation, expected `fn() -> usize`, found `fn() -> usize {foo}`

Desired output

error[E0277]: a value of type `Vec<(&str, fn() -> usize)>` cannot be built from an iterator over elements of type `(&str, fn() -> usize {foo})`
    --> src/main.rs:10:69
     |
10   |     let _x: Vec<(&str, fn() -> usize)> = [("foo", foo)].into_iter().collect();
     |                                                                     ^^^^^^^ value of type `Vec<(&str, fn() -> usize)>` cannot be built from `std::iter::Iterator<Item=(&str, fn() -> usize {foo})>`
     |
     = help: the trait `FromIterator<(&str, fn() -> usize {foo})>` is not implemented for `Vec<(&str, fn() -> usize)>`
     = help: the trait `FromIterator<(&str, fn() -> usize)>` is implemented for `Vec<(&str, fn() -> usize)>`
     = help: for that trait implementation, expected `fn() -> usize`, found `fn() -> usize {foo}`
     = help: consider casting the fn item to a fn pointer using `as fn() -> usize`

Rationale and extra context

If we add a second entry in the array, we get a type check error suggesting this:

  --> src/main.rs:10:65
   |
10 |     let _x: Vec<(&str, fn() -> usize)> = [("foo", foo), ("bar", bar)].into_iter().collect();
   |                                                                 ^^^ expected fn item, found a different fn item
   |
   = note: expected fn item `fn() -> _ {foo}`
              found fn item `fn() -> _ {bar}`
   = note: different fn items have unique types, even if their signatures are the same
   = help: consider casting both fn items to fn pointers using `as fn() -> usize`

This can lead to a confusing diagnostic if one is not aware of the distinction between fn items and pointers

Other cases

No response

Rust Version

$ rustc 1.84.0-nightly (662180b 2024-10-20)
binary: rustc
commit-hash: 662180b
commit-date: 2024-10-20
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1

Anything else?

No response

@clubby789 clubby789 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Nov 5, 2024
@mu001999
Copy link
Contributor

@rustbot claim

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 29, 2025
Suggest considering casting fn item as fn pointer in more cases

Fixes rust-lang#132648
@bors bors closed this as completed in f8d103d Jan 29, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 29, 2025
Rollup merge of rust-lang#133382 - mu001999-contrib:diag/fnitem, r=lcnr

Suggest considering casting fn item as fn pointer in more cases

Fixes rust-lang#132648
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants