Rollup merge of #35486 - KiChjang:e0081-bonus, r=jonathandturner

Shrink span to variant discriminant expression for E0081

Part of #35233.
Extension of #35353.
Fixes #35224.

r? @jonathandturner
This commit is contained in:
Jonathan Turner 2016-08-11 06:33:58 -07:00 committed by GitHub
commit 853fe86906
3 changed files with 30 additions and 17 deletions

View File

@ -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);
}

View File

@ -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
}

View File

@ -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`
}