-
Notifications
You must be signed in to change notification settings - Fork 1k
Special case matches! macro
#5554
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
Open
ytmimi
wants to merge
11
commits into
rust-lang:main
Choose a base branch
from
ytmimi:special_case_matches_macro
base: main
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
11 commits
Select commit
Hold shift + click to select a range
46f3b2a
Add helper function to determine if a TokenTree is a comma
ytmimi f2d6f85
Parse `matches!` macro
ytmimi d32e72b
Increase visibility of `expr::choose_separator_tactic`
ytmimi 5973f0f
Better encapsulate guard rewrite logic in relation to pattern rewirte
ytmimi 34a6d17
Add MatchesMacroItem and imp Rewrite, Spanned, and IntoOverflowableItem
ytmimi 9687b16
Special case `matches!` macro
ytmimi 1ccdbc2
Add unit tests for `matches!` special case formatting
ytmimi e7553df
Add `matches!` test case for issue 5709
ytmimi 26e198f
Apply updated `matches!` formatting to rustfmt
ytmimi 1a502d0
Add test case for issue 6650
ytmimi 7b12588
Add test case for issue 6714
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
| use rustc_ast::ast; | ||
| use rustc_ast::ptr::P; | ||
| use rustc_ast::tokenstream::TokenStream; | ||
| use rustc_parse::exp; | ||
| use rustc_parse::parser::{CommaRecoveryMode, RecoverColon, RecoverComma}; | ||
|
|
||
| use super::is_token_tree_comma; | ||
| use crate::rewrite::{RewriteContext, RewriteError}; | ||
|
|
||
| #[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, | ||
| ) -> Result<Matches, RewriteError> { | ||
| let mut cursor = ts.iter().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().map_err(|_| RewriteError::Unknown)?; | ||
|
|
||
| let _ = parser.eat(exp!(Comma)); | ||
|
|
||
| let pat = parser | ||
| .parse_pat_allow_top_guard( | ||
| None, | ||
| RecoverComma::Yes, | ||
| RecoverColon::Yes, | ||
| CommaRecoveryMode::EitherTupleOrPipe, | ||
| ) | ||
| .map_err(|_| RewriteError::Unknown)?; | ||
|
|
||
| let guard = if parser.eat_keyword(exp!(If)) { | ||
| Some(parser.parse_expr().map_err(|_| RewriteError::Unknown)?) | ||
| } else { | ||
| None | ||
| }; | ||
| Ok(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 hidden or 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 hidden or 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 hidden or 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-style_edition: 2027 | ||
|
|
||
| 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) | ||
| ) | ||
| } |
This file contains hidden or 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,5 @@ | ||
| // rustfmt-style_edition: 2027 | ||
|
|
||
| fn main() { | ||
| matches!(stmt, ast::Stmt{kind: ast::StmtKind::MacCall(box ast::MacCallStmt {style : ast::MacStmtStyle::Braces,..}),..}) | ||
| } |
This file contains hidden or 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,36 @@ | ||
| // rustfmt-style_edition: 2027 | ||
|
|
||
| // The only difference between the two macros here is that one uses | ||
| // `matches!('a', 'a'..='z' | '0'..='9')` while the other use | ||
| // `matches!('a', 'a'..='z' | '0')`, without the second range. The problem is | ||
| // that the first isn't formated by rustfmt. | ||
| // You can test it yourself by using the Rustfmt tool, top right | ||
| // | ||
| // (Note that in this specific example, I could have used | ||
| // `matches!('a', 'a'..='z') || matches!('a', '0'..='9')`, which *is* formated, | ||
| // but that's not the point) | ||
|
|
||
| macro_rules! this_macro_will_not_be_formated { | ||
| () => { | ||
|
|
||
| let _ = | ||
| "some ugly not formated code" | ||
| ; | ||
|
|
||
| let _ = matches!('a', 'a'..='z' | '0'..='9'); | ||
|
|
||
| }; | ||
| } | ||
|
|
||
| macro_rules! this_macro_will_be_formated { | ||
| () => { | ||
|
|
||
| let _ = | ||
| "some ugly not formated code" | ||
| ; | ||
|
|
||
| let _ = matches!('a', 'a'..='z' | '0'); | ||
|
|
||
| }; | ||
| } | ||
|
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I feel it might be better to support (debug_)assert_matches will be stabilized in 1.96 macros in similar bases.
View changes since the review