Skip to content

fix(derive-encode): emit friendly compile errors instead of panicking#307

Open
lohitkolluri wants to merge 1 commit into
prometheus:masterfrom
lohitkolluri:fix/269-friendly-compile-errors
Open

fix(derive-encode): emit friendly compile errors instead of panicking#307
lohitkolluri wants to merge 1 commit into
prometheus:masterfrom
lohitkolluri:fix/269-friendly-compile-errors

Conversation

@lohitkolluri

Copy link
Copy Markdown

Summary

Replace panic! calls in the EncodeLabelSet and EncodeLabelValue proc-macro derives with compile_error! so unsupported shapes surface a direct diagnostic that points to the #[derive(...)] line the user wrote, instead of the current error: proc-macro derive panicked message that points into the proc-macro internals.

Full changes

  • All 7 panic! sites in derive-encode/src/lib.rs now emit compile_error! tokens.
  • Unknown #[prometheus(...)] attributes (which previously panicked inside a closure) are validated in a pre-pass so the derive can return a compile_error! token stream before generating the impl.
  • New trybuild compile_fail test in derive-encode/tests/build/friendly-compilation-error-msg.rs covers unnamed struct, unit struct, enum, union, and unknown-attribute paths. The expected .stderr is committed alongside it.

Testing

  • cargo test -p prometheus-client-derive-encode --test lib passes deterministically (with and without TRYBUILD=overwrite).
  • cargo clippy -p prometheus-client-derive-encode --all-targets -- -D warnings clean.
  • cargo fmt --check -p prometheus-client-derive-encode clean.

Notes

For valid inputs the derive behavior is unchanged. The new diagnostics are user-facing; no downstream tooling currently parses these derive errors, so this is not a breaking change.

Issues

Closes #269

Developed with Claude Code assistance.

@lohitkolluri lohitkolluri force-pushed the fix/269-friendly-compile-errors branch from 756f7a2 to 9fcbe1b Compare July 1, 2026 20:46

@krisztianfekete krisztianfekete left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR, left a few comments!

Comment thread derive-encode/src/lib.rs Outdated
Comment thread derive-encode/src/lib.rs Outdated
Use syn::Error::new_spanned(...).to_compile_error() for all invalid
input shapes so diagnostics point at the offending type or attribute
value (e.g. the unknown ident in #[prometheus(unknown)]), rather than
the derive site or a proc-macro panic.

This also removes the double-parse + unwrap for #[prometheus(...)] by
parsing the attribute once during field processing.

Signed-off-by: Lohit Kolluri <lohitkolluri@gmail.com>
@lohitkolluri lohitkolluri force-pushed the fix/269-friendly-compile-errors branch from 9fcbe1b to aeffec1 Compare July 9, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compilation error messages could be better

2 participants