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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/f42ee1fd2b71c2865ed579c1547adbeb26a700ae

The commit has been mentioned in the following issue(s):
 - Rust-GCC/gccrs#4940: https://github.com/Rust-GCC/gccrs/issues/4940

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4951

 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 38aa1b0ab..a04dd51d2 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 000000000..21734c5ca
--- /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

Reply via email to