Is there any way to get a trace of where an error occurs within a proc macro?

Viewed 53

I am implementing a proc macro, and testing in another crate. When I compile the client crate, there is an error thrown from the invocation site of the proc macro:

error: proc macro panicked
  --> foo/src/main.rs:17:1

The error is occurring within the proc macro implementation.

This error is not very helpful, because it doesn't point me to where specifically is failing. It's possible to figure it out using unique error messages, but it would be much simpler to have an exact file and line number where the error originates in the proc macro implementation as part of the trace.

Is there any way to achieve this?

1 Answers

There's a nightly rustc flag, -Zproc-macro-backtrace, that prints a backtrace when a proc macro panics.

Note this is a rustc flag, not a Cargo flag, and so you need to invoke it like so:

cargo +nightly rustc -- -Zproc-macro-backtrace
Related