Closed
Description
This is split out from the discussion in #97319.
First, the existing error for duplicate enum discriminants could be improved. The best summary of what we have now can be seen in the test added for #97319, which fixes part of it:
enum Enum {
P = 3,
X = 3,
Y = 5
}
#[repr(u8)]
enum EnumOverflowRepr {
P = 257,
X = 513,
}
#[repr(i8)]
enum NegDisEnum {
First = -1,
Second = -2,
Last,
}
Which outputs:
error[E0081]: discriminant value `3` assigned more than once
--> $DIR/E0081.rs:1:1
|
LL | / enum Enum {
LL | |
LL | | P = 3,
| | - first assignment of `3`
LL | |
LL | | X = 3,
| | - second assignment of `3`
LL | |
LL | | Y = 5
LL | | }
| |_^
error[E0081]: discriminant value `1` assigned more than once
--> $DIR/E0081.rs:11:1
|
LL | / enum EnumOverflowRepr {
LL | |
LL | | P = 257,
| | --- first assignment of `1` (overflowed from `257`)
LL | |
LL | | X = 513,
| | --- second assignment of `1` (overflowed from `513`)
LL | |
LL | | }
| |_^
error[E0081]: discriminant value `-1` assigned more than once
--> $DIR/E0081.rs:20:1
|
LL | / enum NegDisEnum {
LL | |
LL | | First = -1,
| | -- first assignment of `-1`
LL | |
LL | | Second = -2,
| | ----------- assigned discriminant for `Last` was incremented from this discriminant
LL | |
LL | | Last,
| | ---- second assignment of `-1`
LL | |
LL | | }
| |_^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0081`.
I suggested that the last version of the error should suggest explicitly stating that the discriminant for Last
is computed as (Second = -2) + (1 variant later) = -1 = First
, or somehow better wording that. Essentially, make it clear to the user that things are incremented positively, regardless of the order of existing discriminants.
Secondly, @Bryysen suggested that the code that does this (check_enum
) could definitely use some additional refactoring as well in this comment.
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: Too much output caused by a single piece of incorrect code.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
Bryysen commentedon May 29, 2022
Another thing we might want to do is report all the duplicates in one error, instead of generating a new error for every collision. So currently for:
We get the errors:
It would be nice if we instead got:
rdzhaafar commentedon Jun 22, 2022
@rustbot claim
rdzhaafar commentedon Jul 5, 2022
I don't have time to work on this, unfortunately. I did get all the errors to show up in the same error message, will paste the code here when I get to my computer.
@rustbot release-assignment
Bryysen commentedon Jul 5, 2022
Thanks for the effort. I have a bunch of spare time going forwards so I can pick up where you left off
rdzhaafar commentedon Jul 6, 2022
@Bryysen
Perfect! My changes are in this fork
Basically I've factored out the duplicate-checking code into a separate function in rustc_typeck/check/check.rs. I've also added a couple of derive macros to make sure that
Discr<>
's can be used in a hashmap.The code now displays all duplicates in one message. What I haven't figured out, however, is how to check that a
Variant<>
's predecessor is negative, since the value is stored as au128
.Best of luck!
Bryysen commentedon Jul 6, 2022
@rustbot claim
Rollup merge of rust-lang#100238 - Bryysen:master, r=cjgillot