Skip to content

Conversation

@folkertdev
Copy link
Contributor

@folkertdev folkertdev commented Dec 8, 2025

Stabilization report: rust-lang/rust#149783

@rustbot rustbot added S-waiting-on-review Status: The marked PR is awaiting review from a maintainer S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository labels Dec 8, 2025
@folkertdev folkertdev force-pushed the cfg-select branch 2 times, most recently from 11dedff to 6cbeb12 Compare December 8, 2025 20:25
Comment on lines 469 to 516
r[cfg.cfg_select]
### The `cfg_select` macro

The built-in `cfg_select` macro expands to the right-hand side of the first configuration predicate that evaluates to `true`.

For example:

```rust
cfg_select! {
unix => {
fn foo() { /* unix specific functionality */ }
}
target_pointer_width = "32" => {
fn foo() { /* non-unix, 32-bit functionality */ }
}
_ => {
fn foo() { /* fallback implementation */ }
}
}
```
The `cfg_select` macro can also be used in expression position:

```rust
let is_unix_str = cfg_select! {
unix => "unix",
_ => "not unix",
};
```

A `_` can be used to write a configuration predicate that always evaluates to `true`.
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 the Reference PR. It'd be worth filling in the details you provided in rust-lang/rust#149783 (comment), e.g. with the grammar, the removal of one level of braces, the compile error on fallthrough, etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

This can discuss as well how the bodies of each branch must be able to parse.

Copy link
Contributor Author

@folkertdev folkertdev left a comment

Choose a reason for hiding this comment

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

I think all the raw information is here now, but it probably needs some editing/clarification.

Also should the lint on branches that come after a wildcard be mentioned?

Comment on lines 485 to 486
r[cfg.cfg_select.general]
The built-in `cfg_select` macro expands to the `TokenTree` on the right-hand side of the first configuration predicate that evaluates to `true`.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is it clear enough from the grammer and the language here that the { /* ... */ } are dropped?

Comment on lines +518 to +530
r[cfg.cfg_select.positions]
The `cfg_select!` macro is accepted in the following macro expansion positions

- items
- statements
- expression
- impl items
- trait impl items
- trait items
- foreign items
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 can't find the positions in which macro expansion is allowed in the reference, actually.

Copy link
Contributor

Choose a reason for hiding this comment

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

The list of macro invocation sites is listed in https://doc.rust-lang.org/nightly/reference/macros.html#r-macro.invocation.intro. I don't think this needs to duplicate the list.

We haven't really gotten into documenting built-in attributes, so there isn't a well-trodden path here. I think it would be fine to say that it may specified in any position where macro invocations are allowed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, did you intend to exclude patterns and types in this list?

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 suspect that the list was accurate at the time, but it no longer is and should now include patterns and types.

Copy link
Contributor

@ehuss ehuss left a comment

Choose a reason for hiding this comment

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

Also should the lint on branches that come after a wildcard be mentioned?

As for lints, we normally don't document those. However, in this situation, it seems like it would be fine to have a > [!NOTE] block that mentions the lint (in whichever way rust-lang/rust#149960 ends up being).

Comment on lines 473 to 486
```grammar,configuration
CfgSelect ->
cfg_select! `{` CfgSelectBranch* `}`
CfgSelectConfigurationPredicate ->
ConfigurationPredicate | `_`
CfgSelectBranch ->
CfgSelectConfigurationPredicate `=>` `{` TokenTree `}`
| CfgSelectConfigurationPredicate `=>` TokenTree `,`
```
Copy link
Contributor

Choose a reason for hiding this comment

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

This grammar doesn't quite look correct to me.

Some comments:

  • We don't have a precedent for documenting macro inputs. I don't think we can express it like `cfg_select!` `{` … `}` because the braces can also be () or [] (also ! is a separate token), and the macro name can be renamed. I would suggest just documenting what the input to the macro is, and not the surrounding invocation syntax.
  • This doesn't handle the difference in behavior of the last branch.
  • This doesn't specify that the non-braced value needs to be an expression of a certain kind.
  • This doesn't handle the optional , behavior.

I'm thinking the grammar would be something closer to:

@root CfgSelect ->
    CfgSelectBranch*
    LastCfgSelectBranch?

