Rollup merge of #137171 - makai410:swapping-e0277, r=compiler-errors

Suggest swapping equality on E0277

Closes: #132695 .
This commit is contained in:
Matthias Krüger 2025-03-01 05:49:51 +01:00 committed by GitHub
commit 51e0976441
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 88 additions and 0 deletions

View File

@ -2419,6 +2419,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&mut vec![],
&mut Default::default(),
);
self.suggest_swapping_lhs_and_rhs(
err,
obligation.predicate,
obligation.param_env,
obligation.cause.code(),
);
self.suggest_unsized_bound_if_applicable(err, obligation);
if let Some(span) = err.span.primary_span()
&& let Some(mut diag) =

View File

@ -4914,6 +4914,53 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
);
true
}
pub(crate) fn suggest_swapping_lhs_and_rhs<T>(
&self,
err: &mut Diag<'_>,
predicate: T,
param_env: ty::ParamEnv<'tcx>,
cause_code: &ObligationCauseCode<'tcx>,
) where
T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
{
let tcx = self.tcx;
let predicate = predicate.upcast(tcx);
match *cause_code {
ObligationCauseCode::BinOp {
lhs_hir_id,
rhs_hir_id: Some(rhs_hir_id),
rhs_span: Some(rhs_span),
..
} if let Some(typeck_results) = &self.typeck_results
&& let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
&& let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
&& let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
&& let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
{
if let Some(pred) = predicate.as_trait_clause()
&& tcx.is_lang_item(pred.def_id(), LangItem::PartialEq)
&& self
.infcx
.type_implements_trait(pred.def_id(), [rhs_ty, lhs_ty], param_env)
.must_apply_modulo_regions()
{
let lhs_span = tcx.hir().span(lhs_hir_id);
let sm = tcx.sess.source_map();
if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_span)
&& let Ok(lhs_snippet) = sm.span_to_snippet(lhs_span)
{
err.note(format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
err.multipart_suggestion(
"consider swapping the equality",
vec![(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)],
Applicability::MaybeIncorrect,
);
}
}
}
_ => {}
}
}
}
/// Add a hint to add a missing borrow or remove an unnecessary one.

View File

@ -0,0 +1,11 @@
struct T(String);
impl PartialEq<String> for T {
fn eq(&self, other: &String) -> bool {
&self.0 == other
}
}
fn main() {
String::from("Girls Band Cry") == T(String::from("Girls Band Cry")); //~ can't compare `String` with `T` [E0277]
}

View File

@ -0,0 +1,24 @@
error[E0277]: can't compare `String` with `T`
--> $DIR/partialeq_suggest_swap_on_e0277.rs:10:36
|
LL | String::from("Girls Band Cry") == T(String::from("Girls Band Cry"));
| ^^ no implementation for `String == T`
|
= help: the trait `PartialEq<T>` is not implemented for `String`
= help: the following other types implement trait `PartialEq<Rhs>`:
`String` implements `PartialEq<&str>`
`String` implements `PartialEq<ByteStr>`
`String` implements `PartialEq<ByteString>`
`String` implements `PartialEq<Cow<'_, str>>`
`String` implements `PartialEq<str>`
`String` implements `PartialEq`
= note: `T` implements `PartialEq<String>`
help: consider swapping the equality
|
LL - String::from("Girls Band Cry") == T(String::from("Girls Band Cry"));
LL + T(String::from("Girls Band Cry")) == String::from("Girls Band Cry");
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.