https://gcc.gnu.org/g:4d3f4efc9329dc070d2aed1efa089d44ba1a76d7

commit r16-2924-g4d3f4efc9329dc070d2aed1efa089d44ba1a76d7
Author: Philip Herron <herron.phi...@googlemail.com>
Date:   Tue Jul 8 22:33:02 2025 +0100

    gccrs: Fix ice with invalid borrow expression
    
    This is an invalid test case but we just need a guard for the missing
    borrow expression.
    
    Fixes Rust-GCC#3874
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast-collector.cc (TokenCollector::visit): check for 
missing borrow
            * ast/rust-expr.h: add helper
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/issue-3874.rs: New test.
    
    Signed-off-by: Philip Herron <herron.phi...@googlemail.com>

Diff:
---
 gcc/rust/ast/rust-ast-collector.cc       | 3 ++-
 gcc/rust/ast/rust-expr.h                 | 2 ++
 gcc/testsuite/rust/compile/issue-3874.rs | 4 ++++
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index 0b5f27d36d2c..4fe246d7a3ad 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -870,7 +870,8 @@ TokenCollector::visit (BorrowExpr &expr)
        push (Rust::Token::make (MUT, UNDEF_LOCATION));
     }
 
-  visit (expr.get_borrowed_expr ());
+  if (expr.has_borrow_expr ())
+    visit (expr.get_borrowed_expr ());
 }
 
 void
diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 3e50c46e58de..296821131b1e 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -395,6 +395,8 @@ public:
     return *main_or_left_expr;
   }
 
+  bool has_borrow_expr () const { return main_or_left_expr != nullptr; }
+
   bool get_is_mut () const { return mutability == Mutability::Mut; }
 
   Mutability get_mutability () const { return mutability; }
diff --git a/gcc/testsuite/rust/compile/issue-3874.rs 
b/gcc/testsuite/rust/compile/issue-3874.rs
new file mode 100644
index 000000000000..ebce4b6da87d
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3874.rs
@@ -0,0 +1,4 @@
+fn wow(){
+    &#[serde]
+    // { dg-error "found unexpected token .#. in null denotation" "" { target 
*-*-* } .-1 }
+}

Reply via email to