Rollup merge of #133424 - Nadrieril:guard-patterns-parsing, r=fee1-dead
Parse guard patterns This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](https://github.com/rust-lang/rust/issues/129967)). This PR is extracted from https://github.com/rust-lang/rust/pull/129996 with minor modifications. cc `@max-niederman`
This commit is contained in:
commit
1868c8f66f
31 changed files with 328 additions and 81 deletions
|
@ -2631,7 +2631,7 @@ impl<'a> Parser<'a> {
|
|||
};
|
||||
self.bump(); // Eat `let` token
|
||||
let lo = self.prev_token.span;
|
||||
let pat = self.parse_pat_allow_top_alt(
|
||||
let pat = self.parse_pat_no_top_guard(
|
||||
None,
|
||||
RecoverComma::Yes,
|
||||
RecoverColon::Yes,
|
||||
|
@ -2778,7 +2778,7 @@ impl<'a> Parser<'a> {
|
|||
};
|
||||
// Try to parse the pattern `for ($PAT) in $EXPR`.
|
||||
let pat = match (
|
||||
self.parse_pat_allow_top_alt(
|
||||
self.parse_pat_allow_top_guard(
|
||||
None,
|
||||
RecoverComma::Yes,
|
||||
RecoverColon::Yes,
|
||||
|
@ -3241,7 +3241,7 @@ impl<'a> Parser<'a> {
|
|||
// then we should recover.
|
||||
let mut snapshot = this.create_snapshot_for_diagnostic();
|
||||
let pattern_follows = snapshot
|
||||
.parse_pat_allow_top_alt(
|
||||
.parse_pat_no_top_guard(
|
||||
None,
|
||||
RecoverComma::Yes,
|
||||
RecoverColon::Yes,
|
||||
|
@ -3315,43 +3315,37 @@ impl<'a> Parser<'a> {
|
|||
|
||||
fn parse_match_arm_pat_and_guard(&mut self) -> PResult<'a, (P<Pat>, Option<P<Expr>>)> {
|
||||
if self.token == token::OpenDelim(Delimiter::Parenthesis) {
|
||||
// Detect and recover from `($pat if $cond) => $arm`.
|
||||
let left = self.token.span;
|
||||
match self.parse_pat_allow_top_alt(
|
||||
let pat = self.parse_pat_no_top_guard(
|
||||
None,
|
||||
RecoverComma::Yes,
|
||||
RecoverColon::Yes,
|
||||
CommaRecoveryMode::EitherTupleOrPipe,
|
||||
) {
|
||||
Ok(pat) => Ok((pat, self.parse_match_arm_guard()?)),
|
||||
Err(err)
|
||||
if let prev_sp = self.prev_token.span
|
||||
&& let true = self.eat_keyword(kw::If) =>
|
||||
{
|
||||
// We know for certain we've found `($pat if` so far.
|
||||
let mut cond = match self.parse_match_guard_condition() {
|
||||
Ok(cond) => cond,
|
||||
Err(cond_err) => {
|
||||
cond_err.cancel();
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
err.cancel();
|
||||
CondChecker::new(self).visit_expr(&mut cond);
|
||||
self.eat_to_tokens(&[&token::CloseDelim(Delimiter::Parenthesis)]);
|
||||
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
|
||||
let right = self.prev_token.span;
|
||||
self.dcx().emit_err(errors::ParenthesesInMatchPat {
|
||||
span: vec![left, right],
|
||||
sugg: errors::ParenthesesInMatchPatSugg { left, right },
|
||||
});
|
||||
Ok((self.mk_pat(left.to(prev_sp), ast::PatKind::Wild), Some(cond)))
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
)?;
|
||||
if let ast::PatKind::Paren(subpat) = &pat.kind
|
||||
&& let ast::PatKind::Guard(..) = &subpat.kind
|
||||
{
|
||||
// Detect and recover from `($pat if $cond) => $arm`.
|
||||
// FIXME(guard_patterns): convert this to a normal guard instead
|
||||
let span = pat.span;
|
||||
let ast::PatKind::Paren(subpat) = pat.into_inner().kind else { unreachable!() };
|
||||
let ast::PatKind::Guard(_, mut cond) = subpat.into_inner().kind else {
|
||||
unreachable!()
|
||||
};
|
||||
self.psess.gated_spans.ungate_last(sym::guard_patterns, cond.span);
|
||||
CondChecker::new(self).visit_expr(&mut cond);
|
||||
let right = self.prev_token.span;
|
||||
self.dcx().emit_err(errors::ParenthesesInMatchPat {
|
||||
span: vec![left, right],
|
||||
sugg: errors::ParenthesesInMatchPatSugg { left, right },
|
||||
});
|
||||
Ok((self.mk_pat(span, ast::PatKind::Wild), Some(cond)))
|
||||
} else {
|
||||
Ok((pat, self.parse_match_arm_guard()?))
|
||||
}
|
||||
} else {
|
||||
// Regular parser flow:
|
||||
let pat = self.parse_pat_allow_top_alt(
|
||||
let pat = self.parse_pat_no_top_guard(
|
||||
None,
|
||||
RecoverComma::Yes,
|
||||
RecoverColon::Yes,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue