Skip to content

make cfg_select a builtin macro #143461

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

folkertdev
Copy link
Contributor

@folkertdev folkertdev commented Jul 4, 2025

tracking issue: #115585

This parses mostly the same as the macro cfg_select version, except:

  1. wrapping in double brackets is no longer supported (or needed): cfg_select {{ /* ... */ }} is now rejected.
  2. in an expression context, the rhs is no longer wrapped in a block, so that this now works:
fn main() {
    println!(cfg_select! {
        unix => { "foo" }
        _ => { "bar" }
    });
}
  1. a single wildcard rule is now supported: cfg_select { _ => 1 } now works

I've also added an error if none of the rules evaluate to true, and warnings for any arms that follow the _ wildcard rule.

cc @traviscross if I'm missing any feature that should/should not be included
r? @petrochenkov for the macro logic details

@rustbot rustbot added A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 4, 2025
@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the cfg-select-builtin-macro branch 2 times, most recently from e3aeb08 to 1dc0034 Compare July 5, 2025 06:03
@folkertdev folkertdev marked this pull request as ready for review July 5, 2025 06:07
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 5, 2025

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

This PR modifies tests/auxiliary/minicore.rs.

cc @jieyouxu

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@traviscross traviscross left a comment

Choose a reason for hiding this comment

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

The behavior, per the tests and the impl, looks correct to me.

@folkertdev folkertdev force-pushed the cfg-select-builtin-macro branch 2 times, most recently from 3d68353 to 3abcece Compare July 5, 2025 12:52
@folkertdev
Copy link
Contributor Author

I moved some things around (analogously to asm!) so that rustfmt can use the parsed representation of cfg_select!. Now that it is a builtin macro, I'm assuming formatting won't work and becomes a blocker for stabilization. I think the formatting rules are reasonably straightforward though.

Implementation-wise the one thing I'm not sure about is how to get rustfmt to format a TokenStream that represents the rhs of an arm. It gets expanded based on the context (can be an item, expr, stmt, etc.). But hopefully the rustfmt folks know how to deal with that.

@traviscross
Copy link
Contributor

cc @rust-lang/rustfmt @rust-lang/style

@bors

This comment was marked as resolved.

@folkertdev folkertdev force-pushed the cfg-select-builtin-macro branch from 3abcece to 53b3b88 Compare July 6, 2025 15:27
@bors

This comment was marked as resolved.

@folkertdev folkertdev force-pushed the cfg-select-builtin-macro branch from 53b3b88 to 5d550b1 Compare July 7, 2025 18:46
@folkertdev folkertdev force-pushed the cfg-select-builtin-macro branch from 4b84ad7 to 01f41cc Compare July 7, 2025 18:58
@folkertdev
Copy link
Contributor Author

Given that formatting now seems straightforward, I think this can be merged without the precise formatting logic. It's probably better (also for review etc.) to add formatting separately in the rustfmt repo. I've played around with it a bit, but I think there is some benefit from refactoring the macro_rules! logic a bit for more code sharing.

So, this PR is complete from my perspective.

@ytmimi
Copy link
Contributor

ytmimi commented Jul 10, 2025

Given that formatting now seems straightforward, I think this can be merged without the precise formatting logic. It's probably better (also for review etc.) to add formatting separately in the rustfmt repo. I've played around with it a bit, but I think there is some benefit from refactoring the macro_rules! logic a bit for more code sharing.

Nice! I agree that a PR to the rustfmt repo would be ideal to add the formatting logic / refactoring for code sharing with macro_rules!

@petrochenkov petrochenkov 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 Jul 10, 2025
@folkertdev folkertdev force-pushed the cfg-select-builtin-macro branch from 01f41cc to b4afa4a Compare July 10, 2025 14:57
@folkertdev
Copy link
Contributor Author

@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 10, 2025
@petrochenkov petrochenkov 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 Jul 10, 2025
@folkertdev folkertdev force-pushed the cfg-select-builtin-macro branch from b4afa4a to f8e7813 Compare July 10, 2025 16:51
@folkertdev
Copy link
Contributor Author

@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 10, 2025
@petrochenkov
Copy link
Contributor

Thanks!
@bors r+

@bors
Copy link
Collaborator

bors commented Jul 11, 2025

📌 Commit f8e7813 has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors 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 11, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 12, 2025
…o, r=petrochenkov

make `cfg_select` a builtin macro

tracking issue: rust-lang#115585

This parses mostly the same as the `macro cfg_select` version, except:

1. wrapping in double brackets is no longer supported (or needed): `cfg_select {{ /* ... */ }}` is now rejected.
2. in an expression context, the rhs is no longer wrapped in a block, so that this now works:
  ```rust
  fn main() {
      println!(cfg_select! {
          unix => { "foo" }
          _ => { "bar" }
      });
  }
  ```
3. a single wildcard rule is now supported: `cfg_select { _ => 1 }` now works

I've also added an error if none of the rules evaluate to true, and warnings for any arms that follow the `_` wildcard rule.

cc `@traviscross` if I'm missing any feature that should/should not be included
r? `@petrochenkov` for the macro logic details
bors added a commit that referenced this pull request Jul 12, 2025
Rollup of 9 pull requests

Successful merges:

 - #143213 (de-duplicate condition scoping logic between AST→HIR lowering and `ScopeTree` construction)
 - #143461 (make `cfg_select` a builtin macro)
 - #143519 (Check assoc consts and tys later like assoc fns)
 - #143554 (slice: Mark `rotate_left`, `rotate_right` unstably const)
 - #143704 (Be a bit more careful around exotic cycles in in the inliner)
 - #143774 (constify `From` and `Into`)
 - #143786 (Fix fallback for CI_JOB_NAME)
 - #143796 (Fix ICE for parsed attributes with longer path not handled by CheckAttribute)
 - #143798 (Remove format short command trait)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants