Skip to content

Commit fbafa95

Browse files
authored
Unrolled build for #144266
Rollup merge of #144266 - xizheyin:139050, r=nnethercote Supress swapping lhs and rhs in equality suggestion in extern macro Fixes #139050
2 parents ed1d943 + 94d6601 commit fbafa95

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3636,7 +3636,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
36363636
.must_apply_modulo_regions()
36373637
{
36383638
let sm = self.tcx.sess.source_map();
3639-
if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_expr.span)
3639+
// If the span of rhs_expr or lhs_expr is in an external macro,
3640+
// we just suppress the suggestion. See issue #139050
3641+
if !rhs_expr.span.in_external_macro(sm)
3642+
&& !lhs_expr.span.in_external_macro(sm)
3643+
&& let Ok(rhs_snippet) = sm.span_to_snippet(rhs_expr.span)
36403644
&& let Ok(lhs_snippet) = sm.span_to_snippet(lhs_expr.span)
36413645
{
36423646
err.note(format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[macro_export]
2+
macro_rules! eq {
3+
(assert $a:expr, $b:expr) => {
4+
match (&$a, &$b) {
5+
(left_val, right_val) => {
6+
if !(*left_val == *right_val) {
7+
panic!(
8+
"assertion failed: `(left == right)`\n left: `{:?}`,\n right: `{:?}`",
9+
left_val, right_val
10+
);
11+
}
12+
}
13+
}
14+
};
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// if we use lhs == rhs in a macro, we should not suggest to swap the equality
2+
// because the origin span of lhs and rhs can not be found. See issue #139050
3+
4+
//@ aux-build:extern-macro-issue-139050.rs
5+
//@ aux-crate:ext=extern-macro-issue-139050.rs
6+
7+
extern crate ext;
8+
9+
use std::fmt::Debug;
10+
11+
macro_rules! eq_local {
12+
(assert $a:expr, $b:expr) => {
13+
match (&$a, &$b) {
14+
(left_val, right_val) => {
15+
if !(*left_val == *right_val) {
16+
//~^ ERROR mismatched types [E0308]
17+
panic!(
18+
"assertion failed: `(left == right)`\n left: `{:?}`,\n right: `{:?}`",
19+
left_val, right_val
20+
);
21+
}
22+
}
23+
}
24+
};
25+
}
26+
27+
pub fn foo<I: Iterator>(mut iter: I, value: &I::Item)
28+
where
29+
Item: Eq + Debug, //~ ERROR cannot find type `Item` in this scope [E0412]
30+
{
31+
ext::eq!(assert iter.next(), Some(value)); //~ ERROR mismatched types [E0308]
32+
eq_local!(assert iter.next(), Some(value));
33+
}
34+
fn main() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0412]: cannot find type `Item` in this scope
2+
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:29:5
3+
|
4+
LL | Item: Eq + Debug,
5+
| ^^^^ not found in this scope
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:31:5
9+
|
10+
LL | ext::eq!(assert iter.next(), Some(value));
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
12+
|
13+
= note: expected enum `Option<_>`
14+
found enum `Option<&_>`
15+
= note: this error originates in the macro `ext::eq` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:15:35
19+
|
20+
LL | if !(*left_val == *right_val) {
21+
| ^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
22+
...
23+
LL | eq_local!(assert iter.next(), Some(value));
24+
| ------------------------------------------ in this macro invocation
25+
|
26+
= note: expected enum `Option<_>`
27+
found enum `Option<&_>`
28+
= note: `Option<&<I as Iterator>::Item>` implements `PartialEq<Option<<I as Iterator>::Item>>`
29+
= note: this error originates in the macro `eq_local` (in Nightly builds, run with -Z macro-backtrace for more info)
30+
help: consider swapping the equality
31+
|
32+
LL - if !(*left_val == *right_val) {
33+
LL + if !(*right_val == *left_val) {
34+
|
35+
36+
error: aborting due to 3 previous errors
37+
38+
Some errors have detailed explanations: E0308, E0412.
39+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)