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
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
6 changes: 6 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 🤔

// we should not suggest to swap the equality. See issue #139050
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.

if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_expr.span)
&& let Ok(lhs_snippet) = sm.span_to_snippet(lhs_expr.span)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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

use std::fmt::Debug;

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

error[E0308]: mismatched types
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:10:35
|
LL | debug_assert_eq!(iter.next(), Some(value));
| ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
|
= note: expected enum `Option<_>`
found enum `Option<&_>`

error[E0308]: mismatched types
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:11:29
|
LL | assert_eq!(iter.next(), Some(value));
| ^^^^^^^^^^^ 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

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