CfgSelectBranch ->
    CfgSelectConfigurationPredicate `=>` `{` TokenTree `}`
  | CfgSelectConfigurationPredicate `=>` ExpressionWithBlockX `,`?
  | CfgSelectConfigurationPredicate `=>` ExpressionWithoutBlockX `,`

ExpressionWithBlockX ->
      BlockExpression
    | ConstBlockExpression
    | UnsafeBlockExpression
    | LoopExpression
    | IfExpression
    | MatchExpression

ExpressionWithoutBlockX ->
      LiteralExpression
    | PathExpression
    | OperatorExpression
    | GroupedExpression
    | ArrayExpression
    | AwaitExpression
    | IndexExpression
    | TupleExpression
    | TupleIndexingExpression
    | StructExpression
    | CallExpression
    | MethodCallExpression
    | FieldExpression
    | ClosureExpression
    | AsyncBlockExpression
    | ContinueExpression
    | BreakExpression
    | RangeExpression
    | ReturnExpression
    | UnderscoreExpression
    | MacroInvocation

LastCfgSelectBranch ->
    CfgSelectConfigurationPredicate => ExpressionX `,`?

ExpressionX -> ExpressionWithBlockX | ExpressionWithoutBlockX

CfgSelectConfigurationPredicate ->
    ConfigurationPredicate | `_`

Where the X expressions are from the Expression grammar, but without the outer attributes. If this is correct, we'll need to rework Expression to support that.

Does that make sense?

Copy link
Contributor

@ehuss ehuss Jan 15, 2026

Choose a reason for hiding this comment

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

Though looking again, my suggestion above won't work because we are moving towards a grammar that does not have infinite lookahead for disambiguation. So the LastCfgSelectBranch won't work. I offhand can't think of a way to actually express that...

(Maybe lookahead is required?)

Copy link
Contributor

Choose a reason for hiding this comment

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

Pushed something that might be closer.

@ehuss
Copy link
Contributor

ehuss commented Jan 15, 2026

What does this line do when checking for a closebrace?

I would expect that:

  • If it is wrapped in braces, then it went through the initial braced-token-tree branch above.
  • Otherwise, it's impossible for a branch to start with }.

That is, I would expect that token::Eof is what matters, and it is not possible for it to be token::CloseBrace.

@rustbot
Copy link
Collaborator

rustbot commented Jan 17, 2026

This PR was rebased onto a different master 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.

Comment on lines 479 to 480
ExpressionWithBlockNoAttrs `,`? CfgSelectArms?
| ExpressionWithoutBlockNoAttrs ( `,` CfgSelectArms? )?
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this needs to include { TokenTree }. Otherwise, this wouldn't match other non-expression things like patterns, types, etc.

Copy link
Contributor

@traviscross traviscross Jan 18, 2026

Choose a reason for hiding this comment

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

Yes... updated to:

CfgSelectArms ->
    CfgSelectConfigurationPredicate `=>`
    (
        `{` ^ TokenTree `}` CfgSelectArms?
      | ExpressionWithBlockNoAttrs `,`? CfgSelectArms?
      | ExpressionWithoutBlockNoAttrs ( `,` CfgSelectArms? )?
    )

It needs the cut because if it takes the first branch but then there's a comma after the closing brace, we can't have it backtrack and succeed on the second branch. (Negative lookahead in the second branch would also work for this.)

@folkertdev: Is there a reason for not allowing an optional comma after the closing brace?

Copy link
Contributor

Choose a reason for hiding this comment

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

@ehuss: Do you think it's possible to be exhaustive here? I.e., rather than TokenTree, could we have some more-semantic production that comprised patterns, types, etc., or does not work?

Macros can be renamed when imported and can delimit their bodies with
different tokens, so we don't want to document the outer part of the
call; we just want to document the input to the macro.

Doing this for `cfg_select!` is a bit tricky since an expression with
a block doesn't need to be followed by a comma while an expression
without a block does unless it's the last one.  We don't want to
handle this the way that the `match` grammar does, currently, since we
want to move toward finite lookahead.  So, instead, we handle this
with right recursion in `CfgSelectArms`.

Unlike `match`, outer attributes aren't allowed on the RHS
expressions.  To handle this, we need to refactor
`ExpressionWithoutBlock` and `ExpressionWithBlock`.

We also document that the outer braces are removed during expansion
when the payload is a block expression.

(We called it the arm "payload" after running out of other options.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants