Skip to content

Commit

Permalink
Rollup merge of rust-lang#35486 - KiChjang:e0081-bonus, r=jonathandtu…
Browse files Browse the repository at this point in the history
…rner

Shrink span to variant discriminant expression for E0081

Part of rust-lang#35233.
Extension of rust-lang#35353.
Fixes rust-lang#35224.

r? @jonathandturner
  • Loading branch information
Jonathan Turner authored Aug 11, 2016
2 parents d423c25 + bd2bd6c commit 853fe86
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
20 changes: 14 additions & 6 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/compile-fail/E0081.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
21 changes: 12 additions & 9 deletions src/test/compile-fail/issue-15524.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`

}

Expand Down

0 comments on commit 853fe86

Please sign in to comment.