Skip to content

rustfmt: Format cfg_select! - #154202

Open
ytmimi wants to merge 11 commits into
rust-lang:mainfrom
ytmimi:format_cfg_select
Open

rustfmt: Format cfg_select!#154202
ytmimi wants to merge 11 commits into
rust-lang:mainfrom
ytmimi:format_cfg_select

Conversation

@ytmimi

@ytmimi ytmimi commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

View all comments

tracking issue: #115585

Implementing cfg_select! formatting here in rust-lang/rust so 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:

See also:

@rustbot

rustbot commented Mar 22, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Mar 22, 2026
@rustbot

rustbot commented Mar 22, 2026

Copy link
Copy Markdown
Collaborator

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: rustfmt, rustfmt-contributors
  • rustfmt, rustfmt-contributors expanded to 6 candidates
  • Random selection from fee1-dead, jieyouxu

Comment thread src/tools/rustfmt/tests/target/cfg_select.rs Outdated
Comment on lines +286 to +303
// 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
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A pre-existing issue with trailing comments in rustfmt. I wanted to explicitly capture the current behavior in a test case.

Comment on lines +419 to +437
// 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;
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +305 to +320
// 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",
}

@ytmimi ytmimi Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely happy to see this!

View changes since this review

Comment thread src/tools/rustfmt/tests/source/cfg_select.rs
@jieyouxu

Copy link
Copy Markdown
Member

(I'll gradually review this; need to build up some background ctx first.)

@jieyouxu jieyouxu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The impl broadly looks good to me, just some minor nits and a few test coverage discussions.
@rustbot author

View changes since this review

Comment thread src/tools/rustfmt/src/parse/macros/cfg_select.rs
Comment thread src/tools/rustfmt/src/parse/macros/cfg_select.rs
Comment thread src/tools/rustfmt/src/parse/macros/cfg_select.rs
Comment thread src/tools/rustfmt/src/parse/macros/cfg_select.rs
Comment thread src/tools/rustfmt/src/parse/macros/cfg_select.rs
Comment on lines +305 to +320
// 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",
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +322 to +329
// comments before and after the `=>` get dropped right now
cfg_select! {
any(true, true, true, true,) => {}

not(false) => {}

any(false) => "any",
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: this should also a "known bug" right? Should we also include a follow-up issue to track this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we can create an issue for it. I'll handle that after the initial formatting lands

Comment thread src/tools/rustfmt/tests/target/cfg_select.rs Outdated
Comment thread src/tools/rustfmt/tests/source/cfg_select.rs
Comment thread src/tools/rustfmt/tests/source/cfg_select.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 30, 2026
@rustbot

rustbot commented Mar 30, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backlinks / context for myself (and future travellers):

Metadata

History

Initial style team discussions

  • The arms must be wrapped in braces, so rustfmt will have to ensure to not remove those.

Follow-up: unbraced expressions are permitted

PR: #145233

@rust-bors

This comment has been minimized.

jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 24, 2026
…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
rust-timer added a commit that referenced this pull request Jul 24, 2026
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
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jul 25, 2026
…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
ytmimi added 6 commits July 30, 2026 00:25
@ytmimi
ytmimi force-pushed the format_cfg_select branch from c267330 to d32c7e9 Compare July 30, 2026 04:29
@rustbot

rustbot commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

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.

@ytmimi

ytmimi commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

@jieyouxu Thanks for all the good feedback on this. Finally got around to making all the updates.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 30, 2026

@jieyouxu jieyouxu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have two residual questions/nits, but the impl itself and tests look good

View changes since this review

Comment thread src/tools/rustfmt/src/parse/macros/cfg_select.rs Outdated
Comment thread src/tools/rustfmt/src/macros.rs
Comment thread src/tools/rustfmt/src/macros.rs
Comment thread src/tools/rustfmt/tests/target/cfg_select.rs

@jieyouxu jieyouxu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/tools/rustfmt/src/macros.rs Outdated
Comment thread src/tools/rustfmt/src/parse/macros/cfg_select.rs Outdated
ytmimi added 5 commits July 30, 2026 10:03
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.

@jieyouxu jieyouxu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +1105 to +1115
// 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
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: good call, I forgot about this coverage

@rust-bors

rust-bors Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ca398cd has been approved by jieyouxu

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 31, 2026
@jieyouxu jieyouxu added the F-cfg_select `#![feature(cfg_select)]` label Jul 31, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 31, 2026
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
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 31, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-cfg_select `#![feature(cfg_select)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants