Skip to main content

with_spans

Macro with_spans 

Source
with_spans!() { /* proc-macro */ }
Expand description

Provides a way for macro_rules to annotate pieces of syntax with source spans, so that you may improve error messages to point to the correct source span.

Within the scope of an outer with_spans!(...) invocation, you may use spanned!(foo => tokens...) to cause the tokens... to be annotated with the source location attached to foo. The foo must be a single token-tree.

The most common use case is to attribute missing instances in a rules_derive macro to a particular field of the source type. For this use case, you should use the $fieldty token-tree as the source span to attach to.

Why provide with_spans!(...) as a proc macro instead of directly providing spanned!(...) as a proc macro? Because the latter is too restrictive: Rust only allows a limited number of syntactic places where a macro invocation is allowed, and we need to use spanned!(...) in locations where macro invocations are not allowed, such as impl “where” clauses. The two-level with_spans!(... spanned!(...) ...) approach allows us to work around this limitation.