Skip to content

Extend cfg_if! support to cfg_match! - #6522

Merged
ytmimi merged 3 commits into
rust-lang:masterfrom
CAD97:cfg_match
Apr 5, 2025
Merged

Extend cfg_if! support to cfg_match!#6522
ytmimi merged 3 commits into
rust-lang:masterfrom
CAD97:cfg_match

Conversation

@CAD97

@CAD97 CAD97 commented Mar 26, 2025

Copy link
Copy Markdown
Contributor

Like is done for cfg_if!, try to discover mod items that are defined in inactive cfg_match! arms.

Comment thread tests/source/cfg_match/detect/arch/aarch64.rs Outdated
Comment thread tests/source/issue-4656-match/format_me_please.rs Outdated

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.

Is there a purpose to this file? lib2.rs isn't being referenced at all from lib.rs.

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.

This is a copy of the original issue-4656 regression test, where a separate file in the same session could lead to the cfg_if!-transparent mod discovery not happening in other files. Since cfg_match! duplicates much of the same handling, it could easily suffer the same regression, so it makes sense to test. The two files are loaded into the same session by the format_files_find_new_files_via_cfg_match test.

If there's a way to make this clearer, I'll happily make the change, but as-is, this is directly copied from the existing issue-4656 test, and the tested path is distinct between cfg_if! and 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.

Thanks for looking into that. In that case we can keep the test.

Comment thread src/test/mod.rs Outdated
Comment thread src/modules.rs
fn is_cfg_match(item: &ast::Item) -> bool {
match item.kind {
ast::ItemKind::MacCall(ref mac) => {
if let Some(last_segment) = mac.path.segments.last() {

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.

I want to draw attention to this: cfg_match! is checking that the last path segment is cfg_match! (i.e. tries to process any::path::cfg_match!), whereas cfg_if! is checking that the first path segment is cfg_if! (i.e. tries to process cfg_if::any::path!). I think my behavior is more correct, and the worst case scenario is that some more files get discovered when a custom cfg_match! that looks similar enough to core::cfg_match! is used, but it bears pointing out.

If factoring this behavior out to be the same between the two is desirable, I can do that, and would choose to check the macro-ident, not the path front. (The current cfg_if! processing will skip ::cfg_if::cfg_if!, since the path starts with kw::PathRoot.)

@CAD97 CAD97 Mar 27, 2025

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.

I went ahead and wrote the patch to unify the predicate: a129548

The visitor could be further refactored into a single KnownMacroVisitor which processes both cfg_if! and cfg_match!, but that starts to feel a bit excessive...

I haven't included it in this PR since it's behavior changing for the existing cfg_if! support, to keep this PR scoped to just the cfg_match! support.

@CAD97 CAD97 Mar 27, 2025

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.

... and I ended up doing that cleanup as well: 4ae9dff

But I'm actually done doing extended cleanup for this now 😅

To be fully explicit about it: the two linked commits in this and the prior comment are made available under the same license terms as if I had directly contributed them by placing them in a PR. Feel free to pull them (or ask me to PR them) if you believe them to be an improvement.

@CAD97 CAD97 left a comment

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.

I cleaned up the added tests to only test as much as is necessary and not duplicate the stddetect snapshot. Should hopefully be reasonable to merge in this state.

@CAD97
CAD97 requested a review from ytmimi March 27, 2025 17:20

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.

Should we extend this test case to add a few different way of specifying the attribute item? Looking at the tracking issue, it looks like there are a few different ways it can be specified e.g. (unix, target_pointer_width = "32", cfg(key = "value"), and _).

cfg_match! {
    unix => {
        mod format_me_please_1
    }
    target_pointer_width = "32" => {
        mod format_me_please_2
    }
    cfg(key = "value") => {
        mod format_me_please_3
    }
    _ => {
        mod format_me_please_4
    }
}

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 went ahead an added this to the test case.

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

Should be good to go here. Thanks for getting the ball rolling on cfg_match!.

@ytmimi ytmimi added pr-ready-to-merge Status: PR is largely ready for merge, waiting for secondary review / last nits release-notes Needs an associated changelog entry and removed pr-waiting-on-author labels Apr 5, 2025
@ytmimi
ytmimi merged commit bedef9b into rust-lang:master Apr 5, 2025
@ytmimi ytmimi removed the pr-ready-to-merge Status: PR is largely ready for merge, waiting for secondary review / last nits label Apr 5, 2025
@CAD97
CAD97 deleted the cfg_match branch April 5, 2025 21:51
@ytmimi ytmimi removed the release-notes Needs an associated changelog entry label Feb 26, 2026
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 to rust-lang/rust 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants