Skip to content

Commit 43ff885

Browse files
committed
Add ui test macro-shorthand-issue-140659
Signed-off-by: xizheyin <[email protected]>
1 parent 414482f commit 43ff885

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// This should give the specific error message with the wrong help suggestion
49+
for_each_operator!(translate)
50+
}
51+
52+
fn main() {
53+
let mut reencoder = Reencoder;
54+
let op = Operator::Suspend { tag_index: 1 };
55+
56+
let _ = process(&op, &mut reencoder);
57+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
help: consider using `Result::expect` to unwrap the `Result<u32, <T as Reencode>::Error>` value, panicking if the value is a `Result::Err`
14+
|
15+
LL | }: tag_index.expect("REASON")
16+
| ++++++++++++++++++++++++++++
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)