Attributes and Good Errors

Parse

Part 3 of 3Updated

Custom attributes must be declared or the compiler rejects them:

#[proc_macro_derive(Describe, attributes(describe))]

Then filter fields by inspecting f.attrs.

Errors are the whole craft. panic! in a proc macro produces a message pointing at the derive with no useful context. Use spans instead:

syn::Error::new_spanned(field, "unsupported field type").to_compile_error()

new_spanned attaches the error to the exact tokens the user wrote, so their editor underlines the right field. The difference between a macro people tolerate and one they like is almost entirely this.

Test with trybuild: it compiles files that should fail and asserts on the exact error output, so you notice when your diagnostics regress.