Show discriminant before overflow in diagnostic.
This commit is contained in:
parent
8b50cc9a2c
commit
4c0ff4db80
5 changed files with 52 additions and 8 deletions
|
|
@ -1472,15 +1472,17 @@ fn check_enum<'tcx>(
|
|||
Some(ref expr) => tcx.hir().span(expr.hir_id),
|
||||
None => v.span,
|
||||
};
|
||||
let display_discr = display_discriminant_value(tcx, v, discr.val);
|
||||
let display_discr_i = display_discriminant_value(tcx, variant_i, disr_vals[i].val);
|
||||
struct_span_err!(
|
||||
tcx.sess,
|
||||
span,
|
||||
E0081,
|
||||
"discriminant value `{}` already exists",
|
||||
disr_vals[i]
|
||||
discr.val,
|
||||
)
|
||||
.span_label(i_span, format!("first use of `{}`", disr_vals[i]))
|
||||
.span_label(span, format!("enum already has `{}`", disr_vals[i]))
|
||||
.span_label(i_span, format!("first use of {}", display_discr_i))
|
||||
.span_label(span, format!("enum already has {}", display_discr))
|
||||
.emit();
|
||||
}
|
||||
disr_vals.push(discr);
|
||||
|
|
@ -1490,6 +1492,25 @@ fn check_enum<'tcx>(
|
|||
check_transparent(tcx, sp, def);
|
||||
}
|
||||
|
||||
/// Format an enum discriminant value for use in a diagnostic message.
|
||||
fn display_discriminant_value<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
variant: &hir::Variant<'_>,
|
||||
evaluated: u128,
|
||||
) -> String {
|
||||
if let Some(expr) = &variant.disr_expr {
|
||||
let body = &tcx.hir().body(expr.body).value;
|
||||
if let hir::ExprKind::Lit(lit) = &body.kind {
|
||||
if let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node {
|
||||
if evaluated != *lit_value {
|
||||
return format!("`{}` (overflowed from `{}`)", evaluated, lit_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
format!("`{}`", evaluated)
|
||||
}
|
||||
|
||||
pub(super) fn check_type_params_are_used<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
generics: &ty::Generics,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue