Improve contract macro diagnostics#116
Conversation
📝 WalkthroughWalkthroughAttribute macros ChangesMacro error handling and validation
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
7de7db0 to
34c0259
Compare
3af2153 to
9b2c6ac
Compare
9b2c6ac to
8055bcf
Compare
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/implementation/requires.rs (1)
13-22: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winSolid fix: panics replaced with compile errors. Consider matching
invariant's explicit item-kind handling for a clearer message.Right now,
#[requires]on a non-fn item (struct/enum/etc.) surfaces whatever generic messagesynproduces forItemFnparse failure (e.g. "expectedfn"), rather than a purpose-built diagnostic like invariant's"the #[{}] attribute only works on functions and impl blocks". Since this is the same category of misuse the PR is targeting, using an explicitItem-based check (asinvariant.rsdoes) would give consistent, clearer diagnostics here too.♻️ Suggested approach
- let func: ItemFn = match syn::parse2(toks.clone()) { - Ok(func) => func, - Err(err) => { - let error = err.to_compile_error(); - return quote::quote! { - `#error` - `#toks` - }; - } - }; + let item: syn::Item = match syn::parse2(toks.clone()) { + Ok(item) => item, + Err(err) => { + let error = err.to_compile_error(); + return quote::quote! { + `#error` + `#toks` + }; + } + }; + + let func = match item { + syn::Item::Fn(func) => func, + item => { + let error = syn::Error::new_spanned( + &item, + "the #[requires] attribute only works on functions", + ) + .to_compile_error(); + return quote::quote! { + `#error` + `#item` + }; + } + };Same applies to
ensures.rs.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/implementation/requires.rs` around lines 13 - 22, The #[requires] and #[ensures] attribute handlers currently rely on parsing as ItemFn, which produces generic syn errors for non-function items; update the entry points in requires.rs and ensures.rs to do explicit item-kind matching like invariant.rs, so misuse on structs/enums/etc. returns a purpose-built compile error such as “the #[...] attribute only works on functions and impl blocks.” Keep the existing compile-error flow, but replace the ItemFn-only parse failure path with a clearer Item-based check and diagnostic.src/implementation/parse.rs (1)
40-40: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueSimplify
Optionflattening withand_then.
.map(string_lit_value).unwrap_or(None)is equivalent to.and_then(string_lit_value).♻️ Proposed simplification
- let desc = conds.last().map(string_lit_value).unwrap_or(None); + let desc = conds.last().and_then(string_lit_value);🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/implementation/parse.rs` at line 40, Simplify the `Option` handling in the `parse.rs` logic by replacing the `conds.last().map(string_lit_value).unwrap_or(None)` pattern with `and_then(string_lit_value)`. Update the `desc` assignment in the affected parsing code to use the flatter combinator directly, keeping the same behavior while making the expression shorter and clearer.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/implementation/parse.rs`:
- Line 40: Simplify the `Option` handling in the `parse.rs` logic by replacing
the `conds.last().map(string_lit_value).unwrap_or(None)` pattern with
`and_then(string_lit_value)`. Update the `desc` assignment in the affected
parsing code to use the flatter combinator directly, keeping the same behavior
while making the expression shorter and clearer.
In `@src/implementation/requires.rs`:
- Around line 13-22: The #[requires] and #[ensures] attribute handlers currently
rely on parsing as ItemFn, which produces generic syn errors for non-function
items; update the entry points in requires.rs and ensures.rs to do explicit
item-kind matching like invariant.rs, so misuse on structs/enums/etc. returns a
purpose-built compile error such as “the #[...] attribute only works on
functions and impl blocks.” Keep the existing compile-error flow, but replace
the ItemFn-only parse failure path with a clearer Item-based check and
diagnostic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 94e16527-1adf-4518-bcfe-32d82d50e139
📒 Files selected for processing (9)
src/implementation/ensures.rssrc/implementation/invariant.rssrc/implementation/parse.rssrc/implementation/requires.rstests/ui/fail/description_must_be_last.stderrtests/ui/fail/invalid_predicate_syntax.rstests/ui/fail/invalid_predicate_syntax.stderrtests/ui/fail/invariant_on_struct.stderrtests/ui/fail/requires_on_struct.stderr
Summary
Validation
Summary by CodeRabbit
Bug Fixes
Tests