Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tweak ‘discriminant value already exists’ error message
Closes #15524.
  • Loading branch information
ftxqxd committed Oct 5, 2014
1 parent 88baca7 commit a29df44
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/librustc/middle/typeck/check/mod.rs
Expand Up @@ -5004,9 +5004,14 @@ pub fn check_enum_variants(ccx: &CrateCtxt,
};

// Check for duplicate discriminant values
if disr_vals.contains(&current_disr_val) {
span_err!(ccx.tcx.sess, v.span, E0081,
"discriminant value already exists");
match disr_vals.iter().position(|&x| x == current_disr_val) {
Some(i) => {
span_err!(ccx.tcx.sess, v.span, E0081,
"discriminant value `{}` already exists", disr_vals[i]);
span_note!(ccx.tcx.sess, ccx.tcx().map.span(variants[i].id.node),
"conflicting discriminant here")
}
None => {}
}
// Check for unrepresentable discriminant values
match hint {
Expand Down
24 changes: 24 additions & 0 deletions src/test/compile-fail/issue-15524.rs
@@ -0,0 +1,24 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

static N: int = 1;

enum Foo {
A = 1,
B = 1, //~ ERROR discriminant value `1` already exists
//~^^ NOTE conflicting
C = 0,
D, //~ ERROR discriminant value `1` already exists
//~^^^^^ NOTE conflicting
E = N, //~ ERROR discriminant value `1` already exists
//~^^^^^^^ NOTE conflicting
}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/compile-fail/tag-variant-disr-dup.rs
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//error-pattern:discriminant value already exists
//error-pattern:discriminant value

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

Expand Down

0 comments on commit a29df44

Please sign in to comment.