rustfmt: Format cfg_select! - #154202
Conversation
|
Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt |
|
r? @jieyouxu rustbot has assigned @jieyouxu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| // trailing comments on the last line are a little buggy and always wrap back up | ||
| cfg_select! { | ||
| windows => { | ||
| "windows" | ||
| } | ||
| unix => { | ||
| "unix" | ||
| } | ||
| _ => { | ||
| "none" | ||
| } // FIXME. Prevent wrapping back up to the next line | ||
| } | ||
|
|
||
| cfg_select! { | ||
| windows => "windows", | ||
| unix => "unix", | ||
| _ => "none", // FIXME. Prevent wrapping back up to the next line | ||
| } |
There was a problem hiding this comment.
A pre-existing issue with trailing comments in rustfmt. I wanted to explicitly capture the current behavior in a test case.
| // Can't format cfg_select! at all with style_edition <= 2021. | ||
| // Things can be formatted with style_edition >= 2024 | ||
| cfg_select! { | ||
| feature = "debug-with-rustfmt-long-long-long-long-loooooooonnnnnnnnnnnnnnnggggggffffffffffffffff" => | ||
| { | ||
| // abc | ||
| println!(); | ||
| } | ||
| feature = "debug-with-rustfmt-long-long-long-long-loooooooonnnnnnnnnnnnnnnggggggffffffffffffffff" => | ||
| { | ||
| // abc | ||
| } | ||
| all(anything( | ||
| "some other long long long long long thing long long long long long long long long long long long", | ||
| feature = "debug-with-rustfmt-long-long-long-long-loooooooonnnnnnnnnnnnnnnggggggffffffffffffffff" | ||
| )) => { | ||
| let x = 7; | ||
| } | ||
| } |
There was a problem hiding this comment.
Through some testing I found that in style_edition <= 2021 this example won't be formatted at all because the predicate can't be formatted within max_width, but with style_edition >= 2024 that's not an issue.
| // comments within the predicate are fine with style_edition=2024+ | ||
| cfg_select! { | ||
| any( | ||
| true, /* comment */ | ||
| true, true, // true, | ||
| true, | ||
| ) => {} | ||
|
|
||
| not( | ||
| false // comment | ||
| ) => {} | ||
|
|
||
| any( | ||
| false // comment | ||
| ) => "any", | ||
| } |
There was a problem hiding this comment.
Not the best place for a comment to begin with IMO, but I wanted to call out that comments within the predicate aren't correctly handled in style_edition <= 2021. This is how the current example is formatted:
cfg_select! {
any(
true, /* comment */
true, true, // true,
true,
) => {}
not(false // comment) => {}
any(false // comment) => "any",
}Clearly that's wrong and produces invalid code, but fixing this seems like it's outisde the scope of this PR. since it only impacts style_edition <= 2021.
Maybe this is something we can address later?
There was a problem hiding this comment.
I agree, I think we shouldn't try to bundle that in this PR. We probably should open an issue in rustfmt/r-l/r to track this as a known-bug though.
|
(I'll gradually review this; need to build up some background ctx first.) |
There was a problem hiding this comment.
Thanks! The impl broadly looks good to me, just some minor nits and a few test coverage discussions.
@rustbot author
| // comments within the predicate are fine with style_edition=2024+ | ||
| cfg_select! { | ||
| any( | ||
| true, /* comment */ | ||
| true, true, // true, | ||
| true, | ||
| ) => {} | ||
|
|
||
| not( | ||
| false // comment | ||
| ) => {} | ||
|
|
||
| any( | ||
| false // comment | ||
| ) => "any", | ||
| } |
There was a problem hiding this comment.
I agree, I think we shouldn't try to bundle that in this PR. We probably should open an issue in rustfmt/r-l/r to track this as a known-bug though.
| // comments before and after the `=>` get dropped right now | ||
| cfg_select! { | ||
| any(true, true, true, true,) => {} | ||
|
|
||
| not(false) => {} | ||
|
|
||
| any(false) => "any", | ||
| } |
There was a problem hiding this comment.
Question: this should also a "known bug" right? Should we also include a follow-up issue to track this?
There was a problem hiding this comment.
Yeah, I think we can create an issue for it. I'll handle that after the initial formatting lands
|
Reminder, once the PR becomes ready for a review, use |
There was a problem hiding this comment.
Backlinks / context for myself (and future travellers):
Metadata
- Library feature:
cfg_select - std docs: https://doc.rust-lang.org/nightly/std/macro.cfg_select.html
- Tracking issue: Tracking issue for
cfg_select(formerlycfg_match) #115585 - Style team discussion: Formatting for cfg_select style-team#201
- Reference update PR:
cfg_select!macro reference#2103
History
Initial style team discussions
- The arms must be wrapped in braces, so
rustfmtwill have to ensure to not remove those.
Follow-up: unbraced expressions are permitted
PR: #145233
This comment has been minimized.
This comment has been minimized.
…es, r=ytmimi,jieyouxu rustfmt: Discover modules via `cfg_select!` This PR renames all occurrences of `cfg_match!` in rustfmt to `cfg_select!`. This makes the module file detection logic from rust-lang/rustfmt#6522 kick in for `cfg_select!` instead of `cfg_match!`, which no longer exists. This PR performs no other adjustments to the logic to be as small as possible. I am opening this PR in this repo since that is also the target for the more comprehensive rust-lang#154202, which covers more than merely detecting other files through `cfg_select!`. Closes rust-lang#158371. CC: @ytmimi, @CAD97
Rollup merge of #158372 - mkroening:rustfmt-cfg_select-modules, r=ytmimi,jieyouxu rustfmt: Discover modules via `cfg_select!` This PR renames all occurrences of `cfg_match!` in rustfmt to `cfg_select!`. This makes the module file detection logic from rust-lang/rustfmt#6522 kick in for `cfg_select!` instead of `cfg_match!`, which no longer exists. This PR performs no other adjustments to the logic to be as small as possible. I am opening this PR in this repo since that is also the target for the more comprehensive #154202, which covers more than merely detecting other files through `cfg_select!`. Closes #158371. CC: @ytmimi, @CAD97
…imi,jieyouxu rustfmt: Discover modules via `cfg_select!` This PR renames all occurrences of `cfg_match!` in rustfmt to `cfg_select!`. This makes the module file detection logic from rust-lang/rustfmt#6522 kick in for `cfg_select!` instead of `cfg_match!`, which no longer exists. This PR performs no other adjustments to the logic to be as small as possible. I am opening this PR in this repo since that is also the target for the more comprehensive rust-lang/rust#154202, which covers more than merely detecting other files through `cfg_select!`. Closes rust-lang/rust#158371. CC: @ytmimi, @CAD97
`cfg_select!` parsing needs to be implemented in rustfmt right now because there's no good way to call `rustc_attr_parsing::parse_cfg_select`.
The plan is to leverage `rewrite_match_body` to help with `cfg_select!` formatting.
c267330 to
d32c7e9
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
Apply feedback from PR review.
Per the PR review I'm making `context: &RewriteContext<'_>` the first argument. Also moved the `shape` and `span` to follow the `context`.
Because `inside_macro` is a `Rc<Cell<bool>>` cloning the entire context doesn't actually isolate the `inside_macro` state. However, I've added a `debug_assert!` to make sure that we only ever call `context.leave_macro` when we're on a code path that immediately returns from `rewrite_macro_inner` so that we don't unexpectedly impact default macro handling where we need to be more cautious about adding or removing tokens.
| // Doesn't parse as expected so this is handled by the default macro handling | ||
| cfg_select! ( | ||
| A + B + C | ||
| ); | ||
| cfg_select! [ | ||
| A + B + C | ||
| ]; | ||
| // rustfmt doesn't format macros with brace delimiters | ||
| cfg_select! { | ||
| A + B + C | ||
| } |
There was a problem hiding this comment.
Remark: good call, I forgot about this coverage
rustfmt: Format `cfg_select!` tracking issue: rust-lang#115585 Implementing `cfg_select!` formatting here in `rust-lang/rust` so that the feature can get out to nightly quicker. The previous PR (rust-lang#144323) is a bit old at this point and I felt like I could simplify the implementation so I've opted to reimplement the formatting instead of building off the previous PR. I've tried to break the PR up into logical commits and I think this would be best reviewed one commit at a time. --- Previous PR: * rust-lang#144323 See also: * rust-lang/style-team#201
rustfmt: Format `cfg_select!` tracking issue: rust-lang#115585 Implementing `cfg_select!` formatting here in `rust-lang/rust` so that the feature can get out to nightly quicker. The previous PR (rust-lang#144323) is a bit old at this point and I felt like I could simplify the implementation so I've opted to reimplement the formatting instead of building off the previous PR. I've tried to break the PR up into logical commits and I think this would be best reviewed one commit at a time. --- Previous PR: * rust-lang#144323 See also: * rust-lang/style-team#201
View all comments
tracking issue: #115585
Implementing
cfg_select!formatting here inrust-lang/rustso that the feature can get out to nightly quicker.The previous PR (#144323) is a bit old at this point and I felt like I could simplify the implementation so I've opted to reimplement the formatting instead of building off the previous PR.
I've tried to break the PR up into logical commits and I think this would be best reviewed one commit at a time.
Previous PR:
cfg_select#144323See also: