]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Protect from errors in alternate pattern parsing
authorOwen Avery <powerboat9.gamer@gmail.com>
Sun, 14 Sep 2025 22:05:35 +0000 (18:05 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:59:13 +0000 (20:59 +0100)
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 <powerboat9.gamer@gmail.com>
gcc/rust/parse/rust-parse-impl.h
gcc/testsuite/rust/compile/issue-4155.rs [new file with mode: 0644]

index 4a6717a3bf90830f2063e79360f71abdccd8ba50..c54685d44ba6c2e3496a11d3fb8843506e03fd66 100644 (file)
@@ -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 (file)
index 0000000..9fae613
--- /dev/null
@@ -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 }
+    }],
+}