Skip to content

Using variant on self type with path Self::Variant causes subpar diagnostic #52118

Description

@andradei

The snippet below fails to compile with the following messages of each enum variant:

pub enum State {
    Init,
    NewProject,
    NewTask,
    NewEvent,
}

impl State {
    pub fn commands<'a>(&self) -> usize {
        match self {
            Self::Init => 1,
            Self::NewProject | Self::NewTask | Self::NewEvent => 2,
            _ => 0,
        }
    }
}

fn main() {}
error[E0599]: no variant named `Init` found for type `State` in the current scope
  --> src/main.rs:11:13
   |
1  | pub enum State {
   | -------------- variant `Init` not found here
...
11 |             Self::Init => 1,
   |             ^^^^^^^^^^ variant not found in `State`
   |
   = note: did you mean `variant::Init`?

The solution is to replace Self::Init with explicit type State::Init. But the issues are:

  • Error E0599 doesn't explain the actual problem (Use of Self instead of the explicit type)
  • The message no variant named "Init" found for type "State" in the current scope is misleading
  • note: did you mean "variant::Init"? is also a little misleading. A phrase like note: replace "Self::Init" with "State::Init" is much clearer

And finally:

  • Why isn't Self:: allowed inside impl MyEnum blocks?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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