From: Owen Avery <[email protected]>

Fixes https://github.com/Rust-GCC/gccrs/issues/4155.

gcc/rust/ChangeLog:

        * parse/rust-parse-impl.h (Parser::parse_pattern): Ignore
        inner patterns which fail to parse.

gcc/testsuite/ChangeLog:

        * rust/compile/issue-4155.rs: New test.

Signed-off-by: Owen Avery <[email protected]>
---
 gcc/rust/parse/rust-parse-impl.h         | 10 ++++++++--
 gcc/testsuite/rust/compile/issue-4155.rs |  7 +++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-4155.rs

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 4a6717a3bf9..c54685d44ba 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -10483,16 +10483,22 @@ Parser<ManagedTokenSource>::parse_pattern ()
     return first;
 
   std::vector<std::unique_ptr<AST::Pattern>> alts;
-  alts.push_back (std::move (first));
+  if (first != nullptr)
+    alts.push_back (std::move (first));
 
   do
     {
       lexer.skip_token ();
-      alts.push_back (parse_pattern_no_alt ());
+      auto follow = parse_pattern_no_alt ();
+      if (follow != nullptr)
+       alts.push_back (std::move (follow));
     }
 
   while (lexer.peek_token ()->get_id () == PIPE);
 
+  if (alts.empty ())
+    return nullptr;
+
   /* alternates */
   return std::unique_ptr<AST::Pattern> (
     new AST::AltPattern (std::move (alts), start_locus));
diff --git a/gcc/testsuite/rust/compile/issue-4155.rs 
b/gcc/testsuite/rust/compile/issue-4155.rs
new file mode 100644
index 00000000000..9fae613c691
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4155.rs
@@ -0,0 +1,7 @@
+struct Bug {
+    inner: [(); match Vec::new {
+        f @  |n() => 1
+// { dg-error "failed to parse pattern to bind" "" { target *-*-* } .-1 }
+// { dg-error "unexpected token .|. in pattern" "" { target *-*-* } .-2 }
+    }],
+}
-- 
2.50.1

Reply via email to