Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propagate the error from parsing enum variant to the parser and emit out #105098

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,10 @@ impl<'a> Parser<'a> {

Ok((Some(vr), TrailingToken::MaybeComma))
},
)
).map_err(|mut err|{
err.help("enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`");
err
})
}

/// Parses `struct Foo { ... }`.
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,10 @@ impl<'a> Parser<'a> {
Err(e) => {
// Parsing failed, therefore it must be something more serious
// than just a missing separator.
for xx in &e.children {
// propagate the help message from sub error 'e' to main error 'expect_err;
expect_err.children.push(xx.clone());
}
expect_err.emit();

e.cancel();
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/macros/syntax-error-recovery.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LL | $token $($inner)? = $value,
LL | values!(STRING(1) as (String) => cfg(test),);
| -------------------------------------------- in this macro invocation
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
= note: this error originates in the macro `values` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores token `(String)` and any following
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/parser/issue-101477-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error: unexpected `==`
|
LL | B == 2
| ^^ help: try using `=` instead
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error: expected item, found `==`
--> $DIR/issue-101477-enum.rs:6:7
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/parser/issue-103869.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum VecOrMap{
vec: Vec<usize>,
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
//~| HELP: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
//~| ERROR expected item, found `:`
map: HashMap<String,usize>
}

fn main() {}
16 changes: 16 additions & 0 deletions src/test/ui/parser/issue-103869.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
--> $DIR/issue-103869.rs:2:8
|
LL | vec: Vec<usize>,
| ^ expected one of `(`, `,`, `=`, `{`, or `}`
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error: expected item, found `:`
--> $DIR/issue-103869.rs:2:8
|
LL | vec: Vec<usize>,
| ^ expected item

error: aborting due to 2 previous errors

1 change: 1 addition & 0 deletions src/test/ui/parser/macro/issue-37113.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LL | $( $t, )*
LL | test_macro!(String,);
| -------------------- in this macro invocation
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
= note: this error originates in the macro `test_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/structs/struct-fn-in-definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum E {
//~^ ERROR functions are not allowed in enum definitions
//~| HELP unlike in C++, Java, and C#, functions are declared in `impl` blocks
//~| HELP see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information
//~| HELP enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
}

fn main() {}
1 change: 1 addition & 0 deletions src/test/ui/structs/struct-fn-in-definition.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ LL | fn foo() {}
|
= help: unlike in C++, Java, and C#, functions are declared in `impl` blocks
= help: see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error: aborting due to 3 previous errors