Skip to content

No warning for an associated constant always hidden by an enum variant #76347

Closed
@scottmcm

Description

@scottmcm

To my great surprise, this compiles fine, with no warnings:

pub enum Foo {
    A(i32),
}
impl Foo {
    pub const A: Self = Foo::A(0);
}

Trying to actually use that constant, however, doesn't work:

pub fn demo() -> Foo {
    Foo::A
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d1e7c140a60c78cc6ede62b677a74e5e

error[E0308]: mismatched types
 --> src/lib.rs:9:5
  |
2 |     A(i32),
  |     ------ fn(i32) -> Foo {Foo::A} defined here
...
8 | pub fn demo() -> Foo {
  |                  --- expected `Foo` because of return type
9 |     Foo::A
  |     ^^^^^^ expected enum `Foo`, found fn item
  |
  = note: expected enum `Foo`
          found fn item `fn(i32) -> Foo {Foo::A}`
help: use parentheses to instantiate this tuple variant
  |
9 |     Foo::A(_)
  |           ^^^

If that constant is going to compile, it should probably at least be linted against.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions