Macro hardening#151
Open
Wietse wants to merge 3 commits into
Open
Conversation
Collaborator
|
Can you run cargo fmt? The build fails because of this. |
Author
Done. (I've added a pre-push hook locally so this shouldn't happen again). |
Convert the panic / unwrap sites in xee-xpath-macros to bail_spanned!
errors so that bad #[xpath_fn] signatures produce compile errors
pointing at the user's code, instead of bare panics at macro
expansion time.
Sites converted:
- parse.rs:49 — `expect("Signature not found")` when #[xpath_fn]
is invoked with keyword args only (no signature string).
- convert.rs — six panic sites for unsupported signature shapes:
NonEmpty occurrence (`+`), named element tests (`element(foo)`),
non-`element` / non-`item` kind tests, typed array tests,
typed map tests, and atomic types without a Rust wrapper.
The convert.rs sites threaded `fn_arg: &syn::FnArg` through the
internal helpers so each error's span points at the offending
Rust argument. The public surface (`convert_sequence_type`) is
unchanged; only internal helpers gained a parameter.
Adds insta snapshot tests for each converted site, pinning the
error message text and confirming the error path is reached
rather than the panic.
No semantic change to the macro's happy path. Conformance suite
unchanged: Passed 20221, Failed 0, Error 0.
Two review findings on the first hardening pass: 1. (High) Arity mismatch between the XPath signature and the Rust function still panicked with a bare `index out of bounds` from `ast.sig.inputs[i + adjust]` in `make_wrapper`. The new `bail_spanned!` paths in convert.rs never got a chance to run for this case. Add an explicit bounds check after `adjust` is determined, surfacing a `syn::Error` pointing at the function name with both counts and (when relevant) a note about how many arguments were injected. Adds three regression tests: too-few Rust args, too-many Rust args, and a mismatch under `context` injection. 2. (Low) `convert_kind_test`'s diagnostic for `Element(Some(_))` reported "named element tests" but the AST shape (ElementOrAttributeTest with name_or_wildcard + optional type_name) also covers `element(*, xs:string)` and other typed/wildcard forms. Generalise the wording to "constrained element tests" with examples, and add a snapshot for `element(*, xs:string)` to lock in coverage.
Author
|
Rebased onto main after you fixed the clippy warnings yourself. If you want to avoid these surprises you could pin the Rust toolchain version in CI. |
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.
xee-xpath-macros currently panics at macro-expansion time when given certain bad
#[xpath_fn]inputs, with no span pointing at the user's code. This PR replaces the panic sites withbail_spanned!errors so users see clean compile errors at the right location:#[xpath_fn(context_first)]NonEmpty+ occurrence in signatureselement(foo),element(*, xs:string))attribute(),text(),comment(), ...)Each error is covered by an insta snapshot test pinning the message text. To give meaningful spans,
fn_arg: &syn::FnArgis threaded through the internal helpers inconvert.rs. Public macro API unchanged.No semantic change to the macro's happy path. XPath conformance unchanged: 20,221 passing, 0 failing.