From: Lucas Ly Ba <[email protected]>

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]>
---
 gcc/rust/parse/rust-parse-impl.h         | 9 ++++++++-
 gcc/testsuite/rust/compile/issue-4162.rs | 9 +++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-4162.rs

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 62831ebbf10..64554f5e9e4 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 00000000000..c82bac611d3
--- /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 }
+    {}
+}
+
-- 
2.50.1

Reply via email to