sak
sak

Reputation: 3307

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

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?

Upvotes: 6

Views: 3592

Answers (1)

Chayim Friedman
Chayim Friedman

Reputation: 71380

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

You can also use RUSTFLAGS:

RUSTFLAGS="-Zproc-macro-backtrace" cargo check

Upvotes: 6

Related Questions