Skip to content

Commit b45db77

Browse files
powerboat9CohenArthur
authored andcommitted
Protect from errors in alternate pattern parsing
Fixes #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]>
1 parent 6256391 commit b45db77

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

gcc/rust/parse/rust-parse-impl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10483,16 +10483,22 @@ Parser<ManagedTokenSource>::parse_pattern ()
1048310483
return first;
1048410484

1048510485
std::vector<std::unique_ptr<AST::Pattern>> alts;
10486-
alts.push_back (std::move (first));
10486+
if (first != nullptr)
10487+
alts.push_back (std::move (first));
1048710488

1048810489
do
1048910490
{
1049010491
lexer.skip_token ();
10491-
alts.push_back (parse_pattern_no_alt ());
10492+
auto follow = parse_pattern_no_alt ();
10493+
if (follow != nullptr)
10494+
alts.push_back (std::move (follow));
1049210495
}
1049310496

1049410497
while (lexer.peek_token ()->get_id () == PIPE);
1049510498

10499+
if (alts.empty ())
10500+
return nullptr;
10501+
1049610502
/* alternates */
1049710503
return std::unique_ptr<AST::Pattern> (
1049810504
new AST::AltPattern (std::move (alts), start_locus));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct Bug {
2+
inner: [(); match Vec::new {
3+
f @ |n() => 1
4+
// { dg-error "failed to parse pattern to bind" "" { target *-*-* } .-1 }
5+
// { dg-error "unexpected token .|. in pattern" "" { target *-*-* } .-2 }
6+
}],
7+
}

0 commit comments

Comments
 (0)