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