0

Is there a way to enable backtraces for panics that occur inside a procedural macro?

I have a fairly complicated macro which includes a lot of parse_quote! calls. Currently, one or more are panicking, presumably due to bad syntax. The only way I can figure out which is causing this is by commenting out code until the error stops, then toggling the comments to narrow in on the specific line.

I can't use cargo expand because the macro panics before it emits anything useful, so the effected code is just removed from the command's output.

RUST_BACKTRACE=1 has no effect. RUSTFLAGS="-Z macro-backtrace" also has no effect.

The error message just prints out a very basic message:

error: custom attribute panicked
  --> [insert file here]:57:1
   |
57 | #[entity]
   | ^^^^^^^^^
   |
   = help: message: expected `,`
A.Z.
  • 318
  • 1
  • 8
  • 2
    I would tackle this by testing the macro as a _function_ from within the macro crate itself. – Peter Hall Jan 09 '23 at 23:53
  • @PeterHall How so? I already have it broken up into a dozen or two functions, hence the lack of helpful source code, and as far as I know, you can't run procedural macros in a `#[test]` function. – A.Z. Jan 10 '23 at 00:00
  • 1
    @A.Z. I'm doing this for [my own crate](https://github.com/jcaesar/structstruck/blob/c269a5b858dcdc08897883b270db93257d57729b/src/test.rs#L5-L49) (`recurse_through_definition` is the main macro function): you can run procedural macros in a `#[test]`, as long as that test is in the same crate as the macro. (From there, the macro is really just a piece of normal rust code, operating on normal rust structs.) You may have to rearrange things a bit to work around the `proc_macro`/`proc_macro2` problem though. – Caesar Jan 10 '23 at 00:09
  • You can `eprintln` the `TokenStream` you are trying to debug so it will show in the build output. – pigeonhands Jan 10 '23 at 00:15
  • @Caesar Thanks! I ran the code inside a `#[test]` and I found where the panic was being thrown. I had thought that proc_macro2 was inseparable from the `procedural macro API is used outside of a procedural macro` error, but it turned out to just be the `proc_macro2::TokenStream` into `proc_macro::TokenStream` conversion, which is why I thought macros couldn't be tested like this. Could you write your comment as an answer? – A.Z. Jan 10 '23 at 00:37
  • 1
    Note that if you are the macro author, it is better to return a `compile_error!()` than to panic. – Chayim Friedman Jan 10 '23 at 05:19

0 Answers0