Skip to content

Enum variants completely shadow inherent associated functions. #48758

Open
@leoyvens

Description

@leoyvens

Consider the code below:

enum Foo { Bar(i32) }

impl Foo {
    fn Bar(x: i32) -> Foo { 
        println!("I'm uncallable"); 
        Foo::Bar(x) 
    }
}

fn main() {
    Foo::Bar(0); // Variant constructor.
   <Foo>::Bar(0); // Inherent function.
}

It compiles successfully and terminates with no output. There is an ambiguity between the variant and the associated function, though it seems we always prefer the variant, making the associated function uncallable. Having an inherent associated function with the same name as a variant should be an error.

Edit: @petrochenkov points out that it's not actually uncallable, you can call it with <Foo>::Bar(0). A deny-by-default lint then perhaps?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions