rust-lang / rust-lang/rust

Using `${index()}` inside `${concat()}` fails with "expected identifier or string literal" error

Open
#138,181 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-feature-request F-macro_metavar_expr F-macro_metavar_expr_concat T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![feature(macro_metavar_expr)]
#![feature(macro_metavar_expr_concat)]
#![allow(dead_code)]

macro_rules! state_num {
    ($($state:ident),+) => {
        enum StateNum {
            $(${concat(S, ${index()})} ${ignore($state)}),+
        }
    };
}

state_num!(A, B, C);

fn main() {
    let _s = StateNum::S1;
}

I expected to see this happen: it compiling successfully.

Instead, this happened:

error: expected identifier or string literal
 --> src/main.rs:8:28
  |
8 |             $(${concat(S, ${index()})} ${ignore($state)}),+
  |                            ^^^^^^^^^

error: expected identifier, found `$`
  --> src/main.rs:8:15
   |
7  |         enum StateNum {
   |              -------- while parsing this enum
8  |             $(${concat(S, ${index()})} ${ignore($state)}),+
   |               ^ expected identifier
...
13 | state_num!(A, B, C);
   | ------------------- in this macro invocation
   |
   = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
   = note: this error originates in the macro `state_num` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared type `StateNum`
  --> src/main.rs:16:14
   |
16 |     let _s = StateNum::S1;
   |              ^^^^^^^^ use of undeclared type `StateNum`

For more information about this error, try `rustc --explain E0433`.
Meta

rustc --version --verbose:

rustc 1.87.0-nightly (b74da9613 2025-03-06)
binary: rustc
commit-hash: b74da9613a8cb5ba67a985f71325be0b7b16c0dd
commit-date: 2025-03-06
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/main.rs, especially the nested ${concat(S, ${index()})} expression on line 8, and run it with the nightly compiler version reported in the issue. Investigate how nested macro metavariable expressions are parsed and expanded. Done means the example compiles and StateNum::S1 resolves successfully, with a regression test covering the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.