From: Utkarsh Bahuguna <[email protected]>
parse_vis_item can fail without recording an error, so input like
`pub <` was silently dropped and compiled. Emit "expected item" there.
Fixes Rust-GCC/gccrs#4940
gcc/rust/ChangeLog:
* parse/rust-parse-impl.hxx (Parser::parse_item): Emit an error when
parse_vis_item fails without one.
gcc/testsuite/ChangeLog:
* rust/compile/issue-4940.rs: New test.
Signed-off-by: Utkarsh Bahuguna <[email protected]>
Tested on x86_64-pc-linux-gnu.
---
gcc/rust/parse/rust-parse-impl.hxx | 12 +++++++++++-
gcc/testsuite/rust/compile/issue-4940.rs | 6 ++++++
2 files changed, 17 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/rust/compile/issue-4940.rs
diff --git a/gcc/rust/parse/rust-parse-impl.hxx
b/gcc/rust/parse/rust-parse-impl.hxx
index 38aa1b0ab36..a04dd51d27a 100644
--- a/gcc/rust/parse/rust-parse-impl.hxx
+++ b/gcc/rust/parse/rust-parse-impl.hxx
@@ -399,9 +399,19 @@ Parser<ManagedTokenSource>::parse_item (bool
called_from_statement)
// if any of these (should be all possible VisItem prefixes), parse a
// VisItem
{
+ auto errors_before = error_table.size ();
auto vis_item = parse_vis_item (std::move (outer_attrs));
if (!vis_item)
- return Parse::Error::Item::make_malformed ();
+ {
+ // e.g. `pub` followed by something that isn't an item
+ if (error_table.size () == errors_before)
+ {
+ const_TokenPtr t = lexer.peek_token ();
+ add_error (Error (t->get_locus (), "expected item, found %qs",
+ t->get_token_description ()));
+ }
+ return Parse::Error::Item::make_malformed ();
+ }
return RType{std::move (vis_item)};
}
case SUPER:
diff --git a/gcc/testsuite/rust/compile/issue-4940.rs
b/gcc/testsuite/rust/compile/issue-4940.rs
new file mode 100644
index 00000000000..21734c5ca7c
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4940.rs
@@ -0,0 +1,6 @@
+#![feature(no_core)]
+#![no_core]
+
+fn main() {}
+
+pub <<><>< MAX_CONNECTIONS: u32 = 100; // { dg-error "expected item, found" }
--
2.55.0