Skip to content

Enum associated functions can name-clash with variants, but cannot be called. #39946

Open
@tekacs

Description

@tekacs

Description

It's currently possible to name a function associated to an enum identically to the name of one of its variants.

This function cannot be called (see below), but rather than warning at the point that the function is defined, the compiler only emits an error upon trying to call said function (and in some cases - see the 'Alternate Reproduction' - it doesn't even do that).

https://is.gd/EZQ9mL

#[derive(Debug)]
enum X {
    a
}
impl X {
    fn a() -> X {
        X::a
    }
}
fn main() {
    println!("{:?}", X::a())
}

yields:

rustc 1.15.1 (021bd294c 2017-02-08)
error: `X::a` is being called, but it is not a function
  --> <anon>:13:22
   |
13 |     println!("{:?}", X::a())
   |                      ^^^^^^
   |
   = help: did you mean to write `X::a`?
note: defined here
  --> <anon>:3:5
   |
3  |     a
   |     ^

error: aborting due to previous error

Expected behaviour:

Emit an error at the point (fn a() -> X) that the function is defined.

Alternate reproduction

Even more confusing is the code:

https://is.gd/S15gOw

#[derive(Debug)]
enum X {
    a(),
    b,
}

impl X {
    fn a() -> X {
        X::b
    }
}

fn main() {
    println!("{:?}", X::a())
}

This compiles and runs.
Any guesses whether the variant or the function wins? 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.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