Using proc-macro for local and user crates

⚓ rust    📅 2025-07-05    👤 surdeus    👁️ 2      

surdeus

My crate had an internal proc-macro, which used crate:: to access all values defined inside.
Now I need to extend this crate and allow users to use it as well. Since there is no $crate meta-variable for proc-macro, I defined this...

use std::cell::Cell;

thread_local! {
    pub static MODULE: Cell<&'static syn::Ident> = {
        let crate_name = std::env::var("CARGO_PKG_NAME").unwrap();
        let module = match crate_name.as_str() {
            "my_crate" => "crate",
            _ => "my_crate",
        };
        Cell::new(Box::leak(Box::new(ident(module))))
    }
}

... with the idea of using it like so:

let module = MODULE.get();
syn::parse_quote! { #module::fun()  }

Sadly, the second I try to access this identifier in syn::parse_quote! the compiler crashes:

$ cargo check
error: could not compile `my_crate` (lib)
Caused by:
  process didn't exit successfully: ...  (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

The solution I came up with is hacky at best, so what would be the proper way of doing this?
Also do I understand correctly that this is a bug in the compiler? It happens both on nightly and stable-1.86.

1 post - 1 participant

Read full topic

🏷️ rust_feed