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

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
@xizheyin xizheyin force-pushed the 139050 branch 2 times, most recently from 2ec2014 to 749762a Compare July 21, 2025 16:16
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
@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.

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.

Now, we could reproduce the bug.
@rustbot ready

Comment on lines +3544 to +3545
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)
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.

Comment on lines +3556 to +3560
} 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");
}
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.

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test [ui] tests/ui/zero-sized/zero-sized-btreemap-insert.rs ... ok

failures:

---- [ui] tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.rs stdout ----
Saved the actual stderr to `/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/typeck/sugg-swap-equality-in-macro-issue-139050/sugg-swap-equality-in-macro-issue-139050.stderr`
diff of stderr:

26 LL |                 if !(*left_val == *right_val) {
27    |                                   ^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
28 ...
- LL |     eq_local!(assert iter.next(), Some(value)); 
+ LL |     eq_local!(assert iter.next(), Some(value));
30    |     ------------------------------------------ in this macro invocation
31    |
32    = note: expected enum `Option<_>`


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args typeck/sugg-swap-equality-in-macro-issue-139050.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/typeck/sugg-swap-equality-in-macro-issue-139050" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/typeck/sugg-swap-equality-in-macro-issue-139050/auxiliary" "--extern" "ext=/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/typeck/sugg-swap-equality-in-macro-issue-139050/auxiliary/libextern_macro_issue_139050.so"
stdout: none
--- stderr -------------------------------
error[E0412]: cannot find type `Item` in this scope
##[error]  --> /checkout/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.rs:28:5
   |
LL |     Item: Eq + Debug, //~ ERROR cannot find type `Item` in this scope [E0412]
   |     ^^^^ not found in this scope

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.rs:30:5
   |
LL |     ext::eq!(assert 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 two arguments of the equality
  --> /checkout/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.rs:30:5
   |
LL |     ext::eq!(assert iter.next(), Some(value)); //~ ERROR  mismatched types [E0308]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the macro `ext::eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.rs:15:35
   |
LL |                 if !(*left_val == *right_val) { //~ ERROR  mismatched types [E0308]
   |                                   ^^^^^^^^^^ 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) { //~ ERROR  mismatched types [E0308]
LL +                 if !(*right_val == *left_val) { //~ ERROR  mismatched types [E0308]
   |

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0308, E0412.

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
4 participants