Skip to content

Commit b612d50

Browse files
committed
Add MatchKind member to the Match expr for pretty printing & fmt
1 parent 68a58f2 commit b612d50

File tree

17 files changed

+76
-33
lines changed

17 files changed

+76
-33
lines changed

compiler/rustc_ast/src/ast.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ pub enum ExprKind {
14361436
/// `'label: loop { block }`
14371437
Loop(P<Block>, Option<Label>, Span),
14381438
/// A `match` block.
1439-
Match(P<Expr>, ThinVec<Arm>),
1439+
Match(P<Expr>, ThinVec<Arm>, MatchKind),
14401440
/// A closure (e.g., `move |a, b, c| a + b + c`).
14411441
Closure(Box<Closure>),
14421442
/// A block (`'label: { ... }`).
@@ -1761,6 +1761,15 @@ pub enum StrStyle {
17611761
Raw(u8),
17621762
}
17631763

1764+
/// The kind of match expression
1765+
#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq)]
1766+
pub enum MatchKind {
1767+
/// match expr { ... }
1768+
Prefix,
1769+
/// expr.match { ... }
1770+
Postfix,
1771+
}
1772+
17641773
/// A literal in a meta item.
17651774
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
17661775
pub struct MetaItemLit {

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
14231423
visit_opt(label, |label| vis.visit_label(label));
14241424
vis.visit_span(span);
14251425
}
1426-
ExprKind::Match(expr, arms) => {
1426+
ExprKind::Match(expr, arms, _kind) => {
14271427
vis.visit_expr(expr);
14281428
arms.flat_map_in_place(|arm| vis.flat_map_arm(arm));
14291429
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
919919
visit_opt!(visitor, visit_label, opt_label);
920920
try_visit!(visitor.visit_block(block));
921921
}
922-
ExprKind::Match(subexpression, arms) => {
922+
ExprKind::Match(subexpression, arms, _kind) => {
923923
try_visit!(visitor.visit_expr(subexpression));
924924
walk_list!(visitor, visit_arm, arms);
925925
}

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
181181
)
182182
}),
183183
ExprKind::TryBlock(body) => self.lower_expr_try_block(body),
184-
ExprKind::Match(expr, arms) => hir::ExprKind::Match(
184+
ExprKind::Match(expr, arms, _) => hir::ExprKind::Match(
185185
self.lower_expr(expr),
186186
self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
187187
hir::MatchSource::Normal,

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::pp::Breaks::Inconsistent;
22
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
3-
use ast::ForLoopKind;
3+
use ast::{ForLoopKind, MatchKind};
44
use itertools::{Itertools, Position};
55
use rustc_ast::ptr::P;
66
use rustc_ast::token;
@@ -589,12 +589,22 @@ impl<'a> State<'a> {
589589
self.word_nbsp("loop");
590590
self.print_block_with_attrs(blk, attrs);
591591
}
592-
ast::ExprKind::Match(expr, arms) => {
592+
ast::ExprKind::Match(expr, arms, match_kind) => {
593593
self.cbox(0);
594594
self.ibox(0);
595-
self.word_nbsp("match");
596-
self.print_expr_as_cond(expr);
597-
self.space();
595+
596+
match match_kind {
597+
MatchKind::Prefix => {
598+
self.word_nbsp("match");
599+
self.print_expr_as_cond(expr);
600+
self.space();
601+
}
602+
MatchKind::Postfix => {
603+
self.print_expr_as_cond(expr);
604+
self.word_nbsp(".match");
605+
}
606+
}
607+
598608
self.bopen();
599609
self.print_inner_attributes_no_trailing_hardbreak(attrs);
600610
for arm in arms {

compiler/rustc_builtin_macros/src/assert/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
245245
ExprKind::Let(_, local_expr, _, _) => {
246246
self.manage_cond_expr(local_expr);
247247
}
248-
ExprKind::Match(local_expr, _) => {
248+
ExprKind::Match(local_expr, ..) => {
249249
self.manage_cond_expr(local_expr);
250250
}
251251
ExprKind::MethodCall(call) => {

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn cs_partial_cmp(
132132
// Reference: https://github.com/rust-lang/rust/pull/103659#issuecomment-1328126354
133133

134134
if !tag_then_data
135-
&& let ExprKind::Match(_, arms) = &mut expr1.kind
135+
&& let ExprKind::Match(_, arms, _) = &mut expr1.kind
136136
&& let Some(last) = arms.last_mut()
137137
&& let PatKind::Wild = last.pat.kind
138138
{

compiler/rustc_builtin_macros/src/deriving/debug.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::path_std;
44

5-
use ast::EnumDef;
6-
use rustc_ast::{self as ast, MetaItem};
5+
use rustc_ast::{self as ast, EnumDef, MetaItem};
76
use rustc_expand::base::{Annotatable, ExtCtxt};
87
use rustc_span::symbol::{sym, Ident, Symbol};
98
use rustc_span::Span;

compiler/rustc_expand/src/build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::base::ExtCtxt;
22
use rustc_ast::ptr::P;
3-
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp};
3+
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, MatchKind, PatKind, UnOp};
44
use rustc_ast::{attr, token, util::literal};
55
use rustc_span::source_map::Spanned;
66
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -524,7 +524,7 @@ impl<'a> ExtCtxt<'a> {
524524
}
525525

526526
pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: ThinVec<ast::Arm>) -> P<Expr> {
527-
self.expr(span, ast::ExprKind::Match(arg, arms))
527+
self.expr(span, ast::ExprKind::Match(arg, arms, MatchKind::Prefix))
528528
}
529529

530530
pub fn expr_if(

compiler/rustc_lint/src/unused.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ trait UnusedDelimLint {
865865
(iter, UnusedDelimsCtx::ForIterExpr, true, None, Some(body.span.lo()), true)
866866
}
867867

868-
Match(ref head, _) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
868+
Match(ref head, ..) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
869869
let left = e.span.lo() + rustc_span::BytePos(5);
870870
(head, UnusedDelimsCtx::MatchScrutineeExpr, true, Some(left), None, true)
871871
}
@@ -1133,7 +1133,7 @@ impl EarlyLintPass for UnusedParens {
11331133
}
11341134
return;
11351135
}
1136-
ExprKind::Match(ref _expr, ref arm) => {
1136+
ExprKind::Match(ref _expr, ref arm, _) => {
11371137
for a in arm {
11381138
if let Some(body) = &a.body {
11391139
self.check_unused_delims_expr(

compiler/rustc_parse/src/parser/expr.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::errors;
1111
use crate::maybe_recover_from_interpolated_ty_qpath;
1212
use ast::mut_visit::{noop_visit_expr, MutVisitor};
1313
use ast::token::IdentIsRaw;
14-
use ast::{CoroutineKind, ForLoopKind, GenBlockKind, Pat, Path, PathSegment};
14+
use ast::{CoroutineKind, ForLoopKind, GenBlockKind, MatchKind, Pat, Path, PathSegment};
1515
use core::mem;
1616
use rustc_ast::ptr::P;
1717
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
@@ -1375,10 +1375,11 @@ impl<'a> Parser<'a> {
13751375
return Ok(self.mk_await_expr(self_arg, lo));
13761376
}
13771377

1378+
// Post-fix match
13781379
if self.eat_keyword(kw::Match) {
13791380
let match_span = self.prev_token.span;
13801381
self.psess.gated_spans.gate(sym::postfix_match, match_span);
1381-
return self.parse_match_block(lo, match_span, self_arg);
1382+
return self.parse_match_block(lo, match_span, self_arg, MatchKind::Postfix);
13821383
}
13831384

13841385
let fn_span_lo = self.token.span;
@@ -2897,16 +2898,17 @@ impl<'a> Parser<'a> {
28972898
let match_span = self.prev_token.span;
28982899
let scrutinee = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
28992900

2900-
self.parse_match_block(match_span, match_span, scrutinee)
2901+
self.parse_match_block(match_span, match_span, scrutinee, MatchKind::Prefix)
29012902
}
29022903

2903-
/// Parses a `match expr { ... }` or a `expr.match { ... }` expression.
2904-
/// This is after the match token and scrutinee are eaten
2904+
/// Parses the block of a `match expr { ... }` or a `expr.match { ... }`
2905+
/// expression. This is after the match token and scrutinee are eaten
29052906
fn parse_match_block(
29062907
&mut self,
29072908
lo: Span,
29082909
match_span: Span,
29092910
scrutinee: P<Expr>,
2911+
match_kind: MatchKind,
29102912
) -> PResult<'a, P<Expr>> {
29112913
if let Err(mut e) = self.expect(&token::OpenDelim(Delimiter::Brace)) {
29122914
if self.token == token::Semi {
@@ -2950,15 +2952,15 @@ impl<'a> Parser<'a> {
29502952
});
29512953
return Ok(self.mk_expr_with_attrs(
29522954
span,
2953-
ExprKind::Match(scrutinee, arms),
2955+
ExprKind::Match(scrutinee, arms, match_kind),
29542956
attrs,
29552957
));
29562958
}
29572959
}
29582960
}
29592961
let hi = self.token.span;
29602962
self.bump();
2961-
Ok(self.mk_expr_with_attrs(lo.to(hi), ExprKind::Match(scrutinee, arms), attrs))
2963+
Ok(self.mk_expr_with_attrs(lo.to(hi), ExprKind::Match(scrutinee, arms, match_kind), attrs))
29622964
}
29632965

29642966
/// Attempt to recover from match arm body with statements and no surrounding braces.
@@ -3967,7 +3969,7 @@ impl MutVisitor for CondChecker<'_> {
39673969
| ExprKind::While(_, _, _)
39683970
| ExprKind::ForLoop { .. }
39693971
| ExprKind::Loop(_, _, _)
3970-
| ExprKind::Match(_, _)
3972+
| ExprKind::Match(_, _, _)
39713973
| ExprKind::Closure(_)
39723974
| ExprKind::Block(_, _)
39733975
| ExprKind::Gen(_, _, _)

src/tools/clippy/clippy_lints/src/redundant_else.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'ast> Visitor<'ast> for BreakVisitor {
105105
fn visit_expr(&mut self, expr: &'ast Expr) {
106106
self.is_break = match expr.kind {
107107
ExprKind::Break(..) | ExprKind::Continue(..) | ExprKind::Ret(..) => true,
108-
ExprKind::Match(_, ref arms) => arms.iter().all(|arm|
108+
ExprKind::Match(_, ref arms, _) => arms.iter().all(|arm|
109109
arm.body.is_none() || arm.body.as_deref().is_some_and(|body| self.check_expr(body))
110110
),
111111
ExprKind::If(_, ref then, Some(ref els)) => self.check_block(then) && self.check_expr(els),

src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ fn ident_difference_expr_with_base_location(
552552
| (Gen(_, _, _), Gen(_, _, _))
553553
| (Block(_, _), Block(_, _))
554554
| (Closure(_), Closure(_))
555-
| (Match(_, _), Match(_, _))
555+
| (Match(_, _, _), Match(_, _, _))
556556
| (Loop(_, _, _), Loop(_, _, _))
557557
| (ForLoop { .. }, ForLoop { .. })
558558
| (While(_, _, _), While(_, _, _))

src/tools/clippy/clippy_utils/src/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
198198
},
199199
(AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
200200
(Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
201-
(Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, eq_arm),
201+
(Match(ls, la, lkind), Match(rs, ra, rkind)) => (lkind == rkind) && eq_expr(ls, rs) && over(la, ra, eq_arm),
202202
(
203203
Closure(box ast::Closure {
204204
binder: lb,

src/tools/rustfmt/src/expr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cmp::min;
33

44
use itertools::Itertools;
55
use rustc_ast::token::{Delimiter, Lit, LitKind};
6-
use rustc_ast::{ast, ptr, token, ForLoopKind};
6+
use rustc_ast::{ast, MatchKind, ptr, token, ForLoopKind};
77
use rustc_span::{BytePos, Span};
88

99
use crate::chains::rewrite_chain;
@@ -170,8 +170,8 @@ pub(crate) fn format_expr(
170170
}
171171
}
172172
}
173-
ast::ExprKind::Match(ref cond, ref arms) => {
174-
rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs)
173+
ast::ExprKind::Match(ref cond, ref arms, kind) => {
174+
rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs, kind)
175175
}
176176
ast::ExprKind::Path(ref qself, ref path) => {
177177
rewrite_path(context, PathContext::Expr, qself, path, shape)
@@ -625,7 +625,7 @@ pub(crate) fn rewrite_cond(
625625
shape: Shape,
626626
) -> Option<String> {
627627
match expr.kind {
628-
ast::ExprKind::Match(ref cond, _) => {
628+
ast::ExprKind::Match(ref cond, _, MatchKind::Prefix) => {
629629
// `match `cond` {`
630630
let cond_shape = match context.config.indent_style() {
631631
IndentStyle::Visual => shape.shrink_left(6).and_then(|s| s.sub_width(2))?,

src/tools/rustfmt/src/matches.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::iter::repeat;
44

5-
use rustc_ast::{ast, ptr};
5+
use rustc_ast::{ast, MatchKind, ptr};
66
use rustc_span::{BytePos, Span};
77

88
use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
@@ -72,6 +72,8 @@ pub(crate) fn rewrite_match(
7272
shape: Shape,
7373
span: Span,
7474
attrs: &[ast::Attribute],
75+
// TODO: Use this
76+
_: MatchKind,
7577
) -> Option<String> {
7678
// Do not take the rhs overhead from the upper expressions into account
7779
// when rewriting match condition.

tests/pretty/postfix-match.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(postfix_match)]
2+
3+
fn main() {
4+
let val = Some(42);
5+
6+
val.match {
7+
Some(_) => 2,
8+
_ => 1
9+
};
10+
11+
12+
Some(2).match {
13+
Some(_) => true,
14+
None => false
15+
}.match {
16+
false => "ferris is cute",
17+
true => "I turn cats in to petted cats",
18+
}.match {
19+
_ => (),
20+
}
21+
}

0 commit comments

Comments
 (0)