Skip to content

GROOVY-12740: reject statements in contract closures instead of crashing - #3275

Open
codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:fix/contracts-statement-closures
Open

codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:fix/contracts-statement-closures

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

Fixes GROOVY-12740.

A @Requires, @Ensures or @Invariant closure (and a loop @Invariant) whose body holds a return, assert or if statement crashed the compiler with a NullPointerException from AssertStatementCreationUtility: ExpressionUtils added a null condition for every statement that is not an expression statement, and ClosureExpressionValidator never checked statement kinds. A closure that declares an empty parameter list with an explicit arrow, { -> amount > 0 }, crashed too, because ClosureExpression.getParameters() is null for it and AnnotationClosureVisitor passed that to Arrays.asList.

Changes in groovy-contracts:

  • ClosureExpressionValidator reports a compile error for a statement that is not an expression statement, naming the kind: Annotation closure may only contain boolean expressions; return statements are not supported (e.g. use '@Requires({ argument1 > 0 })'). LoopInvariantASTTransformation reports the same for a loop invariant.
  • ExpressionUtils.getBooleanExpressions skips statements that are not expression statements instead of adding null, and gains describeStatementKind.
  • AnnotationClosureVisitor reads the parameters through ClosureUtils.getParametersSafe, so { -> ... } compiles and behaves like the arrow-less closure.

Tests: nine new tests in ClosureExpressionValidationTests and one in LoopInvariantTests cover return, assert and if statements in preconditions, postconditions and class and loop invariants, a statement beside an expression, and the explicit empty parameter list for all three annotations. All ten fail on master with the crashes above; the groovy-contracts module passes with the fix (406 tests).

A @requires, @ensures or @invariant closure (and a loop @invariant)
whose body held a return, assert or if statement crashed the compiler
with a NullPointerException from AssertStatementCreationUtility, because
ExpressionUtils added a null condition for every statement that is not
an expression statement and the validator never checked statement kinds.
The validator now reports a compile error that names the statement kind.

A closure that declares an empty parameter list with an explicit arrow,
for which ClosureExpression.getParameters() is null, crashed too; it is
now accepted and behaves like the arrow-less form.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.4220%. Comparing base (df1fe66) to head (f6d46b4).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...ontracts/ast/visitor/AnnotationClosureVisitor.java 88.8889% 0 Missing and 1 partial ⚠️
.../apache/groovy/contracts/util/ExpressionUtils.java 66.6667% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3275        +/-   ##
==================================================
+ Coverage     74.4140%   74.4220%   +0.0080%     
- Complexity      44379      44408        +29     
==================================================
  Files            1699       1699                
  Lines          151067     151169       +102     
  Branches        28790      28806        +16     
==================================================
+ Hits           112415     112503        +88     
- Misses          28380      28388         +8     
- Partials        10272      10278         +6     
Files with missing lines Coverage Δ
.../contracts/ast/LoopInvariantASTTransformation.java 69.3878% <100.0000%> (+6.8878%) ⬆️
...ontracts/ast/visitor/AnnotationClosureVisitor.java 91.5698% <88.8889%> (+0.7686%) ⬆️
.../apache/groovy/contracts/util/ExpressionUtils.java 85.1064% <66.6667%> (+3.2882%) ⬆️

... and 22 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 8, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: f6d46b4
▶️ Tests: 116733 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

This branch has not been deployed

No deployments
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.

2 participants