fix(derive-encode): emit friendly compile errors instead of panicking#307
Open
lohitkolluri wants to merge 1 commit into
Open
fix(derive-encode): emit friendly compile errors instead of panicking#307lohitkolluri wants to merge 1 commit into
lohitkolluri wants to merge 1 commit into
Conversation
756f7a2 to
9fcbe1b
Compare
krisztianfekete
left a comment
Collaborator
There was a problem hiding this comment.
Hey, thanks for the PR, left a few comments!
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>
9fcbe1b to
aeffec1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace
panic!calls in theEncodeLabelSetandEncodeLabelValueproc-macro derives withcompile_error!so unsupported shapes surface a direct diagnostic that points to the#[derive(...)]line the user wrote, instead of the currenterror: proc-macro derive panickedmessage that points into the proc-macro internals.Full changes
panic!sites inderive-encode/src/lib.rsnow emitcompile_error!tokens.#[prometheus(...)]attributes (which previously panicked inside a closure) are validated in a pre-pass so the derive can return acompile_error!token stream before generating the impl.compile_failtest inderive-encode/tests/build/friendly-compilation-error-msg.rscovers unnamed struct, unit struct, enum, union, and unknown-attribute paths. The expected.stderris committed alongside it.Testing
cargo test -p prometheus-client-derive-encode --test libpasses deterministically (with and withoutTRYBUILD=overwrite).cargo clippy -p prometheus-client-derive-encode --all-targets -- -D warningsclean.cargo fmt --check -p prometheus-client-derive-encodeclean.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