Repository navigation
GROOVY-12740: reject statements in contract closures instead of crashing - #3275
Open
codeconsole wants to merge 1 commit into
Open
codeconsole wants to merge 1 commit into
codeconsole wants to merge 1 commit into
Conversation
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 Report❌ Patch coverage is Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: f6d46b4 Learn more about TestLens at testlens.app/docs. |
This branch has not been deployed
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.
Fixes GROOVY-12740.
A
@Requires,@Ensuresor@Invariantclosure (and a loop@Invariant) whose body holds areturn,assertorifstatement crashed the compiler with aNullPointerExceptionfromAssertStatementCreationUtility:ExpressionUtilsadded a null condition for every statement that is not an expression statement, andClosureExpressionValidatornever checked statement kinds. A closure that declares an empty parameter list with an explicit arrow,{ -> amount > 0 }, crashed too, becauseClosureExpression.getParameters()is null for it andAnnotationClosureVisitorpassed that toArrays.asList.Changes in
groovy-contracts:ClosureExpressionValidatorreports 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 })').LoopInvariantASTTransformationreports the same for a loop invariant.ExpressionUtils.getBooleanExpressionsskips statements that are not expression statements instead of adding null, and gainsdescribeStatementKind.AnnotationClosureVisitorreads the parameters throughClosureUtils.getParametersSafe, so{ -> ... }compiles and behaves like the arrow-less closure.Tests: nine new tests in
ClosureExpressionValidationTestsand one inLoopInvariantTestscover 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; thegroovy-contractsmodule passes with the fix (406 tests).