Skip to content
Open
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
14 changes: 13 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3927,10 +3927,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
rhs_expr: &'tcx hir::Expr<'tcx>,
lhs_expr: &'tcx hir::Expr<'tcx>,
) {
// An unsuffixed literal leaves an inference variable behind (`{integer}`), which would
// make the swapped impl only *maybe* apply. Check it against the type the literal falls
// back to instead.
let (swapped_self_ty, swapped_rhs_ty) = self.resolve_numeric_literals_with_default(
self.deeply_resolve_ignoring_regions((rhs_ty, lhs_ty)),
);
if let Some(partial_eq_def_id) = self.infcx.tcx.lang_items().partial_eq_trait()
// An error type satisfies every bound, which says nothing about a swap.
&& !(swapped_self_ty, swapped_rhs_ty).references_error()
&& self
.infcx
.type_implements_trait(partial_eq_def_id, [rhs_ty, lhs_ty], self.param_env)
.type_implements_trait(
partial_eq_def_id,
[swapped_self_ty, swapped_rhs_ty],
self.param_env,
)
.must_apply_modulo_regions()
{
let sm = self.tcx.sess.source_map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6694,19 +6694,36 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&& let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
&& let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
{
// An unsuffixed literal leaves an inference variable behind (`{integer}`), which
// would make the swapped impl only *maybe* apply. Check it against the type the
// literal falls back to instead.
let (swapped_self_ty, swapped_rhs_ty) =
self.infcx.resolve_numeric_literals_with_default(
self.infcx.deeply_resolve_ignoring_regions((rhs_ty, lhs_ty)),
);
if let Some(pred) = predicate.as_trait_clause()
&& tcx.is_lang_item(pred.def_id(), LangItem::PartialEq)
// An error type satisfies every bound, which says nothing about a swap.
&& !(swapped_self_ty, swapped_rhs_ty).references_error()
&& self
.infcx
.type_implements_trait(pred.def_id(), [rhs_ty, lhs_ty], param_env)
.type_implements_trait(
pred.def_id(),
[swapped_self_ty, swapped_rhs_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)
err.note(format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
// The operands of e.g. `assert_eq!` are compared inside of the macro, where
// there is nothing for the user to swap (#139050), so only the note is given.
if !lhs_span.in_external_macro(sm)
&& !rhs_span.in_external_macro(sm)
&& 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)],
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/suggestions/partialeq_suggest_swap_unsuffixed_literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// The operands of `==` can be swapped when only the reversed `PartialEq` impl exists. Check
// that this is still noticed when unsuffixed literals leave `{integer}` in the operand types,
// and that a comparison inside of an external macro gets the note without a suggestion.

struct T(i32);

impl PartialEq<i32> for T {
fn eq(&self, other: &i32) -> bool {
&self.0 == other
}
}

struct W<A>(A);

impl<A: PartialEq<B>, B> PartialEq<Option<B>> for W<A> {
fn eq(&self, other: &Option<B>) -> bool {
other.as_ref().is_some_and(|other| &self.0 == other)
}
}

fn main() {
let v = vec![1, 2, 3];
let a = [1, 2, 3];
let _ = a == v; //~ ERROR can't compare `[{integer}; 3]` with `Vec<{integer}>` [E0277]
let _ = a != v; //~ ERROR can't compare `[{integer}; 3]` with `Vec<{integer}>` [E0277]
assert_eq!(a, v); //~ ERROR can't compare `[{integer}; 3]` with `Vec<{integer}>` [E0277]
let _ = 4 == T(4); //~ ERROR can't compare `{integer}` with `T` [E0277]
let _ = Some(1) == W(1); //~ ERROR mismatched types [E0308]
// `T` can't be compared with a float, so there is nothing to swap.
let _ = 1.5 == T(4); //~ ERROR can't compare `{float}` with `T` [E0277]
}
118 changes: 118 additions & 0 deletions tests/ui/suggestions/partialeq_suggest_swap_unsuffixed_literal.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
error[E0277]: can't compare `[{integer}; 3]` with `Vec<{integer}>`
--> $DIR/partialeq_suggest_swap_unsuffixed_literal.rs:24:15
|
LL | let _ = a == v;
| ^^ no implementation for `[{integer}; 3] == Vec<{integer}>`
|
= help: the trait `PartialEq<Vec<{integer}>>` is not implemented for `[{integer}; 3]`
= note: `Vec<{integer}>` implements `PartialEq<[{integer}; 3]>`
help: consider dereferencing here
|
LL | let _ = a == *v;
| +
help: consider swapping the equality
|
LL - let _ = a == v;
LL + let _ = v == a;
|

error[E0277]: can't compare `[{integer}; 3]` with `Vec<{integer}>`
--> $DIR/partialeq_suggest_swap_unsuffixed_literal.rs:25:15
|
LL | let _ = a != v;
| ^^ no implementation for `[{integer}; 3] == Vec<{integer}>`
|
= help: the trait `PartialEq<Vec<{integer}>>` is not implemented for `[{integer}; 3]`
= note: `Vec<{integer}>` implements `PartialEq<[{integer}; 3]>`
help: consider dereferencing here
|
LL | let _ = a != *v;
| +
help: consider swapping the equality
|
LL - let _ = a != v;
LL + let _ = v != a;
|

error[E0277]: can't compare `[{integer}; 3]` with `Vec<{integer}>`
--> $DIR/partialeq_suggest_swap_unsuffixed_literal.rs:26:5
|
LL | assert_eq!(a, v);
| ^^^^^^^^^^^^^^^^ no implementation for `[{integer}; 3] == Vec<{integer}>`
|
= help: the trait `PartialEq<Vec<{integer}>>` is not implemented for `[{integer}; 3]`
= help: the following other types implement trait `PartialEq<Rhs>`:
`&[T]` implements `PartialEq<Cow<'_, [U]>>`
`&[T]` implements `PartialEq<Vec<U, A>>`
`&[T]` implements `PartialEq<[U; N]>`
`&[u8; N]` implements `PartialEq<ByteStr>`
`&[u8; N]` implements `PartialEq<ByteString>`
`&[u8]` implements `PartialEq<ByteStr>`
`&[u8]` implements `PartialEq<ByteString>`
`&mut [T]` implements `PartialEq<Cow<'_, [U]>>`
and 13 others
= note: `Vec<{integer}>` implements `PartialEq<[{integer}; 3]>`

error[E0277]: can't compare `{integer}` with `T`
--> $DIR/partialeq_suggest_swap_unsuffixed_literal.rs:27:15
|
LL | let _ = 4 == T(4);
| ^^ no implementation for `{integer} == T`
|
= help: the trait `PartialEq<T>` is not implemented for `{integer}`
= help: the following other types implement trait `PartialEq<Rhs>`:
f128
f16
f32
f64
i128
i16
i32
i64
and 8 others
= note: `T` implements `PartialEq<{integer}>`
help: consider swapping the equality
|
LL - let _ = 4 == T(4);
LL + let _ = T(4) == 4;
|

error[E0308]: mismatched types
--> $DIR/partialeq_suggest_swap_unsuffixed_literal.rs:28:24
|
LL | let _ = Some(1) == W(1);
| ------- ^^^^ expected `Option<{integer}>`, found `W<{integer}>`
| |
| expected because this is `Option<{integer}>`
|
= note: expected enum `Option<{integer}>`
found struct `W<{integer}>`
= note: `W<{integer}>` implements `PartialEq<Option<{integer}>>`
help: consider swapping the equality
|
LL - let _ = Some(1) == W(1);
LL + let _ = W(1) == Some(1);
|

error[E0277]: can't compare `{float}` with `T`
--> $DIR/partialeq_suggest_swap_unsuffixed_literal.rs:30:17
|
LL | let _ = 1.5 == T(4);
| ^^ no implementation for `{float} == T`
|
= help: the trait `PartialEq<T>` is not implemented for `{float}`
= help: the following other types implement trait `PartialEq<Rhs>`:
f128
f16
f32
f64
i128
i16
i32
i64
and 8 others

error: aborting due to 6 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
Loading