-
Notifications
You must be signed in to change notification settings - Fork 898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Special case matches!
macro
#5554
Open
ytmimi
wants to merge
10
commits into
rust-lang:master
Choose a base branch
from
ytmimi:special_case_matches_macro
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f2942d2
Add helper function to determine if a TokenTree is a comma
ytmimi 00b9003
Parse `matches!` macro
ytmimi 735efa6
Increase visibility of `expr::choose_separator_tactic`
ytmimi fbcc07a
Better encapsulate guard rewrite logic in relation to pattern rewirte
ytmimi 8de2540
Add MatchesMacroItem and imp Rewrite, Spanned, and IntoOverflowableItem
ytmimi 9d6a512
Special case `matches!` macro
ytmimi e563f1b
Version gate `matches!` special case formatting
ytmimi 9e37c1e
Add unit tests for `matches!` special case formatting
ytmimi 93130c0
Add `matches!` test case for issue 5709
ytmimi 0c5552d
Apply updated `matches!` formatting to rustfmt
ytmimi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use rustc_ast::ast; | ||
use rustc_ast::ptr::P; | ||
use rustc_ast::token::TokenKind; | ||
use rustc_ast::tokenstream::TokenStream; | ||
use rustc_parse::parser::{CommaRecoveryMode, RecoverColon, RecoverComma}; | ||
use rustc_span::symbol::kw; | ||
|
||
use super::is_token_tree_comma; | ||
use crate::rewrite::RewriteContext; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct Matches { | ||
pub(crate) expr: P<ast::Expr>, | ||
pub(crate) pat: P<ast::Pat>, | ||
pub(crate) guard: Option<P<ast::Expr>>, | ||
} | ||
|
||
/// Parse matches! from <https://doc.rust-lang.org/std/macro.matches.html> | ||
pub(crate) fn parse_matches(context: &RewriteContext<'_>, ts: TokenStream) -> Option<Matches> { | ||
let mut cursor = ts.trees().peekable(); | ||
// remove trailing commmas from the TokenStream since they lead to errors when parsing ast::Pat | ||
// using parse_pat_allow_top_alt below since the parser isn't expecting a trailing comma. | ||
// This is only an issue when the `ast::Pat` is not followed by a guard. In either case it's ok | ||
// to remove the comma from the stream since we don't need it to parse into a Matches struct | ||
let mut token_trees = vec![]; | ||
while let Some(tt) = cursor.next() { | ||
let is_last = cursor.peek().is_none(); | ||
if !(is_last && is_token_tree_comma(tt)) { | ||
token_trees.push(tt.clone()) | ||
} | ||
} | ||
|
||
let ts = token_trees.into_iter().collect(); | ||
let mut parser = super::build_parser(context, ts); | ||
let expr = parser.parse_expr().ok()?; | ||
|
||
parser.eat(&TokenKind::Comma); | ||
|
||
let pat = parser | ||
.parse_pat_allow_top_alt( | ||
None, | ||
RecoverComma::Yes, | ||
RecoverColon::Yes, | ||
CommaRecoveryMode::EitherTupleOrPipe, | ||
) | ||
.ok()?; | ||
|
||
let guard = if parser.eat_keyword(kw::If) { | ||
Some(parser.parse_expr().ok()?) | ||
} else { | ||
None | ||
}; | ||
Some(Matches { expr, pat, guard }) | ||
} | ||
|
||
impl Matches { | ||
pub(crate) fn items(self) -> [MatchesMacroItem; 2] { | ||
[ | ||
MatchesMacroItem::Expr(self.expr), | ||
MatchesMacroItem::Arm(self.pat, self.guard), | ||
] | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum MatchesMacroItem { | ||
Expr(P<ast::Expr>), | ||
Arm(P<ast::Pat>, Option<P<ast::Expr>>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// rustfmt-version: Two | ||
|
||
fn is_something(foo: Foo, bar: Bar) -> bool { | ||
matches!((legacy_required_finality, signature_weight), | ||
| (LegacyRequiredFinality::Any, Insufficient | Weak | Strict) | ||
| (LegacyRequiredFinality::Weak, Weak | Strict) | ||
| (LegacyRequiredFinality::Strict, Strict) | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@calebcartwright per your comment #5554 (comment), I just wanted to let you know that the changes are version gated.
I don't think there's a rush to get this in, and we can definitely hold off until
style_edition
lands to gate this around the2024
style edition.