Skip to content

Commit 2ec2014

Browse files
committed
Supress swapping lhs and rhs in equality suggestion in extern macro
Signed-off-by: xizheyin <[email protected]>
1 parent 460259d commit 2ec2014

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3533,6 +3533,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35333533
.must_apply_modulo_regions()
35343534
{
35353535
let sm = self.tcx.sess.source_map();
3536+
// If the span of rhs_expr is in an external macro,
3537+
// we should not suggest to swap the equality. See issue #139050
3538+
if rhs_expr.span.in_external_macro(sm) || lhs_expr.span.in_external_macro(sm) {
3539+
return;
3540+
}
3541+
35363542
if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_expr.span)
35373543
&& let Ok(lhs_snippet) = sm.span_to_snippet(lhs_expr.span)
35383544
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
use std::fmt::Debug;
5+
6+
pub fn foo<I: Iterator>(mut iter: I, value: &I::Item)
7+
where
8+
Item: Eq + Debug, //~ ERROR cannot find type `Item` in this scope [E0412]
9+
{
10+
debug_assert_eq!(iter.next(), Some(value)); //~ ERROR mismatched types [E0308]
11+
assert_eq!(iter.next(), Some(value)); //~ ERROR mismatched types [E0308]
12+
}
13+
fn main() {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0412]: cannot find type `Item` in this scope
2+
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:8: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:10:35
9+
|
10+
LL | debug_assert_eq!(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+
16+
error[E0308]: mismatched types
17+
--> $DIR/sugg-swap-equality-in-macro-issue-139050.rs:11:29
18+
|
19+
LL | assert_eq!(iter.next(), Some(value));
20+
| ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
21+
|
22+
= note: expected enum `Option<_>`
23+
found enum `Option<&_>`
24+
25+
error: aborting due to 3 previous errors
26+
27+
Some errors have detailed explanations: E0308, E0412.
28+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)