Skip to content

Commit 14f3ef9

Browse files
authored
Rollup merge of #141070 - xizheyin:issue-140659, r=chenyukang
Do not emit help when shorthand from macro when suggest `?` or `expect` Fixes #140659 I didn't fully minimize the original bug, but I found a similar test case, and they have perhaps the same root cause. For the bug mentioned in #140659 , I also tested it locally and passed it. Jieyou has worked on this part before, maybe r? `@jieyouxu`
2 parents 59ad0cb + 1c17324 commit 14f3ef9

13 files changed

+73
-0
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20432043
};
20442044

20452045
let sugg = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
2046+
Some(_) if expr.span.from_expansion() => return false,
20462047
Some(ident) => format!(": {ident}{sugg}"),
20472048
None => sugg.to_string(),
20482049
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
trait Reencode {
2+
type Error;
3+
fn tag_index(&mut self, tag: u32) -> Result<u32, Self::Error>;
4+
}
5+
6+
struct Reencoder;
7+
impl Reencode for Reencoder {
8+
type Error = &'static str;
9+
fn tag_index(&mut self, tag: u32) -> Result<u32, Self::Error> {
10+
Ok(tag)
11+
}
12+
}
13+
14+
15+
enum Operator {
16+
Suspend { tag_index: u32 },
17+
}
18+
19+
enum Instruction {
20+
Suspend { tag_index: u32 },
21+
}
22+
23+
24+
macro_rules! for_each_operator {
25+
($m:ident) => {
26+
$m! {
27+
Suspend { tag_index: u32 } => visit_suspend
28+
}
29+
};
30+
}
31+
32+
33+
fn process<T: Reencode>(op: &Operator, reencoder: &mut T) -> Instruction {
34+
macro_rules! translate {
35+
(Suspend { tag_index: $ty:ty } => $visit:ident) => {
36+
match op {
37+
Operator::Suspend { tag_index } => {
38+
let tag_index = reencoder.tag_index(*tag_index);
39+
40+
// KEY POINT: Using field shorthand syntax where the compiler gets confused
41+
// Here tag_index is a Result<u32, E> but we're using it where u32 is expected
42+
Instruction::Suspend { tag_index } //~ ERROR mismatched types [E0308]
43+
}
44+
}
45+
};
46+
}
47+
48+
for_each_operator!(translate)
49+
}
50+
51+
fn main() {
52+
let mut reencoder = Reencoder;
53+
let op = Operator::Suspend { tag_index: 1 };
54+
55+
let _ = process(&op, &mut reencoder);
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/macro-shorthand-issue-140659.rs:42:44
3+
|
4+
LL | Instruction::Suspend { tag_index }
5+
| ^^^^^^^^^ expected `u32`, found `Result<u32, <T as Reencode>::Error>`
6+
...
7+
LL | for_each_operator!(translate)
8+
| ----------------------------- in this macro invocation
9+
|
10+
= note: expected type `u32`
11+
found enum `Result<u32, <T as Reencode>::Error>`
12+
= note: this error originates in the macro `translate` which comes from the expansion of the macro `for_each_operator` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)