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

commit r16-5349-geb88b7b6dc7c65955b7349caf5e6d458341921c8
Author: Lucas Ly Ba <[email protected]>
Date:   Mon Oct 13 11:01:54 2025 +0000

    gccrs: fix ICE on missing pattern in while loop
    
    Adds a proper check for missing patterns in while expressions.
    
    Fixes Rust-GCC#4162
    
    gcc/rust/ChangeLog:
    
            * 
parse/rust-parse-impl.h(Parser<ManagedTokenSource>::parse_while_let_loop_expr):
            Add check for missing pattern.
    
    gcc/testsuite/ChangeLog:
            * rust/compile/issue-4162.rs: New test.
    
    Signed-off-by: Lucas Ly Ba <[email protected]>

Diff:
---
 gcc/rust/parse/rust-parse-impl.h         | 9 ++++++++-
 gcc/testsuite/rust/compile/issue-4162.rs | 9 +++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 62831ebbf10e..64554f5e9e49 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -8234,7 +8234,14 @@ Parser<ManagedTokenSource>::parse_while_let_loop_expr (
   // parse predicate patterns
   std::vector<std::unique_ptr<AST::Pattern>> predicate_patterns
     = parse_match_arm_patterns (EQUAL);
-  // TODO: have to ensure that there is at least 1 pattern?
+  // ensure that there is at least 1 pattern
+  if (predicate_patterns.empty ())
+    {
+      Error error (lexer.peek_token ()->get_locus (),
+                  "should be at least 1 pattern");
+      add_error (std::move (error));
+      return nullptr;
+    }
 
   if (!skip_token (EQUAL))
     {
diff --git a/gcc/testsuite/rust/compile/issue-4162.rs 
b/gcc/testsuite/rust/compile/issue-4162.rs
new file mode 100644
index 000000000000..c82bac611d3e
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4162.rs
@@ -0,0 +1,9 @@
+pub fn main() {
+    while let  = 5
+    // { dg-error "should be at least 1 pattern" "" { target *-*-* } .-1 }
+    // { dg-error "failed to parse statement or expression in block 
expression" "" { target *-*-* } .-2 }
+    // { dg-error "unrecognised token .=. for start of item" "" { target *-*-* 
} .-3 }
+    // { dg-error "failed to parse item in crate" "" { target *-*-* } .-4 }
+    {}
+}
+

Reply via email to