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 1 commit into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Jul 21, 2025

Fixes #139050

I have tried to find the initial call point via find_oldest_ancestor_in_same_ctxt, but it terminates inside the debug_assert_eq macro and doesn't get the span we were expecting.

For this reason #139316 (comment) , the test can not show the diff.
I test it locally.
In master branch:

error[E0412]: cannot find type `Item` in this scope
 --> src/main.rs:5:5
  |
5 |     Item: Eq + Debug, //~ ERROR cannot find type `Item` in this scope [E0412]
  |     ^^^^ not found in this scope

error[E0308]: mismatched types
 --> src/main.rs:7:35
  |
7 |     debug_assert_eq!(iter.next(), Some(value)); //~ ERROR  mismatched types [E0308]
  |                                   ^^^^^^^^^^^ 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 the equality
 --> /home/nju/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:46:22
  |
46-                 if !(*left_val == *right_val) {
46+                 if !(*right_val == *left_val) {
  |

error[E0308]: mismatched types
 --> src/main.rs:8:29
  |
8 |     assert_eq!(iter.next(), Some(value)); //~ ERROR  mismatched types [E0308]
  |                             ^^^^^^^^^^^ 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 the equality
 --> /home/nju/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:46:22
  |
46-                 if !(*left_val == *right_val) {
46+                 if !(*right_val == *left_val) {
  |

error: aborting due to 3 previous errors

In this branch:

error[E0412]: cannot find type `Item` in this scope
 --> src/main.rs:5:5
  |
5 |     Item: Eq + Debug, //~ ERROR cannot find type `Item` in this scope [E0412]
  |     ^^^^ not found in this scope

error[E0308]: mismatched types
 --> src/main.rs:7:35
  |
7 |     debug_assert_eq!(iter.next(), Some(value)); //~ ERROR  mismatched types [E0308]
  |                                   ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
  |
  = note: expected enum `Option<_>`
             found enum `Option<&_>`

error[E0308]: mismatched types
 --> src/main.rs:8:29
  |
8 |     assert_eq!(iter.next(), Some(value)); //~ ERROR  mismatched types [E0308]
  |                             ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
  |
  = note: expected enum `Option<_>`
             found enum `Option<&_>`

error: aborting due to 3 previous errors

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 21, 2025
@xizheyin xizheyin changed the title 139050 Supress swapping lhs and rhs in equality suggestion in extern macro Jul 21, 2025
@compiler-errors
Copy link
Member

If the test can't actually demonstrate the change, then there's no reason to make this into two commits. Please squash this into one.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 21, 2025
Copy link
Contributor Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

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

Pretty fast review!

@rustbot ready

if rhs_expr.span.in_external_macro(sm) || lhs_expr.span.in_external_macro(sm) {
return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I checked both.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 21, 2025
@@ -3533,6 +3533,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.must_apply_modulo_regions()
{
let sm = self.tcx.sess.source_map();
// If the span of rhs_expr or lhs_expr is in an external macro,
Copy link
Member

Choose a reason for hiding this comment

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

There is another copy of suggest_swapping_lhs_and_rhs in rustc_trait_selection. Could you please add this check to that, too, since I think it suffers from the same issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'll also look for a test case for that tomorrow.

Copy link
Member

Choose a reason for hiding this comment

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

I don't really think it needs a test case, but if you want to add one it's probably something like:

upstream crate:

macro_rules! eq {
    ($a:expr) => { $a == () }
}

downstream crate:

struct Foo;

impl PartialEq<Foo> for () {
    fn eq(&self, _: &Foo) -> bool { todo!() }
}

fn main() {
    let _ = eq!(Foo);
}

Pay attention to the fact that it's a trait error and there may already be some macro suppression going on there 🤔

@xizheyin
Copy link
Contributor Author

I submitted #144268. We can find the correct span with the newly added methods, thus not needing to suppress this suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Misleading compiler suggestion: Swapp the equality in rustlib due to mismatched types in user code
3 participants