Skip to content

Commit 78b3bf9

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

File tree

17 files changed

+76
-33
lines changed

17 files changed

+76
-33
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 10 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 15 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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(

0 commit comments

Comments
 (0)