Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
computermouth opened this issue Jul 31, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@computermouth
Copy link

computermouth commented Jul 31, 2024

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

@computermouth computermouth added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 31, 2024
@petrochenkov
Copy link
Contributor

Closing as a duplicate of #48758.
(There are multiple duplicates and I'll take the opportunity and close them all.)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants