# Rust Macro Error Diagnostics: Edge Cases?

**URL:** <https://internals.rust-lang.org/t/rust-macro-error-diagnostics-edge-cases/22964>\
**Category:** Uncategorized\
**Created:** [May 21, 2025, 12:33pm UTC](https://internals.rust-lang.org/t/rust-macro-error-diagnostics-edge-cases/22964 "2025-05-21T12:33:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![x0rw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/x0rw/32/13013_2.png) [@x0rw](https://internals.rust-lang.org/u/x0rw)\
**Post date:** [May 21, 2025, 12:33pm UTC](https://internals.rust-lang.org/t/rust-macro-error-diagnostics-edge-cases/22964/1 "2025-05-21T12:33:22Z")

</div>

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?.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [May 21, 2025, 4:04pm UTC](https://internals.rust-lang.org/t/rust-macro-error-diagnostics-edge-cases/22964/2 "2025-05-21T16:04:52Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![x0rw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/x0rw/32/13013_2.png) [@x0rw](https://internals.rust-lang.org/u/x0rw)\
**Post date:** [May 21, 2025, 6:26pm UTC](https://internals.rust-lang.org/t/rust-macro-error-diagnostics-edge-cases/22964/3 "2025-05-21T18:26:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [May 21, 2025, 9:40pm UTC](https://internals.rust-lang.org/t/rust-macro-error-diagnostics-edge-cases/22964/4 "2025-05-21T21:40:21Z")

</div>

I believe the most important thing to know about macro errors is that _all_ attribution of errors to source code happens through the [spans](https://doc.rust-lang.org/proc_macro/struct.Span.html) 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.

---

<div class="post-metadata">

**Author:** ![x0rw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/x0rw/32/13013_2.png) [@x0rw](https://internals.rust-lang.org/u/x0rw)\
**Post date:** [May 21, 2025, 11:10pm UTC](https://internals.rust-lang.org/t/rust-macro-error-diagnostics-edge-cases/22964/5 "2025-05-21T23:10:27Z")

</div>

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](https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/hygiene/struct.ExpnData.html), 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).
