Add MatchKind member to the Match expr for pretty printing & fmt

This commit is contained in:
Ross Smyth 2024-02-17 12:43:54 -05:00
parent 68a58f255a
commit 78b3bf98c3
17 changed files with 76 additions and 33 deletions

View file

@ -11,7 +11,7 @@ use crate::errors;
use crate::maybe_recover_from_interpolated_ty_qpath;
use ast::mut_visit::{noop_visit_expr, MutVisitor};
use ast::token::IdentIsRaw;
use ast::{CoroutineKind, ForLoopKind, GenBlockKind, Pat, Path, PathSegment};
use ast::{CoroutineKind, ForLoopKind, GenBlockKind, MatchKind, Pat, Path, PathSegment};
use core::mem;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
@ -1375,10 +1375,11 @@ impl<'a> Parser<'a> {
return Ok(self.mk_await_expr(self_arg, lo));
}
// Post-fix match
if self.eat_keyword(kw::Match) {
let match_span = self.prev_token.span;
self.psess.gated_spans.gate(sym::postfix_match, match_span);
return self.parse_match_block(lo, match_span, self_arg);
return self.parse_match_block(lo, match_span, self_arg, MatchKind::Postfix);
}
let fn_span_lo = self.token.span;
@ -2897,16 +2898,17 @@ impl<'a> Parser<'a> {
let match_span = self.prev_token.span;
let scrutinee = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
self.parse_match_block(match_span, match_span, scrutinee)
self.parse_match_block(match_span, match_span, scrutinee, MatchKind::Prefix)
}
/// Parses a `match expr { ... }` or a `expr.match { ... }` expression.
/// This is after the match token and scrutinee are eaten
/// Parses the block of a `match expr { ... }` or a `expr.match { ... }`
/// expression. This is after the match token and scrutinee are eaten
fn parse_match_block(
&mut self,
lo: Span,
match_span: Span,
scrutinee: P<Expr>,
match_kind: MatchKind,
) -> PResult<'a, P<Expr>> {
if let Err(mut e) = self.expect(&token::OpenDelim(Delimiter::Brace)) {
if self.token == token::Semi {
@ -2950,7 +2952,7 @@ impl<'a> Parser<'a> {
});
return Ok(self.mk_expr_with_attrs(
span,
ExprKind::Match(scrutinee, arms),
ExprKind::Match(scrutinee, arms, match_kind),
attrs,
));
}
@ -2958,7 +2960,7 @@ impl<'a> Parser<'a> {
}
let hi = self.token.span;
self.bump();
Ok(self.mk_expr_with_attrs(lo.to(hi), ExprKind::Match(scrutinee, arms), attrs))
Ok(self.mk_expr_with_attrs(lo.to(hi), ExprKind::Match(scrutinee, arms, match_kind), attrs))
}
/// Attempt to recover from match arm body with statements and no surrounding braces.
@ -3967,7 +3969,7 @@ impl MutVisitor for CondChecker<'_> {
| ExprKind::While(_, _, _)
| ExprKind::ForLoop { .. }
| ExprKind::Loop(_, _, _)
| ExprKind::Match(_, _)
| ExprKind::Match(_, _, _)
| ExprKind::Closure(_)
| ExprKind::Block(_, _)
| ExprKind::Gen(_, _, _)