Skip to content

Improve error message when using Self in impl enum #56104

Closed
@0xrgb

Description

@0xrgb
#[derive(Debug)]
enum MainError {
    IO(std::io::Error),
    Parse(std::num::ParseIntError),
}

impl From<std::io::Error> for MainError {
    fn from(e: std::io::Error) -> Self {
        Self::IO(e)
    }
}

impl From<std::num::ParseIntError> for MainError {
    fn from(e: std::num::ParseIntError) -> Self {
        Self::Parse(e)
    }
}

Compiling above code give errors,

error[E0599]: no variant named `IO` found for type `MainError` in the current scope
  --> src\main.rs:12:9
   |
5  | enum MainError {
   | -------------- variant `IO` not found here
...
12 |         Self::IO(e)
   |         ^^^^^^^^ variant not found in `MainError`
   |
   = note: did you mean `MainError::IO`?

The error message is helpful (it indicates correct code, MainError::IO). However, the first line of message is confusing, and E0599 does not help much to understand why this code is not working.

Can we improve error messages for such situations?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-resolveArea: Name/path resolution done by `rustc_resolve` specifically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions