Skip to content

Supress swapping lhs and rhs in equality suggestion in extern macro #144266

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
24 changes: 17 additions & 7 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3539,15 +3539,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.must_apply_modulo_regions()
{
let sm = self.tcx.sess.source_map();
if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_expr.span)
&& let Ok(lhs_snippet) = sm.span_to_snippet(lhs_expr.span)
// If the span of rhs_expr or lhs_expr is in an external macro,
// we should find the user-written code span. See issue #139050
if let Some(rhs_span) = rhs_expr.span.find_ancestor_not_from_extern_macro(sm)
&& let Some(lhs_span) = lhs_expr.span.find_ancestor_not_from_extern_macro(sm)
Comment on lines +3544 to +3545
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the == is from the extern macro, it will find the first ancestor that is not from extern macro by find_ancestor_not_from_extern_macro.

&& 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_expr.span, rhs_snippet), (rhs_expr.span, lhs_snippet)],
Applicability::MaybeIncorrect,
);
if rhs_span != lhs_span {
err.multipart_suggestion(
"consider swapping the equality",
vec![(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)],
Applicability::MaybeIncorrect,
);
} else {
// rhs_span and lhs_span are the same because it from extern macro.
// we should suggest to swap two arguments of the equality
err.span_help(rhs_span, "consider swapping two arguments of the equality");
}
Comment on lines +3556 to +3560
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Itrhs_span and lhs_span is the same, it is expand from extern macro, we use span_help to hint.

}
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/typeck/auxiliary/extern-macro-issue-139050.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[macro_export]
macro_rules! eq {
(assert $a:expr, $b:expr) => {
match (&$a, &$b) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
panic!(
"assertion failed: `(left == right)`\n left: `{:?}`,\n right: `{:?}`",
left_val, right_val
);
}
}
}
};
}
33 changes: 33 additions & 0 deletions tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// if we use lhs == rhs in a macro, we should not suggest to swap the equality
// because the origin span of lhs and rhs can not be found. See issue #139050

//@ aux-build:extern-macro-issue-139050.rs
//@ aux-crate:ext=extern-macro-issue-139050.rs

extern crate ext;

use std::fmt::Debug;

macro_rules! eq_local {
(assert $a:expr, $b:expr) => {
match (&$a, &$b) {
(left_val, right_val) => {
if !(*left_val == *right_val) { //~ ERROR mismatched types [E0308]
panic!(
"assertion failed: `(left == right)`\n left: `{:?}`,\n right: `{:?}`",
left_val, right_val
);
}
}
}
};
}

pub fn foo<I: Iterator>(mut iter: I, value: &I::Item)
where
Item: Eq + Debug, //~ ERROR cannot find type `Item` in this scope [E0412]
{
ext::eq!(assert iter.next(), Some(value)); //~ ERROR mismatched types [E0308]
eq_local!(assert iter.next(), Some(value));
}
fn main() {}
45 changes: 45 additions & 0 deletions tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
error[E0412]: cannot find type `Item` in this scope
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:28:5
|
LL | Item: Eq + Debug,
| ^^^^ not found in this scope

error[E0308]: mismatched types
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:30:5
|
LL | ext::eq!(assert iter.next(), Some(value));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
|
= note: expected enum `Option<_>`
found enum `Option<&_>`
= note: `Option<&<I as Iterator>::Item>` implements `PartialEq<Option<<I as Iterator>::Item>>`
help: consider swapping two arguments of the equality
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:30:5
|
LL | ext::eq!(assert iter.next(), Some(value));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `ext::eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:15:35
|
LL | if !(*left_val == *right_val) {
| ^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
...
LL | eq_local!(assert iter.next(), Some(value));
| ------------------------------------------ in this macro invocation
|
= note: expected enum `Option<_>`
found enum `Option<&_>`
= note: `Option<&<I as Iterator>::Item>` implements `PartialEq<Option<<I as Iterator>::Item>>`
= note: this error originates in the macro `eq_local` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider swapping the equality
|
LL - if !(*left_val == *right_val) {
LL + if !(*right_val == *left_val) {
|

error: aborting due to 3 previous errors

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