From bd2bd6c71ea23661b30bdec8c12ff5dfee17bede Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Sun, 7 Aug 2016 23:33:05 -0700 Subject: [PATCH] Shrink span to variant discriminant expression for E0081 --- src/librustc_typeck/check/mod.rs | 20 ++++++++++++++------ src/test/compile-fail/E0081.rs | 6 ++++-- src/test/compile-fail/issue-15524.rs | 21 ++++++++++++--------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 4bb36aa639c54..5aa72e051c2a3 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1272,13 +1272,21 @@ pub fn check_enum_variants<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, // Check for duplicate discriminant values if let Some(i) = disr_vals.iter().position(|&x| x == current_disr_val) { - let mut err = struct_span_err!(ccx.tcx.sess, v.span, E0081, - "discriminant value `{}` already exists", disr_vals[i]); let variant_i_node_id = ccx.tcx.map.as_local_node_id(variants[i].did).unwrap(); - err.span_label(ccx.tcx.map.span(variant_i_node_id), - &format!("first use of `{}`", disr_vals[i])); - err.span_label(v.span , &format!("enum already has `{}`", disr_vals[i])); - err.emit(); + let variant_i = ccx.tcx.map.expect_variant(variant_i_node_id); + let i_span = match variant_i.node.disr_expr { + Some(ref expr) => expr.span, + None => ccx.tcx.map.span(variant_i_node_id) + }; + let span = match v.node.disr_expr { + Some(ref expr) => expr.span, + None => v.span + }; + struct_span_err!(ccx.tcx.sess, span, E0081, + "discriminant value `{}` already exists", disr_vals[i]) + .span_label(i_span, &format!("first use of `{}`", disr_vals[i])) + .span_label(span , &format!("enum already has `{}`", disr_vals[i])) + .emit(); } disr_vals.push(current_disr_val); } diff --git a/src/test/compile-fail/E0081.rs b/src/test/compile-fail/E0081.rs index b63265564b334..9911e093a8980 100644 --- a/src/test/compile-fail/E0081.rs +++ b/src/test/compile-fail/E0081.rs @@ -9,8 +9,10 @@ // except according to those terms. enum Enum { - P = 3, - X = 3, //~ ERROR E0081 + P = 3, //~ NOTE first use of `3isize` + X = 3, + //~^ ERROR discriminant value `3isize` already exists + //~| NOTE enum already has `3isize` Y = 5 } diff --git a/src/test/compile-fail/issue-15524.rs b/src/test/compile-fail/issue-15524.rs index 3d6f224c24904..658a0c1546b9f 100644 --- a/src/test/compile-fail/issue-15524.rs +++ b/src/test/compile-fail/issue-15524.rs @@ -12,17 +12,20 @@ const N: isize = 1; enum Foo { A = 1, - //~^ NOTE first use - //~| NOTE first use - //~| NOTE first use - B = 1, //~ ERROR discriminant value - //~^ NOTE enum already + //~^ NOTE first use of `1isize` + //~| NOTE first use of `1isize` + //~| NOTE first use of `1isize` + B = 1, + //~^ ERROR discriminant value `1isize` already exists + //~| NOTE enum already has `1isize` C = 0, - D, //~ ERROR discriminant value - //~^ NOTE enum already + D, + //~^ ERROR discriminant value `1isize` already exists + //~| NOTE enum already has `1isize` - E = N, //~ ERROR discriminant value - //~^ NOTE enum already + E = N, + //~^ ERROR discriminant value `1isize` already exists + //~| NOTE enum already has `1isize` }