Skip to content

Defining an associated constant with the same name as an enum value does not produce an error #128462

Closed
@computermouth

Description

@computermouth

Code

#[derive(Debug)]
enum Number {
    One,
    Two,
}

impl Number {
    pub const Uno: Number = Number::One;
    pub const Two: Number = Number::One;
}

fn main() {
    
    eprintln!("{:?}", Number::Uno);
    eprintln!("{:?}", Number::Two);

}

Current output

warning: associated constant `Two` is never used
 --> src/main.rs:9:15
  |
7 | impl Number {
  | ----------- associated constant in this implementation
8 |     pub const Uno: Number = Number::One;
9 |     pub const Two: Number = Number::One;
  |               ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: associated constant `Uno` should have an upper case name
[...]
warning: associated constant `Two` should have an upper case name
[...]
One
Two

Desired output

error: associated constant `Two` conflicts with enum value `Number::Two`
 --> src/main.rs:9:15
  |
7 | impl Number {
  | ----------- associated constant in this implementation
8 |     pub const Uno: Number = Number::One;
9 |     pub const Two: Number = Number::One;

Rationale and extra context

Maybe this isn't a bug, and I just don't understand the purpose for this behavior. It seems to me the associated constant cannot be constructed, and the compiler should produce a specific error or at least a warning for it.

I imagine the current consensus might be that the warning:

warning: associated constant `Two` should have an upper case name
 --> src/main.rs:9:15
  |
9 |     pub const Two: Number = Number::One;
  |               ^^^ help: convert the identifier to upper case (notice the capitalization): `TWO`

Would constitute an answer to the problem we see here, but it also doesn't specifically mention that the two identifiers are conflicting.

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

Anything else?

No response

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