Rust Macro Error Diagnostics: Edge Cases?

Does the Rust compiler always manage to point errors to both the inside of the expanded macro and the original macro invocation? Are there any edge cases where it might only point to the expanded code, or where the invocation link is less clear?.

What are you hoping to do with the answer to this question?

(In general, "always" is a hard thing to answer in any meaningful way on its own.)

I'm writing a coding guideline about macros and want to avoid imprecise terms like 'always' when describing how the compiler reports macro-related errors.

I believe the most important thing to know about macro errors is that all attribution of errors to source code happens through the spans of the individual tokens, whether those are original unaltered parsed tokens or macro-created tokens. Therefore, when macros produce tokens, and therefore have to choose spans for those tokens, the author of the macro should have diagnostics in mind when making that choice.

2 Likes

I can't thank you enough, this opened a new gate for me, I dived in-depth into Spans and on the way I discovered HygieneData, ExpnData, and how the rustc tracks errors to their source location and their invocation sit, also, how proc macros have the power to merge and change spans to some degree, so if i have to summarize i can say two things, for declarative macros: every token is tracked through its span, for proc macros: the author has the power to manipulate these spans(by assigning a span to a token).