Skip to content

Closure signature not mentioned on mismatch with fn ptr #119266

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 23, 2023
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 23, 2023
  3. added
    D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 23, 2023
  4. wutchzone commented on Mar 26, 2024

    @wutchzone
    Contributor

    @rustbot claim

    Sounds like a feature I would like to use as well.

  5. added 2 commits that reference this issue on Apr 20, 2024
    b15ad39
    e9e936c
  6. added a commit that references this issue on Apr 21, 2024
    4cff725
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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