Skip to content

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

Closed
@archnim

Description

@archnim

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 !

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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