Skip to content

Commit b0812d9

Browse files
committed
Add test sugg-swap-equality-in-macro
Signed-off-by: xizheyin <[email protected]>
1 parent dc0bae1 commit b0812d9

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
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) { //~ ERROR mismatched types [E0308]
16+
panic!(
17+
"assertion failed: `(left == right)`\n left: `{:?}`,\n right: `{:?}`",
18+
left_val, right_val
19+
);
20+
}
21+
}
22+
}
23+
};
24+
}
25+
26+
pub fn foo<I: Iterator>(mut iter: I, value: &I::Item)
27+
where
28+
Item: Eq + Debug, //~ ERROR cannot find type `Item` in this scope [E0412]
29+
{
30+
ext::eq!(assert iter.next(), Some(value)); //~ ERROR mismatched types [E0308]
31+
eq_local!(assert iter.next(), Some(value));
32+
}
33+
fn main() {}
34+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error[E0412]: cannot find type `Item` in this scope
2+
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:28: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:30: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: `Option<&<I as Iterator>::Item>` implements `PartialEq<Option<<I as Iterator>::Item>>`
16+
= note: this error originates in the macro `ext::eq` (in Nightly builds, run with -Z macro-backtrace for more info)
17+
help: consider swapping the equality
18+
--> $DIR/auxiliary/extern-macro-issue-139050.rs:6:22
19+
|
20+
LL - if !(*left_val == *right_val) {
21+
LL + if !(*right_val == *left_val) {
22+
|
23+
24+
error[E0308]: mismatched types
25+
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:15:35
26+
|
27+
LL | if !(*left_val == *right_val) {
28+
| ^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
29+
...
30+
LL | eq_local!(assert iter.next(), Some(value));
31+
| ------------------------------------------ in this macro invocation
32+
|
33+
= note: expected enum `Option<_>`
34+
found enum `Option<&_>`
35+
= note: `Option<&<I as Iterator>::Item>` implements `PartialEq<Option<<I as Iterator>::Item>>`
36+
= note: this error originates in the macro `eq_local` (in Nightly builds, run with -Z macro-backtrace for more info)
37+
help: consider swapping the equality
38+
|
39+
LL - if !(*left_val == *right_val) {
40+
LL + if !(*right_val == *left_val) {
41+
|
42+
43+
error: aborting due to 3 previous errors
44+
45+
Some errors have detailed explanations: E0308, E0412.
46+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)