Skip to content

Commit 0ff2f58

Browse files
committed
Suggest only when Rhs for PartialEq and PartialOrd is the same type as self
1 parent 0d2a000 commit 0ff2f58

File tree

1 file changed

+31
-29
lines changed
  • compiler/rustc_trait_selection/src/traits/error_reporting

1 file changed

+31
-29
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+31-29
Original file line numberDiff line numberDiff line change
@@ -2593,35 +2593,37 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
25932593
}
25942594

25952595
fn suggest_derive(&self, err: &mut Diagnostic, trait_pred: ty::PolyTraitPredicate<'tcx>) {
2596-
if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
2597-
let adt = match trait_pred.skip_binder().self_ty().ty_adt_def() {
2598-
Some(adt) if adt.did().is_local() => adt,
2599-
_ => return,
2600-
};
2601-
let can_derive = match diagnostic_name {
2602-
sym::Default => !adt.is_enum(),
2603-
sym::Eq
2604-
| sym::PartialEq
2605-
| sym::Ord
2606-
| sym::PartialOrd
2607-
| sym::Clone
2608-
| sym::Copy
2609-
| sym::Hash
2610-
| sym::Debug => true,
2611-
_ => false,
2612-
};
2613-
if can_derive {
2614-
err.span_suggestion_verbose(
2615-
self.tcx.def_span(adt.did()).shrink_to_lo(),
2616-
&format!(
2617-
"consider annotating `{}` with `#[derive({})]`",
2618-
trait_pred.skip_binder().self_ty().to_string(),
2619-
diagnostic_name.to_string(),
2620-
),
2621-
format!("#[derive({})]\n", diagnostic_name.to_string()),
2622-
Applicability::MaybeIncorrect,
2623-
);
2624-
}
2596+
let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
2597+
return;
2598+
};
2599+
let Some(self_ty) = trait_pred.self_ty().no_bound_vars() else {
2600+
return;
2601+
};
2602+
2603+
let adt = match self_ty.ty_adt_def() {
2604+
Some(adt) if adt.did().is_local() => adt,
2605+
_ => return,
2606+
};
2607+
let can_derive = match diagnostic_name {
2608+
sym::Default => !adt.is_enum(),
2609+
sym::PartialEq | sym::PartialOrd => {
2610+
let rhs_ty = trait_pred.skip_binder().trait_ref.substs.type_at(1);
2611+
self_ty == rhs_ty
2612+
}
2613+
sym::Eq | sym::Ord | sym::Clone | sym::Copy | sym::Hash | sym::Debug => true,
2614+
_ => false,
2615+
};
2616+
if can_derive {
2617+
err.span_suggestion_verbose(
2618+
self.tcx.def_span(adt.did()).shrink_to_lo(),
2619+
&format!(
2620+
"consider annotating `{}` with `#[derive({})]`",
2621+
trait_pred.skip_binder().self_ty().to_string(),
2622+
diagnostic_name.to_string(),
2623+
),
2624+
format!("#[derive({})]\n", diagnostic_name.to_string()),
2625+
Applicability::MaybeIncorrect,
2626+
);
26252627
}
26262628
}
26272629
}

0 commit comments

Comments
 (0)