Skip to content

Commit 0e19fb9

Browse files
author
Yiming Lei
committed
While parsing enum variant, the error message always disappear
Because the error message that emit out is from main error of parser The information of enum variant disappears while parsing enum variant with error We only check the syntax of expecting token, i.e, in case #103869 It will error it without telling the message that this error is from pasring enum variant. Propagate the sub-error from parsing enum variant to the main error of parser by chaining it with map_err Check the sub-error before emitting the main error of parser and attach it. Fix #103869
1 parent 90711a8 commit 0e19fb9

File tree

9 files changed

+39
-1
lines changed

9 files changed

+39
-1
lines changed

compiler/rustc_parse/src/parser/item.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,10 @@ impl<'a> Parser<'a> {
14141414

14151415
Ok((Some(vr), TrailingToken::MaybeComma))
14161416
},
1417-
)
1417+
).map_err(|mut err|{
1418+
err.help("enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`");
1419+
err
1420+
})
14181421
}
14191422

14201423
/// Parses `struct Foo { ... }`.

compiler/rustc_parse/src/parser/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,10 @@ impl<'a> Parser<'a> {
943943
Err(e) => {
944944
// Parsing failed, therefore it must be something more serious
945945
// than just a missing separator.
946+
for xx in &e.children {
947+
// propagate the help message from sub error 'e' to main error 'expect_err;
948+
expect_err.children.push(xx.clone());
949+
}
946950
expect_err.emit();
947951

948952
e.cancel();

src/test/ui/macros/syntax-error-recovery.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LL | $token $($inner)? = $value,
77
LL | values!(STRING(1) as (String) => cfg(test),);
88
| -------------------------------------------- in this macro invocation
99
|
10+
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
1011
= note: this error originates in the macro `values` (in Nightly builds, run with -Z macro-backtrace for more info)
1112

1213
error: macro expansion ignores token `(String)` and any following

src/test/ui/parser/issue-101477-enum.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: unexpected `==`
33
|
44
LL | B == 2
55
| ^^ help: try using `=` instead
6+
|
7+
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
68

79
error: expected item, found `==`
810
--> $DIR/issue-101477-enum.rs:6:7

src/test/ui/parser/issue-103869.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
enum VecOrMap{
2+
vec: Vec<usize>,
3+
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
4+
//~| HELP: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
5+
//~| ERROR expected item, found `:`
6+
map: HashMap<String,usize>
7+
}
8+
9+
fn main() {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
2+
--> $DIR/issue-103869.rs:2:8
3+
|
4+
LL | vec: Vec<usize>,
5+
| ^ expected one of `(`, `,`, `=`, `{`, or `}`
6+
|
7+
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
8+
9+
error: expected item, found `:`
10+
--> $DIR/issue-103869.rs:2:8
11+
|
12+
LL | vec: Vec<usize>,
13+
| ^ expected item
14+
15+
error: aborting due to 2 previous errors
16+

src/test/ui/parser/macro/issue-37113.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LL | $( $t, )*
99
LL | test_macro!(String,);
1010
| -------------------- in this macro invocation
1111
|
12+
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
1213
= note: this error originates in the macro `test_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
1314

1415
error: aborting due to previous error

src/test/ui/structs/struct-fn-in-definition.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum E {
2828
//~^ ERROR functions are not allowed in enum definitions
2929
//~| HELP unlike in C++, Java, and C#, functions are declared in `impl` blocks
3030
//~| HELP see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information
31+
//~| HELP enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
3132
}
3233

3334
fn main() {}

src/test/ui/structs/struct-fn-in-definition.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ LL | fn foo() {}
3333
|
3434
= help: unlike in C++, Java, and C#, functions are declared in `impl` blocks
3535
= help: see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information
36+
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
3637

3738
error: aborting due to 3 previous errors
3839

0 commit comments

Comments
 (0)