Skip to content
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

Extend cfg_if! support to cfg_match! #6522

Merged
merged 3 commits into from
Apr 5, 2025
Merged

Conversation

CAD97
Copy link
Contributor

@CAD97 CAD97 commented Mar 26, 2025

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

Copy link
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
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
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.

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

Copy link
Contributor Author

@CAD97 CAD97 Mar 27, 2025

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.

Copy link
Contributor Author

@CAD97 CAD97 Mar 27, 2025

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.

Copy link
Contributor Author

@CAD97 CAD97 left a comment

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

Copy link
Contributor

@ytmimi ytmimi left a comment

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 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
26 checks passed
@CAD97 CAD97 deleted the cfg_match branch April 5, 2025 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes Needs an associated changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants