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

Suggest swapping equality on E0277 #137171

Merged
merged 1 commit into from
Mar 1, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,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) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4843,6 +4843,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.
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/suggestions/partialeq_suggest_swap_on_e0277.rs
Original file line number Diff line number Diff line change
@@ -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]
}
24 changes: 24 additions & 0 deletions tests/ui/suggestions/partialeq_suggest_swap_on_e0277.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading