Skip to content

Commit 74e71da

Browse files
committed
Fix plural form of variant in error message not formatting correctly
due to ordering, added/improved comments and removed redundant test already caught by `E0081.rs`
1 parent bfd7535 commit 74e71da

File tree

5 files changed

+32
-37
lines changed

5 files changed

+32
-37
lines changed

compiler/rustc_typeck/src/check/check.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1499,18 +1499,19 @@ fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, vs: &'tcx [hir::Variant<'tcx>], def_id: L
14991499
check_transparent(tcx, sp, def);
15001500
}
15011501

1502-
/// Part of enum check, errors if two or more discriminants are equal
1502+
/// Part of enum check. Given the discriminants of an enum, errors if two or more discriminants are equal
15031503
fn detect_discriminant_duplicate<'tcx>(
15041504
tcx: TyCtxt<'tcx>,
15051505
mut discrs: Vec<(VariantIdx, Discr<'tcx>)>,
15061506
vs: &'tcx [hir::Variant<'tcx>],
15071507
self_span: Span,
15081508
) {
1509-
// Helper closure to reduce duplicate code. This gets called everytime we detect a duplicate
1509+
// Helper closure to reduce duplicate code. This gets called everytime we detect a duplicate.
1510+
// Here `idx` refers to the order of which the discriminant appears, and its index in `vs`
15101511
let report = |dis: Discr<'tcx>,
15111512
idx: usize,
15121513
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>| {
1513-
let var = &vs[idx];
1514+
let var = &vs[idx]; // HIR for the duplicate discriminant
15141515
let (span, display_discr) = match var.disr_expr {
15151516
Some(ref expr) => {
15161517
// In the case the discriminant is both a duplicate and overflowed, let the user know
@@ -1533,8 +1534,8 @@ fn detect_discriminant_duplicate<'tcx>(
15331534
vs[..idx].iter().rev().enumerate().find(|v| v.1.disr_expr.is_some())
15341535
{
15351536
let ve_ident = var.ident;
1536-
let sp = if n > 1 { "variants" } else { "variant" };
15371537
let n = n + 1;
1538+
let sp = if n > 1 { "variants" } else { "variant" };
15381539

15391540
err.span_label(
15401541
*span,
@@ -1549,7 +1550,7 @@ fn detect_discriminant_duplicate<'tcx>(
15491550
err.span_label(span, format!("{display_discr} assigned here"));
15501551
};
15511552

1552-
// Here we are loop through the discriminants, comparing each discriminant to another.
1553+
// Here we loop through the discriminants, comparing each discriminant to another.
15531554
// When a duplicate is detected, we instatiate an error and point to both
15541555
// initial and duplicate value. The duplicate discriminant is then discarded by swapping
15551556
// it with the last element and decrementing the `vec.len` (which is why we have to evaluate

src/test/ui/error-codes/E0081.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ enum NegDisEnum {
2727
//~^ NOTE `-1` assigned here
2828
}
2929

30-
#[repr(i32)]
3130
enum MultipleDuplicates {
3231
//~^ ERROR discriminant value `0` assigned more than once
32+
//~^^ ERROR discriminant value `-2` assigned more than once
3333
V0,
3434
//~^ NOTE `0` assigned here
3535
V1 = 0,
@@ -39,10 +39,15 @@ enum MultipleDuplicates {
3939
V4 = 0,
4040
//~^ NOTE `0` assigned here
4141
V5 = -2,
42-
//~^ NOTE discriminant for `V7` incremented from this startpoint (`V5` + 2 variant later => `V7` = 0)
42+
//~^ NOTE discriminant for `V7` incremented from this startpoint (`V5` + 2 variants later => `V7` = 0)
43+
//~^^ NOTE `-2` assigned here
4344
V6,
4445
V7,
4546
//~^ NOTE `0` assigned here
47+
V8 = -3,
48+
//~^ NOTE discriminant for `V9` incremented from this startpoint (`V8` + 1 variant later => `V9` = -2)
49+
V9,
50+
//~^ NOTE `-2` assigned here
4651
}
4752

4853
fn main() {

src/test/ui/error-codes/E0081.stderr

+19-4
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ LL | Last,
3838
| ---- `-1` assigned here
3939

4040
error[E0081]: discriminant value `0` assigned more than once
41-
--> $DIR/E0081.rs:31:1
41+
--> $DIR/E0081.rs:30:1
4242
|
4343
LL | enum MultipleDuplicates {
4444
| ^^^^^^^^^^^^^^^^^^^^^^^
45-
LL |
45+
...
4646
LL | V0,
4747
| -- `0` assigned here
4848
LL |
@@ -53,11 +53,26 @@ LL | V4 = 0,
5353
| - `0` assigned here
5454
LL |
5555
LL | V5 = -2,
56-
| ------- discriminant for `V7` incremented from this startpoint (`V5` + 2 variant later => `V7` = 0)
56+
| ------- discriminant for `V7` incremented from this startpoint (`V5` + 2 variants later => `V7` = 0)
5757
...
5858
LL | V7,
5959
| -- `0` assigned here
6060

61-
error: aborting due to 4 previous errors
61+
error[E0081]: discriminant value `-2` assigned more than once
62+
--> $DIR/E0081.rs:30:1
63+
|
64+
LL | enum MultipleDuplicates {
65+
| ^^^^^^^^^^^^^^^^^^^^^^^
66+
...
67+
LL | V5 = -2,
68+
| -- `-2` assigned here
69+
...
70+
LL | V8 = -3,
71+
| ------- discriminant for `V9` incremented from this startpoint (`V8` + 1 variant later => `V9` = -2)
72+
LL |
73+
LL | V9,
74+
| -- `-2` assigned here
75+
76+
error: aborting due to 5 previous errors
6277

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

src/test/ui/tag-variant-disr-dup.rs

-12
This file was deleted.

src/test/ui/tag-variant-disr-dup.stderr

-14
This file was deleted.

0 commit comments

Comments
 (0)