From c0c69cf7d56bb11e61180aa445b3b988cdc055ca Mon Sep 17 00:00:00 2001 From: xizheyin Date: Thu, 3 Apr 2025 18:42:13 +0800 Subject: [PATCH 1/2] Add ui test Signed-off-by: xizheyin --- .../dont-suggest-within-macro-issue-139251.rs | 14 +++++++ ...t-suggest-within-macro-issue-139251.stderr | 40 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/ui/macros/dont-suggest-within-macro-issue-139251.rs create mode 100644 tests/ui/macros/dont-suggest-within-macro-issue-139251.stderr diff --git a/tests/ui/macros/dont-suggest-within-macro-issue-139251.rs b/tests/ui/macros/dont-suggest-within-macro-issue-139251.rs new file mode 100644 index 00000000000..ceeb6e66950 --- /dev/null +++ b/tests/ui/macros/dont-suggest-within-macro-issue-139251.rs @@ -0,0 +1,14 @@ +#[macro_export] +macro_rules! is_equal { + ($left:expr, $right:expr) => { + $left == $right //~ ERROR can't compare `&{integer}` with `{integer}` [E0277] + }; +} + +fn main() { + let x = 1; + let y = &x; + assert!(y == 1); //~ ERROR can't compare `&{integer}` with `{integer}` [E0277] + assert_eq!(y, 2); //~ ERROR can't compare `&{integer}` with `{integer}` [E0277] + is_equal!(y, 2); +} diff --git a/tests/ui/macros/dont-suggest-within-macro-issue-139251.stderr b/tests/ui/macros/dont-suggest-within-macro-issue-139251.stderr new file mode 100644 index 00000000000..68c32470ab0 --- /dev/null +++ b/tests/ui/macros/dont-suggest-within-macro-issue-139251.stderr @@ -0,0 +1,40 @@ +error[E0277]: can't compare `&{integer}` with `{integer}` + --> $DIR/dont-suggest-within-macro-issue-139251.rs:11:15 + | +LL | assert!(y == 1); + | ^^ no implementation for `&{integer} == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}` +help: consider dereferencing here + | +LL | assert!(*y == 1); + | + + +error[E0277]: can't compare `&{integer}` with `{integer}` + --> $DIR/dont-suggest-within-macro-issue-139251.rs:12:5 + | +LL | assert_eq!(y, 2); + | ^^^^^^^^^^^^^^^^ no implementation for `&{integer} == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}` + = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: can't compare `&{integer}` with `{integer}` + --> $DIR/dont-suggest-within-macro-issue-139251.rs:4:15 + | +LL | $left == $right + | ^^ no implementation for `&{integer} == {integer}` +... +LL | is_equal!(y, 2); + | --------------- in this macro invocation + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}` + = note: this error originates in the macro `is_equal` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider dereferencing here + | +LL | is_equal!(*y, 2); + | + + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0277`. From 7753df37def68b78d1fb2cfb5ecd1e99d0cc1c10 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sat, 5 Apr 2025 11:19:09 +0800 Subject: [PATCH 2/2] Suppress suggestions on deref in macro when == is in extern macro Signed-off-by: xizheyin --- .../src/error_reporting/traits/suggestions.rs | 4 ++++ .../dont-suggest-within-macro-issue-139251.stderr | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index dc8022b95c3..55010f613aa 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -449,6 +449,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { err: &mut Diag<'_>, trait_pred: ty::PolyTraitPredicate<'tcx>, ) -> bool { + if obligation.cause.span.in_external_macro(self.infcx.tcx.sess.source_map()) { + return false; + } + let mut code = obligation.cause.code(); if let ObligationCauseCode::FunctionArg { arg_hir_id, call_hir_id, .. } = code && let Some(typeck_results) = &self.typeck_results diff --git a/tests/ui/macros/dont-suggest-within-macro-issue-139251.stderr b/tests/ui/macros/dont-suggest-within-macro-issue-139251.stderr index 68c32470ab0..e4e9b169994 100644 --- a/tests/ui/macros/dont-suggest-within-macro-issue-139251.stderr +++ b/tests/ui/macros/dont-suggest-within-macro-issue-139251.stderr @@ -17,6 +17,16 @@ LL | assert_eq!(y, 2); | ^^^^^^^^^^^^^^^^ no implementation for `&{integer} == {integer}` | = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}` + = help: the following other types implement trait `PartialEq`: + f128 + f16 + f32 + f64 + i128 + i16 + i32 + i64 + and 8 others = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: can't compare `&{integer}` with `{integer}`