Skip to content

Closure signature not mentioned on mismatch with fn ptr #119266

Closed
@Noratrieb

Description

@Noratrieb

Code

fn main() {
    let x = |a: u8| {};
    
    let x: fn(i32) = x;
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:4:22
  |
2 |     let x = |a: u8| {};
  |             ------- the found closure
3 |     
4 |     let x: fn(i32) = x;
  |            -------   ^ expected fn pointer, found closure
  |            |
  |            expected due to this
  |
  = note: expected fn pointer `fn(i32)`
                found closure `{closure@src/main.rs:2:13: 2:20}`

Desired output

error[E0308]: mismatched types
 --> src/main.rs:4:22
  |
2 |     let x = |a: u8| {};
  |             ------- the found closure
3 |     
4 |     let x: fn(i32) = x;
  |            -------   ^ expected fn pointer, found closure
  |            |
  |            expected due to this
  |
  = note: expected fn pointer `fn(i32)`
  |             found closure `{closure@src/main.rs:2:13: 2:20}`
  = note: closure has signature `fn(u8)`

Rationale and extra context

Here it's obvious, but when the error says that it is mismatched with for<'tcx> fn(context::TyCtxt<'tcx>, rustc_span::def_id::LocalDefId) -> &'tcx rustc_data_structures::unord::UnordSet<rustc_span::Symbol> and that "types differ in mutability" then I have zero chance of figuring what's going on.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-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