Skip to content

Enum variants shadow associated constants with no warning #117558

Closed
@loynoir

Description

@loynoir

User Story

As a rust newbie, I don't know which one is recommended.

  • or impl FooEnum

  • or impl FooEnumWrapperStruct

So, I check doc

https://doc.rust-lang.org/stable/rust-by-example/custom_types/enum/testcase_linked_list.html

Found

  • official doc example use impl FooEnum, not impl FooEnumWrapperStruct

Curiously, I tried this code:

enum Foo {
    A,
    B,
    C,
}

impl Foo {
    pub const A: u8 = 42;
    pub const Z: u8 = 42;
}

fn main() {
    assert_eq!(Foo::A as u8, 0);
    assert_eq!(Foo::B as u8, 1);
    assert_eq!(Foo::C as u8, 2);
    assert_eq!(Foo::Z as u8, 42);
}

I expected to see this happen:

  • or same name is treated invalid: compile failure, pub const A conflicts with Foo:A

  • or same name is treated valid: there should be core::get_enum_impl_pub_const(Foo, "A") equals to 42

Instead, this happened:

warning: associated constant `A` is never used
 --> path/to/reproduce.rs:8:15
  |
7 | impl Foo {
  | -------- associated constant in this implementation
8 |     pub const A: u8 = 42;
  |               ^
  |
  = note: `#[warn(dead_code)]` on by default

Meta

rustc --version --verbose:

1.73.0
Backtrace

<backtrace>

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 3, 2023
clubby789

clubby789 commented on Nov 3, 2023

@clubby789
Contributor

Same issue as #76347 I think. Seems to be intended given #57501, but it's confusing not to have a lint

changed the title [-]when `impl FooEnum` have same name[/-] [+]Enum variants shadow associated constants[/+] on Nov 3, 2023
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
and removed
C-bugCategory: This is a bug.
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 3, 2023
changed the title [-]Enum variants shadow associated constants[/-] [+]Enum variants shadow associated constants with no warning[/+] on Nov 3, 2023
added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
on Nov 6, 2023
petrochenkov

petrochenkov commented on Aug 1, 2024

@petrochenkov
Contributor

Closing as a duplicate of #48758.

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-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @petrochenkov@clubby789@fmease@rustbot@loynoir

        Issue actions

          Enum variants shadow associated constants with no warning · Issue #117558 · rust-lang/rust