Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink span to variant discriminant expression for E0081 #35486

Merged
merged 1 commit into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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