liveBook · Manning

Compile-time error

As explained in Write Powerful Rust Macros

A compile error is a compiler diagnostic indicating that source code violates language or compilation rules and cannot be successfully compiled.

A compile-time error is a diagnostic produced when a procedural macro rejects invalid input. A useful diagnostic identifies the problem, points to the relevant source span, and explains how to fix it. section 7.9.1 section 7.12

Instead of panicking when a macro encounters invalid syntax or unsupported input, return a Result containing syn::Error. Convert that error with to_compile_error and return the resulting TokenStream. section 7.9.1

Effective compile-time errors in a procedural macro should:

  • Attach the diagnostic to the offending signature, macro invocation, or expression with a span. section 7.9.1

Rejecting an unsupported function signature

A macro that only transforms functions without an existing Result return type can return syn::Error::new(ast.sig.span(), ...) when it encounters Result<Person, String>. The compiler then reports the requirement beside the function declaration instead of emitting a vague macro panic.

Rejecting an empty panic

When #[panic_to_result] finds panic!() without a message, it returns an error tied to the macro expression: “please make sure every panic in your function has a message.”

How Write Powerful Rust Macros covers it

How deep it goes
Explains spans, Result conversion, contextual messages, empty-input validation, and combining multiple errors.
Examples
Demonstrates rejected Result signatures, empty panic! calls, annotated spans, contextual statements, and combined diagnostics.
What it assumes
Assumes Rust procedural-macro syntax and AST manipulation, while demonstrating Result-based handling instead of panic-based reporting.
What you can do after
Diagnose macro input by returning span-aware syn::Error values and converting them into informative compiler output.
How it connects
Connects procedural macros with Result, syn::Error, source spans, TokenStream, panics, and error-reporting utilities.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.