https://gcc.gnu.org/g:7a01b70d1b35e769ad54f13a2e6602cad39796fd
commit r16-4859-g7a01b70d1b35e769ad54f13a2e6602cad39796fd Author: Owen Avery <[email protected]> Date: Sun Sep 14 18:05:35 2025 -0400 gccrs: Protect from errors in alternate pattern parsing 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]> Diff: --- gcc/rust/parse/rust-parse-impl.h | 10 ++++++++-- gcc/testsuite/rust/compile/issue-4155.rs | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 4a6717a3bf90..c54685d44ba6 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 000000000000..9fae613c691a --- /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 } + }], +}
