Skip to content

Cannot access associated constant with the same name as an enum variant if the enum's fields are unnamed #118555

New issue

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

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

Already on GitHub? Sign in to your account

Closed
Eisverygoodletter opened this issue Dec 3, 2023 · 1 comment
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Eisverygoodletter
Copy link

Eisverygoodletter commented Dec 3, 2023

I tried this code:

enum Enum {
    VariantA {},
    VariantB(i32),
    VariantC
}

impl Enum {
    pub const VariantA: f32 = 1.0;
    pub const VariantB: f32 = 2.0;
    pub const VariantC: f32 = 3.0;
}

fn main() {
    let associated_constant_value_a: f32 = Enum::VariantA;
    let associated_constant_value_b: f32 = Enum::VariantB;
    let associated_constant_value_c: f32 = Enum::VariantC;
}

I expected that there would be no errors because the associated constants would be accessed. Instead,

fn main() {
    // Accessing VariantA is ok and does not result in any errors
    let associated_constant_value_a: f32 = Enum::VariantA;
    // VariantB can't be accessed (`expected `f32`, found enum constructor`)
    // let associated_constant_value_b: f32 = Enum::VariantB;
    // VariantC can't be accessed because (`expected `f32`, found `Enum`)
    // let associated_constant_value_c: f32 = Enum::VariantC;
}

I believe this error occurs because VariantA is normally constructed with VariantA {...}, while VariantB and VariantC are constructed without requiring the curly braces {} or naming any fields.

I think accessing the associated constants with the same names as VariantB and VariantC should be allowed because they're all enum variants, which means they should share the same behaviour.

This issue is somewhat related to #75724 , but that issue only focused on VariantA-type variants with named fields

Meta

rustc --version --verbose:

rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-pc-windows-msvc
release: 1.74.0
LLVM version: 17.0.4

I also tested this in the playground on stable, beta and nightly. This issue is present in all 3 versions.

backtrace

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/main.rs:15:44
   |
15 |     let associated_constant_value_b: f32 = Enum::VariantB;
   |                                      ---   ^^^^^^^^^^^^^^ expected `f32`, found enum constructor
   |                                      |
   |                                      expected due to this
   |
   = note:          expected type `f32`
           found enum constructor `fn(i32) -> Enum {Enum::VariantB}`

error[E0308]: mismatched types
  --> src/main.rs:16:44
   |
16 |     let associated_constant_value_c: f32 = Enum::VariantC;
   |                                      ---   ^^^^^^^^^^^^^^ expected `f32`, found `Enum`
   |                                      |
   |                                      expected due to this

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 2 previous errors

@Eisverygoodletter Eisverygoodletter added the C-bug Category: This is a bug. label Dec 3, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 3, 2023
@Eisverygoodletter Eisverygoodletter changed the title Cannot access associated constant with the same name as an enum variant if the enum' Cannot access associated constant with the same name as an enum variant if the enum's fields are unnamed Dec 3, 2023
@jieyouxu jieyouxu added A-resolve Area: Name/path resolution done by `rustc_resolve` specifically T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 15, 2024
@petrochenkov
Copy link
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
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants