Skip to content

Type mismatch errors through generic enums could be clearer #105240

Open
@jruderman

Description

@jruderman

Code

playground

enum A<T> {
    X(T)
}

fn f() -> A<()> {
    A::X(0_u32)
}

fn main() {}

Current output

error[E0308]: mismatched types
 --> src/main.rs:6:10
  |
6 |     A::X(0_u32)
  |     ---- ^^^^^ expected `()`, found `u32`
  |     |
  |     arguments to this enum variant are incorrect
  |
note: tuple variant defined here
 --> src/main.rs:2:5
  |
2 |     X(T)
  |     ^

Due to the use of generics, seeing the definition of the enum variant doesn't really get at the root of the problem.

Ideal output

error[E0308]: mismatched types
 --> src/main.rs:6:10
  |
6 |     A::X(0_u32)
  |     ---- ^^^^^ expected `()`, found `u32`
  |     |
  |     arguments to this enum variant are incorrect

note: expected `()` due to the function signature specifying the generic type for `A`:
  |
5 | fn f() -> A<()> {
  |             ^^

Impact

This can happen with Option or Result, which are common return types.

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