From 4d3f4efc9329dc070d2aed1efa089d44ba1a76d7 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Tue, 8 Jul 2025 22:33:02 +0100 Subject: [PATCH] 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 --- 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(-) create mode 100644 gcc/testsuite/rust/compile/issue-3874.rs diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index 0b5f27d36d2..4fe246d7a3a 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 3e50c46e58d..296821131b1 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 00000000000..ebce4b6da87 --- /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 } +} -- 2.47.2