Skip to content

Commit

Permalink
Rollup merge of #121619 - RossSmyth:pfix_match, r=petrochenkov
Browse files Browse the repository at this point in the history
Experimental feature postfix match

This has a basic experimental implementation for the RFC postfix match (rust-lang/rfcs#3295, #121618). [Liaison is](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Postfix.20Match.20Liaison/near/423301844) ```@scottmcm``` with the lang team's [experimental feature gate process](https://github.com/rust-lang/lang-team/blob/master/src/how_to/experiment.md).

This feature has had an RFC for a while, and there has been discussion on it for a while. It would probably be valuable to see it out in the field rather than continue discussing it. This feature also allows to see how popular postfix expressions like this are for the postfix macros RFC, as those will take more time to implement.

It is entirely implemented in the parser, so it should be relatively easy to remove if needed.

This PR is split in to 5 commits to ease review.

1. The implementation of the feature & gating.
2. Add a MatchKind field, fix uses, fix pretty.
3. Basic rustfmt impl, as rustfmt crashes upon seeing this syntax without a fix.
4. Add new MatchSource to HIR for Clippy & other HIR consumers
  • Loading branch information
matthiaskrgr authored Mar 22, 2024
2 parents f670f3b + 1709dd5 commit 0aa66d1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cmp::min;

use itertools::Itertools;
use rustc_ast::token::{Delimiter, Lit, LitKind};
use rustc_ast::{ast, ptr, token, ForLoopKind};
use rustc_ast::{ast, ptr, token, ForLoopKind, MatchKind};
use rustc_span::{BytePos, Span};

use crate::chains::rewrite_chain;
Expand Down Expand Up @@ -170,8 +170,8 @@ pub(crate) fn format_expr(
}
}
}
ast::ExprKind::Match(ref cond, ref arms) => {
rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs)
ast::ExprKind::Match(ref cond, ref arms, kind) => {
rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs, kind)
}
ast::ExprKind::Path(ref qself, ref path) => {
rewrite_path(context, PathContext::Expr, qself, path, shape)
Expand Down Expand Up @@ -625,7 +625,7 @@ pub(crate) fn rewrite_cond(
shape: Shape,
) -> Option<String> {
match expr.kind {
ast::ExprKind::Match(ref cond, _) => {
ast::ExprKind::Match(ref cond, _, MatchKind::Prefix) => {
// `match `cond` {`
let cond_shape = match context.config.indent_style() {
IndentStyle::Visual => shape.shrink_left(6).and_then(|s| s.sub_width(2))?,
Expand Down
33 changes: 23 additions & 10 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::iter::repeat;

use rustc_ast::{ast, ptr};
use rustc_ast::{ast, ptr, MatchKind};
use rustc_span::{BytePos, Span};

use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
Expand Down Expand Up @@ -72,6 +72,7 @@ pub(crate) fn rewrite_match(
shape: Shape,
span: Span,
attrs: &[ast::Attribute],
match_kind: MatchKind,
) -> Option<String> {
// Do not take the rhs overhead from the upper expressions into account
// when rewriting match condition.
Expand Down Expand Up @@ -131,15 +132,27 @@ pub(crate) fn rewrite_match(
}
} else {
let span_after_cond = mk_sp(cond.span.hi(), span.hi());
Some(format!(
"match {}{}{{\n{}{}{}\n{}}}",
cond_str,
block_sep,
inner_attrs_str,
nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
))

match match_kind {
MatchKind::Prefix => Some(format!(
"match {}{}{{\n{}{}{}\n{}}}",
cond_str,
block_sep,
inner_attrs_str,
nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
)),
MatchKind::Postfix => Some(format!(
"{}.match{}{{\n{}{}{}\n{}}}",
cond_str,
block_sep,
inner_attrs_str,
nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
)),
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/source/postfix-match/pf-match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![feature(postfix_match)]

fn main() {
let val = Some(42);

val.match {
Some(_) => 2,
_ => 1
};

Some(2).match {
Some(_) => true,
None => false
}.match {
false => "ferris is cute",
true => "I turn cats in to petted cats",
}.match {
_ => (),
}
}
20 changes: 20 additions & 0 deletions tests/target/postfix-match/pf-match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![feature(postfix_match)]

fn main() {
let val = Some(42);

val.match {
Some(_) => 2,
_ => 1,
};

Some(2).match {
Some(_) => true,
None => false,
}.match {
false => "ferris is cute",
true => "I turn cats in to petted cats",
}.match {
_ => (),
}
}

0 comments on commit 0aa66d1

Please sign in to comment.