Skip to content

Improve diagnostics like "incompatible types: expected fn item, found a different fn item" #102608

Closed
@jruderman

Description

@jruderman

Given the following code (playground):

fn double(n: u32) -> u32 { 
    n * 2
}

fn triple(n: u32) -> u32 {
    n * 3
}

fn f(n: u32) -> u32 {
    let g = if n % 2 == 0 {
        &double
    } else {
        &triple
    };

    g(n)
}

fn main() {
    assert_eq!(f(7), 21);
    assert_eq!(f(8), 16);
}

The current output is:

error[E0308]: `if` and `else` have incompatible types
  --> src/main.rs:13:9
   |
10 |       let g = if n % 2 == 0 {
   |  _____________-
11 | |         &double
   | |         ------- expected because of this
12 | |     } else {
13 | |         &triple
   | |         ^^^^^^^ expected fn item, found a different fn item
14 | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected reference `&fn(u32) -> u32 {double}`
              found reference `&fn(u32) -> u32 {triple}`

I would like this error to explain why these are incompatible despite having the same signature (or link to an explanation).

I would also like it to suggest an alternative, perhaps one of these:

  • Change &double to &(double as fn(u32) -> u32)
  • Change &double to double

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.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