1
Fork 0

Rollup merge of #97351 - b-naber:adt-const-params-structural-match-violation, r=michaelwoerister

Output correct type responsible for structural match violation

Previously we included the outermost type that caused a structural match violation in the error message and stated that that type must be annotated with `#[derive(Eq, PartialEq)]` even if it already had that annotation. This PR outputs the correct type in the error message.

Fixes https://github.com/rust-lang/rust/issues/97278
This commit is contained in:
Dylan DPC 2022-05-25 17:37:21 +02:00 committed by GitHub
commit 8a3ad4942c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 73 additions and 30 deletions

View file

@ -827,7 +827,9 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
);
}
if traits::search_for_structural_match_violation(param.span, tcx, ty).is_some() {
if let Some(non_structural_match_ty) =
traits::search_for_structural_match_violation(param.span, tcx, ty)
{
// We use the same error code in both branches, because this is really the same
// issue: we just special-case the message for type parameters to make it
// clearer.
@ -853,19 +855,23 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
)
.emit();
} else {
struct_span_err!(
let mut diag = struct_span_err!(
tcx.sess,
hir_ty.span,
E0741,
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
the type of a const parameter",
ty,
)
.span_label(
hir_ty.span,
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),
)
.emit();
non_structural_match_ty.ty,
);
if ty == non_structural_match_ty.ty {
diag.span_label(
hir_ty.span,
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),
);
}
diag.emit();
}
}
} else {