Skip to content

Commit 9cd1de5

Browse files
committed
suggest swapping equality on e0277
1 parent 5892187 commit 9cd1de5

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21262126
&mut vec![],
21272127
&mut Default::default(),
21282128
);
2129+
self.suggest_swapping_lhs_and_rhs(
2130+
err,
2131+
obligation.predicate,
2132+
obligation.param_env,
2133+
obligation.cause.code(),
2134+
);
21292135
self.suggest_unsized_bound_if_applicable(err, obligation);
21302136
if let Some(span) = err.span.primary_span()
21312137
&& let Some(mut diag) =

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

+47
Original file line numberDiff line numberDiff line change
@@ -4843,6 +4843,53 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
48434843
);
48444844
true
48454845
}
4846+
pub(crate) fn suggest_swapping_lhs_and_rhs<T>(
4847+
&self,
4848+
err: &mut Diag<'_>,
4849+
predicate: T,
4850+
param_env: ty::ParamEnv<'tcx>,
4851+
cause_code: &ObligationCauseCode<'tcx>,
4852+
) where
4853+
T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
4854+
{
4855+
let tcx = self.tcx;
4856+
let predicate = predicate.upcast(tcx);
4857+
match *cause_code {
4858+
ObligationCauseCode::BinOp {
4859+
lhs_hir_id,
4860+
rhs_hir_id: Some(rhs_hir_id),
4861+
rhs_span: Some(rhs_span),
4862+
..
4863+
} if let Some(typeck_results) = &self.typeck_results
4864+
&& let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
4865+
&& let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
4866+
&& let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
4867+
&& let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
4868+
{
4869+
if let Some(pred) = predicate.as_trait_clause()
4870+
&& tcx.is_lang_item(pred.def_id(), LangItem::PartialEq)
4871+
&& self
4872+
.infcx
4873+
.type_implements_trait(pred.def_id(), [rhs_ty, lhs_ty], param_env)
4874+
.must_apply_modulo_regions()
4875+
{
4876+
let lhs_span = tcx.hir().span(lhs_hir_id);
4877+
let sm = tcx.sess.source_map();
4878+
if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_span)
4879+
&& let Ok(lhs_snippet) = sm.span_to_snippet(lhs_span)
4880+
{
4881+
err.note(format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
4882+
err.multipart_suggestion(
4883+
"consider swapping the equality",
4884+
vec![(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)],
4885+
Applicability::MaybeIncorrect,
4886+
);
4887+
}
4888+
}
4889+
}
4890+
_ => {}
4891+
}
4892+
}
48464893
}
48474894

48484895
/// Add a hint to add a missing borrow or remove an unnecessary one.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct T(String);
2+
3+
impl PartialEq<String> for T {
4+
fn eq(&self, other: &String) -> bool {
5+
&self.0 == other
6+
}
7+
}
8+
9+
fn main() {
10+
String::from("Girls Band Cry") == T(String::from("Girls Band Cry")); //~ can't compare `String` with `T` [E0277]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0277]: can't compare `String` with `T`
2+
--> $DIR/partialeq_suggest_swap_on_e0277.rs:10:36
3+
|
4+
LL | String::from("Girls Band Cry") == T(String::from("Girls Band Cry"));
5+
| ^^ no implementation for `String == T`
6+
|
7+
= help: the trait `PartialEq<T>` is not implemented for `String`
8+
= help: the following other types implement trait `PartialEq<Rhs>`:
9+
`String` implements `PartialEq<&str>`
10+
`String` implements `PartialEq<ByteStr>`
11+
`String` implements `PartialEq<ByteString>`
12+
`String` implements `PartialEq<Cow<'_, str>>`
13+
`String` implements `PartialEq<str>`
14+
`String` implements `PartialEq`
15+
= note: `T` implements `PartialEq<String>`
16+
help: consider swapping the equality
17+
|
18+
LL - String::from("Girls Band Cry") == T(String::from("Girls Band Cry"));
19+
LL + T(String::from("Girls Band Cry")) == String::from("Girls Band Cry");
20+
|
21+
22+
error: aborting due to 1 previous error
23+
24+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)