]> git.lizzy.rs Git - rust.git/commitdiff
Tweak ‘discriminant value already exists’ error message
authorP1start <rewi-github@whanau.org>
Sat, 4 Oct 2014 23:18:04 +0000 (12:18 +1300)
committerP1start <rewi-github@whanau.org>
Sun, 5 Oct 2014 01:16:32 +0000 (14:16 +1300)
Closes #15524.

src/librustc/middle/typeck/check/mod.rs
src/test/compile-fail/issue-15524.rs [new file with mode: 0644]
src/test/compile-fail/tag-variant-disr-dup.rs

index ca5d711d360ab509f0689c9ce180ccec6420aef1..315074e19c3a3cc4da764fbc38509cb0b65f872a 100644 (file)
@@ -5004,9 +5004,14 @@ fn do_check(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 {
diff --git a/src/test/compile-fail/issue-15524.rs b/src/test/compile-fail/issue-15524.rs
new file mode 100644 (file)
index 0000000..1e7bd6f
--- /dev/null
@@ -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() {}
index d0608ec4c19273db2eab7f7c85ad94260e77b03d..5da5bb854097d6b4a1be8de095223a27ae4baf4e 100644 (file)
@@ -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 ...