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

Unsufficient report when encountering a mismatch between fn pointer and fn item #106652

Closed
archnim opened this issue Jan 9, 2023 · 1 comment
Closed
Labels
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.

Comments

@archnim
Copy link

archnim commented Jan 9, 2023

Hello world. Here is an example of code;

fn main() {
	fn test() { println!("Testing !") }
	let hm: HashMap<&str, fn()> = HashMap::from([("test", test)]);
	hm.get("test").unwrap()();
}

Here is the output:

error[E0308]: mismatched types
 --> src/main.rs:5:32
  |
5 |     let hm: HashMap<&str, fn()> = HashMap::from([("test", test)]);
  |             -------------------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found fn item
  |             |
  |             expected due to this
  |
  = note: expected struct `HashMap<&str, fn()>`
             found struct `HashMap<&str, fn() {test}>`

The issue with this output is that, for a newbie, it is very mysterious, it doesn't help to know what exactly is wrong or at least how to fix it.

The compiler should print a hint like: "try test as fn()".

Thanks in advance !

@archnim archnim 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. labels Jan 9, 2023
@compiler-errors
Copy link
Member

#105552 might help with this..

@archnim archnim closed this as completed Jan 9, 2023
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants