Skip to content

rustfmt: Discover modules via cfg_select!#158372

Open
mkroening wants to merge 1 commit into
rust-lang:mainfrom
mkroening:rustfmt-cfg_select-modules
Open

rustfmt: Discover modules via cfg_select!#158372
mkroening wants to merge 1 commit into
rust-lang:mainfrom
mkroening:rustfmt-cfg_select-modules

Conversation

@mkroening

Copy link
Copy Markdown
Contributor

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

@rustbot

rustbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

rustfmt is developed in its own repository. If possible, consider making this change to rust-lang/rustfmt instead.

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 Jun 24, 2026
@rustbot

rustbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

r? @ytmimi

rustbot has assigned @ytmimi.
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 jieyouxu, ytmimi

@tgross35

Copy link
Copy Markdown
Contributor

@rustbot label +beta-nominated

cfg_select has already been stable for a few releases so there's a bit of a gap here. Since the only change is a renaming, it seems potentially feasible to backport.

@rustbot rustbot added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jun 24, 2026
test => {
mod format_me_please_1;
}
target_family = "unix" => {
mod format_me_please_2;
}
cfg(target_pointer_width = "32") => {

@jieyouxu jieyouxu Jun 25, 2026

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.

Thought: doesn't this change stable formatting? I guess it's technically a bug fix so maybe this is... acceptable re. formatting stability guarantees?

Hm, I guess technically we could consider it... "stable but unformatted"

View changes since the review

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.

stable but unformatted

I believe that was the precedent set by let-else.

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.

Hm... I do wonder them if it might be better to land this and #154202 in the same release 🤔, otherwise #154202 becomes stable formatted -> stable formatted (changed)

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 that would be best.

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.

Any changes to cfg_select! bodies from #154202 are probably going to be way less substantial than having entire unformatted modules trees, no?

If that PR looked closer to ready then I'd agree we should wait. It As-is though, anyone migrating from cfg_if is going to start accumulating code that's out of formatting sync (possibly without noticing), and that seems like a hole worth plugging.

@mkroening mkroening Jun 25, 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.

Regarding the changes in the test files: only renaming cfg_match! to cfg_select! is required for the tests to work. The following other changes just update the test to be more modern and to compile successfully (which is not tested):

  • removing the nightly feature
  • removing std:: before the Marco
  • migrating away from the removed cfg() syntax

The last point specifically still works for module file discovery, even though it does not compile. The parsing machinery can probably drop support for that but I did not remove it to keep the PR minimal.

@ytmimi

ytmimi commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Overall I think this is a good change, and although it would technically be breaking since we'd end up formatting modules listed inside the cfg_select! there is some president with let-else, which was mentioned in an earlier comment. I think as long as we mention this change in the release notes we should be fine, and I'm sure users would welcome this fix. I'm at work right now, but will review this today when I have some free time.

@ytmimi ytmimi 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.

I think there are still a few references to the old cfg_match! that we should change. Once that's done and we figure out how we'll disclose this breaking change you can r=me.

I think it's fine to get this out before cfg_select! formatting lands since I agree that the impact of entire modules being left unformatted is larger than what will change once we start formatting cfg_select! calls.

View changes since this review

use crate::attr::MetaVisitor;
use crate::parse::macros::cfg_if::parse_cfg_if;
use crate::parse::macros::cfg_match::parse_cfg_match;
use crate::parse::macros::cfg_select::parse_cfg_match;

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.

Shouldn't we also update the name from parse_cfg_match -> parse_cfg_select?

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.

Thanks for catching this! Done. 👍


/// Traverse `cfg_match!` macro and fetch modules.
/// Traverse `cfg_select!` macro and fetch modules.
pub(crate) struct CfgMatchVisitor<'a> {

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.

Shouldn't we also rename CfgMatchVisitor to CfgSelectVisitor?

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.

Thanks for catching this! Done. 👍

@mkroening mkroening force-pushed the rustfmt-cfg_select-modules branch from 3ba2282 to a71b047 Compare June 26, 2026 09:43
@rust-log-analyzer

This comment has been minimized.

@mkroening mkroening force-pushed the rustfmt-cfg_select-modules branch from a71b047 to 4e98db3 Compare June 26, 2026 13:45
@mkroening mkroening force-pushed the rustfmt-cfg_select-modules branch from 4e98db3 to facc787 Compare June 26, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beta-nominated Nominated for backporting to the compiler in the beta channel. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustfmt does not discover modules through cfg_select!

8 participants