Skip to content

format_args!() interacts strangely with $crate #146114

@theemathas

Description

@theemathas

I'm not sure if this is a bug or not.

macro_rules! foo {
    ($x:ident) => {
        println!(concat!("{", stringify!($x), "}"), $x=1);
    }
}

macro_rules! bar {
    () => {
        foo!($crate);
    }
}

fn main() {
    bar!();
}

I expected the above to compile. Instead, I got the following compile error:

error: there is no argument named `crate`
  --> src/main.rs:3:18
   |
 3 |         println!(concat!("{", stringify!($x), "}"), $x=1);
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
14 |     bar!();
   |     ------ in this macro invocation
   |
   = note: did you intend to capture a variable `crate` from the surrounding scope?
   = note: to avoid ambiguity, `format_args!` cannot capture variables when the format string is expanded from a macro
   = note: this error originates in the macro `concat` which comes from the expansion of the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)

error: named argument never used
  --> src/main.rs:3:56
   |
 3 |         println!(concat!("{", stringify!($x), "}"), $x=1);
   |                  ---------------------------------     ^ named argument never used
   |                  |
   |                  formatting specifier missing
...
14 |     bar!();
   |     ------ in this macro invocation
   |
   = note: this error originates in the macro `foo` which comes from the expansion of the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `playground` (bin "playground") due to 2 previous errors

Meta

Reproducible on the playground with version 1.91.0-nightly (2025-09-01 7aef4bec4bec16cb6204)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-fmtArea: `core::fmt`A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions