`format_args!()` interacts strangely with `$crate`
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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