https://gcc.gnu.org/g:b23c49bcf991776ccd1b54c1af3e3c8390236b89

commit r17-5705-gb23c49bcf991776ccd1b54c1af3e3c8390236b89
Author: Utkarsh Bahuguna <[email protected]>
Date:   Sat Oct 3 22:14:52 2026 +0530

    gccrs: Report an error when pub is not followed by an item
    
    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]>

Diff:
---
 gcc/rust/parse/rust-parse-impl.hxx       | 12 +++++++++++-
 gcc/testsuite/rust/compile/issue-4940.rs |  6 ++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/parse/rust-parse-impl.hxx 
b/gcc/rust/parse/rust-parse-impl.hxx
index 38aa1b0ab36f..a04dd51d27a8 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 000000000000..21734c5ca7c9
--- /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" }

Reply via email to