Skip to content

Commit 02e377e

Browse files
committed
Add MatchKind member to the Match expr for pretty printing & fmt
1 parent 62f97c3 commit 02e377e

File tree

20 files changed

+99
-40
lines changed

20 files changed

+99
-40
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ pub enum ExprKind {
14371437
/// `'label: loop { block }`
14381438
Loop(P<Block>, Option<Label>, Span),
14391439
/// A `match` block.
1440-
Match(P<Expr>, ThinVec<Arm>),
1440+
Match(P<Expr>, ThinVec<Arm>, MatchKind),
14411441
/// A closure (e.g., `move |a, b, c| a + b + c`).
14421442
Closure(Box<Closure>),
14431443
/// A block (`'label: { ... }`).
@@ -1759,6 +1759,15 @@ pub enum StrStyle {
17591759
Raw(u8),
17601760
}
17611761

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

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
14221422
visit_opt(label, |label| vis.visit_label(label));
14231423
vis.visit_span(span);
14241424
}
1425-
ExprKind::Match(expr, arms) => {
1425+
ExprKind::Match(expr, arms, _) => {
14261426
vis.visit_expr(expr);
14271427
arms.flat_map_in_place(|arm| vis.flat_map_arm(arm));
14281428
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
981981
visit_opt!(visitor, visit_label, opt_label);
982982
try_visit!(visitor.visit_block(block));
983983
}
984-
ExprKind::Match(subexpression, arms) => {
984+
ExprKind::Match(subexpression, arms, _) => {
985985
try_visit!(visitor.visit_expr(subexpression));
986986
walk_list!(visitor, visit_arm, arms);
987987
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
184184
)
185185
}),
186186
ExprKind::TryBlock(body) => self.lower_expr_try_block(body),
187-
ExprKind::Match(expr, arms) => hir::ExprKind::Match(
187+
ExprKind::Match(expr, arms, _) => hir::ExprKind::Match(
188188
self.lower_expr(expr),
189189
self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
190190
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/ord.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::path_std;
4-
use rustc_ast::MetaItem;
4+
use rustc_ast::{ast::MatchKind, MetaItem};
55
use rustc_expand::base::{Annotatable, ExtCtxt};
66
use rustc_span::symbol::{sym, Ident};
77
use rustc_span::Span;
@@ -70,7 +70,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl
7070
let eq_arm = cx.arm(span, cx.pat_path(span, equal_path.clone()), expr1);
7171
let neq_arm =
7272
cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
73-
cx.expr_match(span, expr2, thin_vec![eq_arm, neq_arm])
73+
cx.expr_match(span, expr2, thin_vec![eq_arm, neq_arm], MatchKind::Prefix)
7474
}
7575
CsFold::Fieldless => cx.expr_path(equal_path.clone()),
7676
},

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::{path_std, pathvec_std};
4-
use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind};
4+
use rustc_ast::{ExprKind, ItemKind, MatchKind, MetaItem, PatKind};
55
use rustc_expand::base::{Annotatable, ExtCtxt};
66
use rustc_span::symbol::{sym, Ident};
77
use rustc_span::Span;
@@ -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
{
@@ -146,7 +146,7 @@ fn cs_partial_cmp(
146146
);
147147
let neq_arm =
148148
cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
149-
cx.expr_match(span, expr2, thin_vec![eq_arm, neq_arm])
149+
cx.expr_match(span, expr2, thin_vec![eq_arm, neq_arm], MatchKind::Prefix)
150150
}
151151
}
152152
CsFold::Fieldless => cx.expr_some(span, cx.expr_path(equal_path.clone())),

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::deriving::generic::*;
33
use crate::deriving::path_std;
44

55
use ast::EnumDef;
6+
use ast::MatchKind;
67
use rustc_ast::{self as ast, MetaItem};
78
use rustc_expand::base::{Annotatable, ExtCtxt};
89
use rustc_span::symbol::{sym, Ident, Symbol};
@@ -235,7 +236,7 @@ fn show_fieldless_enum(
235236
cx.arm(span, pat, cx.expr_str(span, v.ident.name))
236237
})
237238
.collect::<ThinVec<_>>();
238-
let name = cx.expr_match(span, cx.expr_self(span), arms);
239+
let name = cx.expr_match(span, cx.expr_self(span), arms, MatchKind::Prefix);
239240
let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
240241
BlockOrExpr::new_expr(cx.expr_call_global(span, fn_path_write_str, thin_vec![fmt, name]))
241242
}

compiler/rustc_builtin_macros/src/deriving/decodable.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::deriving::generic::ty::*;
44
use crate::deriving::generic::*;
55
use crate::deriving::pathvec_std;
6+
use ast::MatchKind;
67
use rustc_ast::ptr::P;
78
use rustc_ast::{self as ast, Expr, MetaItem, Mutability};
89
use rustc_expand::base::{Annotatable, ExtCtxt};
@@ -153,7 +154,12 @@ fn decodable_substructure(
153154

154155
let result = cx.expr_ok(
155156
trait_span,
156-
cx.expr_match(trait_span, cx.expr_ident(trait_span, variant), arms),
157+
cx.expr_match(
158+
trait_span,
159+
cx.expr_ident(trait_span, variant),
160+
arms,
161+
MatchKind::Prefix,
162+
),
157163
);
158164
let lambda = cx.lambda(trait_span, vec![blkarg, variant], result);
159165
let variant_array_ref = cx.expr_array_ref(trait_span, variants);

0 commit comments

Comments
 (0)