Skip to content

Add combined contract attribute#118

Merged
robjtede merged 1 commit into
mainfrom
codex-add-combined-contract-attribute
Jul 21, 2026
Merged

Add combined contract attribute#118
robjtede merged 1 commit into
mainfrom
codex-add-combined-contract-attribute

Conversation

@robjtede

@robjtede robjtede commented Jul 6, 2026

Copy link
Copy Markdown
Member

Summary

  • add a new #[contract(...)] attribute for grouping contract clauses in one macro invocation
  • reuse the existing contract parser/codegen path for requires, ensures, invariant, and mode-prefixed clauses
  • update the issue clippy reports unused imports when using both requires and ensures #9 regression to import only contract under deny(unused_imports)

Closes #9.

Validation

  • cargo +1.65.0 test --test ui
  • just clippy
  • just test

Summary by CodeRabbit

  • New Features

    • Added support for combining multiple contract conditions in a single #[contract(...)] attribute.
    • Supports mixing preconditions, postconditions, and debug-only postconditions.
  • Documentation

    • Updated the unreleased changelog with combined contract attribute support.
  • Tests

    • Added coverage for combined contracts and updated an existing example to use the new syntax.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@robjtede, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 37 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 47ede8e7-7e59-464a-af45-5c5c8e6609cc

📥 Commits

Reviewing files that changed from the base of the PR and between 9efeb12 and 04c49d7.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • src/implementation/contract.rs
  • src/implementation/mod.rs
  • src/lib.rs
  • tests/issues.rs
  • tests/ui/pass/combined_contract.rs
📝 Walkthrough

Walkthrough

Adds a public #[contract(...)] attribute that parses multiple contract clauses, integrates them into function generation, and validates the feature through issue and UI tests.

Changes

Combined contract attribute

Layer / File(s) Summary
Contract attribute API and parsing
src/lib.rs, src/implementation/mod.rs, src/implementation/contract.rs
Exposes #[contract(...)], parses comma-separated requires, ensures, and debug contract clauses, and generates the annotated function with those contracts.
Function contract construction
src/implementation/mod.rs
Delegates initial contract construction through new_with_contracts.
Combined contract validation and release notes
tests/issues.rs, tests/ui/pass/combined_contract.rs, CHANGELOG.md
Updates the issue regression test, adds a UI pass test covering multiple clauses, and documents the new attribute.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AnnotatedFunction
  participant contract
  participant ContractClauses
  participant FuncWithContracts
  AnnotatedFunction->>contract: provide clauses and function tokens
  contract->>ContractClauses: parse combined clauses
  ContractClauses-->>contract: return contracts
  contract->>FuncWithContracts: generate contracted function
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a combined contract attribute.
Linked Issues check ✅ Passed The changes address issue #9 by enabling a combined contract form and updating the test to avoid unused imports.
Out of Scope Changes check ✅ Passed The added parser, macro wiring, docs, and tests all support the new combined contract attribute and stay within scope.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex-add-combined-contract-attribute

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@robjtede
robjtede force-pushed the codex-add-combined-contract-attribute branch from 5e9b06e to 1c3b1dd Compare July 20, 2026 12:25
@robjtede
robjtede marked this pull request as ready for review July 20, 2026 12:33
@robjtede
robjtede force-pushed the codex-add-combined-contract-attribute branch 2 times, most recently from 8dbfe10 to 9efeb12 Compare July 20, 2026 16:28

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/ui/pass/combined_contract.rs (1)

3-7: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover combined invariant clauses in the regression test.

This validates requires, ensures, and debug_ensures, but the new API also promises combined invariant clauses. Add one (for example, debug_invariant(x >= 0)) so both invariant parsing and its pre/post generation paths are covered.

Suggested addition
 #[contract(
     requires(x >= 0, "input is non-negative"),
+    debug_invariant(x >= 0),
     ensures(ret >= x),
     debug_ensures(ret == x + 1),
 )]
🤖 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 `@tests/ui/pass/combined_contract.rs` around lines 3 - 7, Add a combined
invariant clause, such as debug_invariant(x >= 0), to the contract attribute in
the combined contract regression test, alongside the existing requires, ensures,
and debug_ensures clauses. Keep the test’s current behavior unchanged while
covering invariant parsing and pre/post generation.
🤖 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.

Inline comments:
In `@src/implementation/contract.rs`:
- Line 20: Replace the infallible syn::parse_quote! call assigning func in the
contract macro with syn::parse2(...), propagating parse failures through
emit_error(err, toks) as done in the other contract macros. Preserve the
existing ItemFn result for valid input while returning a compile error instead
of panicking on invalid macro input.

---

Nitpick comments:
In `@tests/ui/pass/combined_contract.rs`:
- Around line 3-7: Add a combined invariant clause, such as debug_invariant(x >=
0), to the contract attribute in the combined contract regression test,
alongside the existing requires, ensures, and debug_ensures clauses. Keep the
test’s current behavior unchanged while covering invariant parsing and pre/post
generation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 40c64443-cb14-425e-add1-e72d98cf9bb8

📥 Commits

Reviewing files that changed from the base of the PR and between 550c50d and 9efeb12.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • src/implementation/contract.rs
  • src/implementation/mod.rs
  • src/lib.rs
  • tests/issues.rs
  • tests/ui/pass/combined_contract.rs

Comment thread src/implementation/contract.rs Outdated
@robjtede
robjtede force-pushed the codex-add-combined-contract-attribute branch from 9efeb12 to c742ce1 Compare July 21, 2026 00:10
@robjtede
robjtede force-pushed the codex-add-combined-contract-attribute branch from c742ce1 to 04c49d7 Compare July 21, 2026 00:13
@robjtede
robjtede merged commit e80b36d into main Jul 21, 2026
9 of 10 checks passed
@robjtede
robjtede deleted the codex-add-combined-contract-attribute branch July 21, 2026 11:53
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.

clippy reports unused imports when using both requires and ensures

1 participant