Skip to content

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

Closed
@Eisverygoodletter

Description

@Eisverygoodletter

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-bugCategory: This is a bug.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions