Skip to content

fix: cfg_select supports non token-tree tokens#21705

Open
A4-Tacks wants to merge 2 commits intorust-lang:masterfrom
A4-Tacks:cfg-select-fallback
Open

fix: cfg_select supports non token-tree tokens#21705
A4-Tacks wants to merge 2 commits intorust-lang:masterfrom
A4-Tacks:cfg-select-fallback

Conversation

@A4-Tacks
Copy link
Member

@A4-Tacks A4-Tacks commented Feb 25, 2026

Fixes #21702

  • Fix single non-curly groups is ignored true => ((),), old: (),, new: ((),)

Example

const _: i32 = cfg_select! { true => 2 + 3, _ => 3 + 4 };

Before this PR

const _: i32 = cfg_select! { true => 2 /* expected a token tree after `=>` */+ 3, _ => 3 + 4 };

After this PR

const _: i32 = 2 + 3;

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 25, 2026
@A4-Tacks A4-Tacks force-pushed the cfg-select-fallback branch from 900fe40 to 441213c Compare February 25, 2026 10:43
);
}
Some(_) => {
let savepoint = iter.savepoint();
Copy link
Contributor

Choose a reason for hiding this comment

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

That's not how rustc does this and this is not the right way. This will break on e.g. paths with commas like foo::<T, U>. You need to parse an expression with the parser.

Copy link
Member Author

Choose a reason for hiding this comment

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

But it still needs to support pat, so I roughly delimited it with commas

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean by "it still need to support pat"? You need to use the parser to parse an expr like with macro_rules matcher.

Copy link
Member Author

Choose a reason for hiding this comment

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

Like let cfg_select! { _ => () } = ();,
but now I realize that it's actually parsing according to the expression, it just happens to be a valid pat

@A4-Tacks A4-Tacks marked this pull request as draft March 5, 2026 15:09
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 5, 2026
- Fix single non-curly groups is ignored `true => ((),)`, old: `(),`, new: `((),)`

Example
---
```rust
const _: i32 = cfg_select! { true => 2 + 3, _ => 3 + 4 };
```

**Before this PR**

```rust
const _: i32 = cfg_select! { true => 2 /* expected a token tree after `=>` */+ 3, _ => 3 + 4 };
```

**After this PR**

```rust
const _: i32 = 2 + 3;
```
@A4-Tacks A4-Tacks force-pushed the cfg-select-fallback branch from 441213c to 56b5dc1 Compare March 5, 2026 16:10
@A4-Tacks A4-Tacks marked this pull request as ready for review March 5, 2026 16:10
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 5, 2026
@A4-Tacks A4-Tacks requested a review from ChayimFriedman2 March 5, 2026 16:10
@A4-Tacks A4-Tacks force-pushed the cfg-select-fallback branch from 56b5dc1 to d50fae3 Compare March 5, 2026 16:17
@A4-Tacks A4-Tacks force-pushed the cfg-select-fallback branch from 00ca67e to b12c79e Compare March 5, 2026 16:44
};

/// Parse one expression, skip comma in turbofish
fn parse_expr<'a>(iter: &mut TtIter<'a>) -> tt::TokenTreesView<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

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

That is definitely not enough. The only way to parse an expression reliably is to invoke the parser.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is definitely not enough.

Is there anything else besides the closure returning the location?

The only way to parse an expression reliably is to invoke the parser.

Do we have an expression parser for TtIter?

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 anything else besides the closure returning the location?

What do you mean?

Do we have an expression parser for TtIter?

No, but you can look at the macro_rules matcher. Look at the function named expect_fragment().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cfg_select] Arms without curly braces are not analyzed correctly

3 participants