Skip to content

fix: don't error on tail comma for some macro - #23134

Merged
ChayimFriedman2 merged 3 commits into
rust-lang:masterfrom
A4-Tacks:str-macro-tail-comma
Aug 17, 2026
Merged

ChayimFriedman2 merged 3 commits into
rust-lang:masterfrom
A4-Tacks:str-macro-tail-comma

Conversation

@A4-Tacks

@A4-Tacks A4-Tacks commented Aug 13, 2026

Copy link
Copy Markdown
Member

Fixes #23132

Example

const _: &str = env!("PATH",);

Before this PR

/* expand error: expected string literal */

After this PR

const _: &str = "/usr/bin:/bin";

Example
---
```rust
const _: &str = env!("PATH",);
```

**Before this PR**

```
/* expand error: expected string literal */
```

**After this PR**

```rust
const _: &str = "/usr/bin:/bin";
```
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 13, 2026

fn parse_string(tt: &tt::TopSubtree) -> Result<(Symbol, Span), ExpandError> {
let mut tt = TtElement::Subtree(tt.top_subtree(), tt.iter());
let expect_literal = |span| ExpandError::other(span, "expected string literal");

@ChayimFriedman2 ChayimFriedman2 Aug 16, 2026

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.

This shouldn't be here. parse_string() is shared to many macros, not just env!().

View changes since the review

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.

These macros need to support trailing commas

The only difference is that env!() also has an optional additional parameter

This implementation is very clean.
Perhaps we can add a boolean flag that only allows tokens after commas in env!()?

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.

Yes they do, but they do not need to support a second parameter.

I'll leave it to you whether to accept a bool in this method or to make env!() parse by itself, but do note that we'll want to use the error message in env!() which complicates a shared implementation.

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 do note that we'll want to use the error message in env!()

Note that in the current implementation, the variable is extended to "UNRESOLVED_ENV_VAR" when it does not exist.
If you want to maintain this behavior and support error strings, the signature of parse_string needs to be modified to ExpandResult<Option<(Symbol, Span)>>

This is not convenient to handle, and r-a has some false positives on env!().
Currently, the implementation of error quiet is better

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.

Fair enough, but I still do not want to parse two parameters in all macros. A boolean will be fine.

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.

Done

@ChayimFriedman2 ChayimFriedman2 left a comment

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.

LGTM with one nit.

View changes since this review


fn parse_string(tt: &tt::TopSubtree) -> Result<(Symbol, Span), ExpandError> {
let mut tt = TtElement::Subtree(tt.top_subtree(), tt.iter());
fn parse_string(tt: &tt::TopSubtree, rest: bool) -> Result<(Symbol, Span), ExpandError> {

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.

Maybe call it allow_second_arg? This looks more descriptive.

@ChayimFriedman2
ChayimFriedman2 added this pull request to the merge queue Aug 17, 2026
Merged via the queue into rust-lang:master with commit 20146aa Aug 17, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 17, 2026
@A4-Tacks
A4-Tacks deleted the str-macro-tail-comma branch August 17, 2026 13:41
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.

FP expected string literal env!("PATH",);

3 participants