codeconsole opened a new pull request, #3275:
URL: https://github.com/apache/groovy/pull/3275

   Fixes [GROOVY-12740](https://issues.apache.org/jira/browse/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).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to