Macro runtime error not helpful 🐛 #114055
Labels
A-debuginfo
Area: Debugging information in compiled programs (DWARF, PDB, etc.)
A-macros
Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
Current output
Desired output
Rationale and extra context
Working with macro, and especially debugging macro code is challenging in Rust. Contrary to all the helpful Rust compile and runtime errors we are used to for non-macro code, error line is not given if any runtime error occurs inside a macro expansion. This quickly becomes challenging, with macros within macros.
It seems like related Issues are also related to this issue, like #100235.
In a related closed discussion about improving compile-time macro errors it is recalled that there are actually 2 users of macros: people that develop them, and people that use them.
In the context of runtime error, it seems logical to give both the line invocation of the macro, as well as the line inside the macro expansion that gives the error, so that both macro users can better report bugs and errors, and developers can easily correct the problem. This information should at least be present in the backtrack of the error (while using RUST_BACKTRACE=1).
Currently, there isn't an automatic way to have Rust provide line numbers within the expanded macros when you encounter a runtime error. Since macro expansion is a compile-time operation, and it abstracts away the internals of the macro from the runtime system, it's seems hard to develop such a system. It's only possible to expand macros using a nightly compile flag of rustc called
-Zunpretty=expanded
, but it's very insufficient, as it just prints a new expanded flag, with no automatic compile or run, making debugging of runtime errors in macro challenging. The proposed improvement would really improve greatly this process, as well a bug reporting.Other cases
Although the runtime error gives the macro invocation line, as well as the code error origin, here
'Option::unwrap()' on a 'None' value"
, this is not really helpful. In a real project scenario, with dozens of unwrap and nested macros, it's quite challenging to rely only on the knowledge that the error is occurring on anunwrap
. I took the example ofunwrap
but it's the same for any runtime error inside a macro.Below is a slightly more complex example:
Compiling and running with either
cargo run
orrustc src/main.rs -o main
,./main
gives:In this situation, it's easy to understand that having more information of the line of the error from within the error invocation, or information about the context, like lines before and after, would be very previous to understand where the error is actually coming from.
Anything else?
This issue has been created by fitz35 and me.
The text was updated successfully, but these errors were encountered: