Skip to content

Commit a29df44

Browse files
committed
Tweak ‘discriminant value already exists’ error message
Closes #15524.
1 parent 88baca7 commit a29df44

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/librustc/middle/typeck/check/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,9 +5004,14 @@ pub fn check_enum_variants(ccx: &CrateCtxt,
50045004
};
50055005

50065006
// Check for duplicate discriminant values
5007-
if disr_vals.contains(&current_disr_val) {
5008-
span_err!(ccx.tcx.sess, v.span, E0081,
5009-
"discriminant value already exists");
5007+
match disr_vals.iter().position(|&x| x == current_disr_val) {
5008+
Some(i) => {
5009+
span_err!(ccx.tcx.sess, v.span, E0081,
5010+
"discriminant value `{}` already exists", disr_vals[i]);
5011+
span_note!(ccx.tcx.sess, ccx.tcx().map.span(variants[i].id.node),
5012+
"conflicting discriminant here")
5013+
}
5014+
None => {}
50105015
}
50115016
// Check for unrepresentable discriminant values
50125017
match hint {

src/test/compile-fail/issue-15524.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
static N: int = 1;
12+
13+
enum Foo {
14+
A = 1,
15+
B = 1, //~ ERROR discriminant value `1` already exists
16+
//~^^ NOTE conflicting
17+
C = 0,
18+
D, //~ ERROR discriminant value `1` already exists
19+
//~^^^^^ NOTE conflicting
20+
E = N, //~ ERROR discriminant value `1` already exists
21+
//~^^^^^^^ NOTE conflicting
22+
}
23+
24+
fn main() {}

src/test/compile-fail/tag-variant-disr-dup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//error-pattern:discriminant value already exists
11+
//error-pattern:discriminant value
1212

1313
// black and white have the same discriminator value ...
1414

0 commit comments

Comments
 (0)