Skip to content

Type mismatch when pattern matching to a unit struct could really use some better diagnostics #48062

Closed
@sgrif

Description

@sgrif

I've answered a question like this every day for the past 3 days... https://users.rust-lang.org/t/how-to-create-a-new-option-serde-json-value-value/15466

The problem is with code like this:

struct foo;

let foo = 1;

Typically the let binding and the struct definition are much more separated, and the struct definition comes from a glob import. The error received is:

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
4 |     let foo = 1;
  |         ^^^ expected integral variable, found struct `main::foo`
  |
  = note: expected type `{integer}`
             found type `main::foo`

This error isn't wrong, and I'm not even proposing that we remove it. However, in the specific case where the match is on a unit struct, I do think it would be incredibly helpful to add a help diagnostic pointing at the span where foo was brought into scope, stating that a struct with zero fields named foo was imported. E.g.

 --> src/main.rs:2:4
  |
2 |     struct foo;
  |     ^^^^^^^^^^ note: A struct named `foo` with zero fields was brought into scope here.
  |

I have purposely biased this error message towards use vs definition, because I think the overwhelmingly common case is going to be from use not definition (though the error is still reasonable when pointing at the definition).

I'd be happy to implement this if it'd be accepted, though I think I'll probably need a bit of help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-feature-requestCategory: A feature request, i.e: not implemented / a PR.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions