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

Confusing error message when enum variant is used without capitalization if a trait function has the name name #112409

Open
dullbananas opened this issue Jun 8, 2023 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dullbananas
Copy link

Code

enum Foo {
    Count,
}

const VALUE: Foo = Foo::count;

Current output

error[E0599]: `Foo` is not an iterator
 --> src/lib.rs:5:25
  |
1 | enum Foo {
  | --------
  | |
  | variant or associated item `count` not found for this enum
  | doesn't satisfy `Foo: Iterator`
...
5 | const VALUE: Foo = Foo::count;
  |                         ^^^^^ `Foo` is not an iterator
  |
  = note: the following trait bounds were not satisfied:
          `Foo: Iterator`
          which is required by `&mut Foo: Iterator`
note: the trait `Iterator` must be implemented
 --> /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/traits/iterator.rs:74:1
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `count`, perhaps you need to implement it:
          candidate #1: `Iterator`

For more information about this error, try `rustc --explain E0599`.

Desired output

help: there is a variant with a similar name (notice the capitalization): `Count`

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

@dullbananas dullbananas added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 8, 2023
@RodBurman
Copy link

RodBurman commented Sep 26, 2024

The compiler seems to get confused with the names of functions in std::*, given the following code:

#[derive(Debug)]
enum Foo {
Count,
Get,
Max,
}

const VALUE1: Foo = Foo::count;
const VALUE2: Foo = Foo::get;
const VALUE3: Foo = Foo::max;

fn main() {
println!("Values 1, 2, 3 are {:?} {:?} {:?}", VALUE1, VALUE2, VALUE3);
}

cargo build // version 1.81.0 (2dbb1af80 2024-08-20) gives:

Compiling enumCap v0.1.0 (/Users/rod/code/rust/triage/enumCap)
error[E0599]: Foo is not an iterator
--> src/main.rs:8:26
|
2 | enum Foo {
| -------- variant or associated item count not found for this enum because it doesn't satisfy Foo: Iterator
...
8 | const VALUE1: Foo = Foo::count;
| ^^^^^ Foo is not an iterator
|
= note: the following trait bounds were not satisfied:
Foo: Iterator
which is required by &mut Foo: Iterator
note: the trait Iterator must be implemented
--> /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/iter/traits/iterator.rs:44:1
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item count, perhaps you need to implement it:
candidate #1: Iterator

error[E0599]: no variant or associated item named get found for enum Foo in the current scope
--> src/main.rs:9:26
|
2 | enum Foo {
| -------- variant or associated item get not found for this enum
...
9 | const VALUE2: Foo = Foo::get;
| ^^^ variant or associated item not found in Foo
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item get, perhaps you need to implement it:
candidate #1: SliceIndex
help: there is a variant with a similar name
|
9 | const VALUE2: Foo = Foo::Get;
| ~~~

error[E0599]: the variant or associated item max exists for enum Foo, but its trait bounds were not satisfied
--> src/main.rs:10:26
|
2 | enum Foo {
| -------- variant or associated item max not found for this enum because it doesn't satisfy Foo: Iterator or Foo: Ord
...
10 | const VALUE3: Foo = Foo::max;
| ^^^ variant or associated item cannot be called on Foo due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
Foo: Ord
which is required by &Foo: Ord
Foo: Ord
which is required by &mut Foo: Ord
Foo: Iterator
which is required by &mut Foo: Iterator
note: the trait Iterator must be implemented
--> /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/iter/traits/iterator.rs:44:1
help: consider annotating Foo with #[derive(Eq, Ord, PartialEq, PartialOrd)]
|
2 + #[derive(Eq, Ord, PartialEq, PartialOrd)]
3 | enum Foo {
|

For more information about this error, try rustc --explain E0599.
error: could not c_ompile enumCap (bin "enumCap") due to 3 previous errors

Note: functions from Iterator, SliceIndex and Ord

@RodBurman
Copy link

RodBurman commented Sep 28, 2024

Thinking further the current error may be showing the correct problem but it may not, so it would be helpful to extend the error with something like "Alternatively did you intend to use enum variant 'Foo::Count'".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

2 participants