rust-lang / rust-lang/rust

`format_args!()` interacts strangely with `$crate`

Open
#146,114 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-fmt A-macros C-bug T-compiler T-lang T-libs
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

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)

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 src/main.rs reproducer and the format_args!/println! expansion shown in the issue. Run it with the reported Rust 1.91.0-nightly version and trace how foo!, bar!, concat!, and $crate are expanded. Done means determining whether the compilation result is intended and resolving or documenting the behavior with coverage for this 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
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.