Skip to content

fix: cfg_select supports non token-tree tokens#21705

Merged
ChayimFriedman2 merged 1 commit intorust-lang:masterfrom
A4-Tacks:cfg-select-fallback
Mar 6, 2026
Merged

fix: cfg_select supports non token-tree tokens#21705
ChayimFriedman2 merged 1 commit intorust-lang:masterfrom
A4-Tacks:cfg-select-fallback

Conversation

@A4-Tacks
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@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 2 times, most recently 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
Copy Markdown
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
Copy Markdown
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
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 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().

- 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 b12c79e to c47f4e0 Compare March 6, 2026 10:01
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 6, 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.

@A4-Tacks A4-Tacks requested a review from ChayimFriedman2 March 6, 2026 10:27
@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Mar 6, 2026
Merged via the queue into rust-lang:master with commit 23cc64a Mar 6, 2026
17 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 6, 2026
@A4-Tacks A4-Tacks deleted the cfg-select-fallback branch March 6, 2026 14:04
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.

[cfg_select] Arms without curly braces are not analyzed correctly

3 participants