Skip to content

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

Open
@MonterraByte

Description

@MonterraByte

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.F-macro_metavar_expr`#![feature(macro_metavar_expr)]`F-macro_metavar_expr_concat`#![feature(macro_metavar_expr_concat)]`T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions