Open
Description
I tried this code:
// replicate-proc-macros/lib.rs
use proc_macro::TokenStream;
#[proc_macro_derive(Happiness)]
pub fn happiness(_item: TokenStream) -> TokenStream {
r#"
impl Life {
pub const THING: &'static str = "secret";
pub fn secret() -> u32 {
42
}
}
"#.parse().unwrap()
}
// replicate/main.rs
#![feature(decl_macro)]
use replicate_proc_macros::*;
// macro_rules! add_the_answer {
macro add_the_answer {
($(#[$($meta:meta)*])* struct $name:ident;) => {
$(#[$($meta)*])*
struct $name;
impl $name {
pub fn the_answer() -> u32 {
assert_eq!(Self::THING, "secret");
Self::secret()
}
}
}
}
add_the_answer! {
#[derive(Happiness)]
struct Life;
}
fn main() {
println!("the {} is {}", Life::THING, Life::secret());
}
When using macro_rules!
instead of the new macros v2 syntax, the above code compiles and prints
the secret is 42
as expected.
However, when using macros v2 syntax, the compiler is unable to find the associated Life::THING
and Life::secret
items from inside Life::the_answer
and thus fails compilation:
error[E0599]: no associated item named `THING` found for struct `Life` in the current scope
--> src/main.rs:13:34
|
9 | struct $name;
| ------------- associated item `THING` not found for this struct
...
13 | assert_eq!(Self::THING, "secret");
| ^^^^^ associated item not found in `Life`
...
20 | / add_the_answer! {
21 | | #[derive(Happiness)]
22 | | struct Life;
23 | | }
| |_- in this macro invocation
|
= note: this error originates in the macro `add_the_answer` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0599]: no function or associated item named `secret` found for struct `Life` in the current scope
--> src/main.rs:14:23
|
9 | struct $name;
| ------------- function or associated item `secret` not found for this struct
...
14 | Self::secret()
| ^^^^^^ function or associated item not found in `Life`
...
20 | / add_the_answer! {
21 | | #[derive(Happiness)]
22 | | struct Life;
23 | | }
| |_- in this macro invocation
|
= note: this error originates in the macro `add_the_answer` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0599`.
error: could not compile `replicate` (bin "replicate") due to 2 previous errors
Interestingly, notice that the main function still compiles, which implies that the proc macro still runs and the associated items are still generated at some point during compilation.
Meta
rustc --version --verbose
:
rustc 1.73.0-nightly (33a2c2487 2023-07-12)
binary: rustc
commit-hash: 33a2c2487ac5d9927830ea4c1844335c6b9f77db
commit-date: 2023-07-12
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
Metadata
Metadata
Assignees
Labels
Area: Declarative macros 2.0 (#39412)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: Procedural macrosArea: Name/path resolution done by `rustc_resolve` specificallyCategory: This is a bug.`#![feature(decl_macro)]`Relevant to the compiler team, which will review and decide on the PR/issue.