Skip to content
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

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

Open
tekacs opened this issue Feb 19, 2017 · 5 comments
Open
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tekacs
Copy link

tekacs commented Feb 19, 2017

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? 😄

@petrochenkov
Copy link
Contributor

There is a way to call the associated function, but I won't tell it to you because it will be removed soon 😄
The plan is indeed to report an error on definition of the associated function in similar way like #36889 is reported.

@Mark-Simulacrum
Copy link
Member

@petrochenkov I don't see an error on the associated function yet; so presumably the "soon" is not yet here. Or did some implementation miss this case?

@petrochenkov
Copy link
Contributor

@Mark-Simulacrum
"Soon" is about resolving UFCS paths like <X>::a to variants (and resolving variants as associated items in general), the error on inherent associated items "overlapping" with variants is a separate (and also unimplemented) thing.

@Mark-Simulacrum Mark-Simulacrum added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Jul 27, 2017
@Spoonbender
Copy link

Spoonbender commented Mar 21, 2022

Triage: both these examples reproduce in the 2021 edition, tested on rustc 1.59.0 (9d1b2106e 2022-02-23)

@petrochenkov
Copy link
Contributor

Enum variants are still not included into the overlap check for inherent impl items.

@Dylan-DPC Dylan-DPC added C-bug Category: This is a bug. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants