Skip to content

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

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
scottmcm opened this issue Sep 4, 2020 · 6 comments
Closed
Labels
A-associated-items Area: Associated items (types, constants & functions) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@scottmcm
Copy link
Member

scottmcm commented Sep 4, 2020

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.

@scottmcm scottmcm added the A-maybe-future-edition Something we may consider for a future edition. label Sep 4, 2020
@petrochenkov
Copy link
Contributor

IIRC, when "variants as associated items" were accepted, the decision was to report an associated item overlap error similar to

struct S;
impl S { fn a() {} }
impl S { fn a() {} } //~ ERROR

in situations like this.

@jyn514
Copy link
Member

jyn514 commented Sep 5, 2020

I recently discovered this in rustdoc: #75756 (comment). I guess rustdoc should track the rustc behavior (right now it's buggy and gives the const precedence):

Res::Def(DefKind::Enum, did) => {
if cx
.tcx
.inherent_impls(did)
.iter()
.flat_map(|imp| cx.tcx.associated_items(*imp).in_definition_order())
.any(|item| item.ident.name == variant_name)
{
return Err(ErrorKind::ResolutionFailure);
}

So give the variant precedence now and change it when/if rustc changes it.

@jyn514 jyn514 added the A-associated-items Area: Associated items (types, constants & functions) label Sep 5, 2020
@scrabsha
Copy link
Contributor

scrabsha commented Sep 6, 2020

Can I start working on it?

@scrabsha
Copy link
Contributor

scrabsha commented Sep 6, 2020

IIRC, when "variants as associated items" were accepted, the decision was to report an associated item overlap error similar to

struct S;
impl S { fn a() {} }
impl S { fn a() {} } //~ ERROR

in situations like this.

@petrochenkov, can you post a link of where this has been discussed?

Also, this would break the test ui/type-alias-enum-variants/enum-variant-priority-higher-than-other-inherent.rs. I am not sure if I should just remove the relevant code.

By the way, should we emit an error when a function name is the same as an enum variant?

@nikomatsakis nikomatsakis removed the A-maybe-future-edition Something we may consider for a future edition. label Mar 8, 2021
@scottmcm
Copy link
Member Author

Looks like this is related to #48758 (comment)

@petrochenkov
Copy link
Contributor

Closing as a duplicate of #48758.

@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

7 participants