]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix ICE on missing pattern in while loop
authorLucas Ly Ba <lucas.ly-ba@outlook.com>
Mon, 13 Oct 2025 11:01:54 +0000 (11:01 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Nov 2025 14:58:19 +0000 (15:58 +0100)
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 <lucas.ly-ba@outlook.com>
gcc/rust/parse/rust-parse-impl.h
gcc/testsuite/rust/compile/issue-4162.rs [new file with mode: 0644]

index 62831ebbf10ef6193d6ff10f86751c602d0b90e6..64554f5e9e4919e26f49fe8929d92c8776093dc8 100644 (file)
@@ -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 (file)
index 0000000..c82bac6
--- /dev/null
@@ -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 }
+    {}
+}
+