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

   Under `@CompileStatic`, `++` or `--` on a subscript whose element type is 
not a primitive or a wrapper number type fails to compile:
   
   ```groovy
   @groovy.transform.CompileStatic
   def f() { List<BigDecimal> l = [1.0]; l[0]++; l }
   assert f() == [2.0]
   ```
   ```
   General error during instruction selection: Index 1 out of bounds for length 
1
   ```
   
   The same happens for arrays, maps, `List<String>`, `List<Character>`, 
`char[]` and custom `getAt`/`putAt`, and in 4.0.33, 5.0.8 and 6.0.0. Dynamic 
Groovy and `@TypeChecked` give `[2.0]`.
   
   `StaticTypeCheckingVisitor.visitPrefixOrPostixExpression` resolves `next()` 
or `previous()` by calling `findMethodOrFail` with the operand as the 
expression. When the operand is a subscript, 
`StaticCompilationVisitor.findMethodOrFail` treats it as the operator call and 
stores `next` as its `BINARY_EXP_TARGET`, overwriting `getAt`. 
`BinaryExpressionTransformer` then rewrites `l[0]` into `l.next(0)`, and the 
call writer runs out of parameters. The wrapper-number branch was not affected 
because it already passes a stand-in expression.
   
   For a subscript operand, the visitor now passes a stand-in variable of the 
operand type, placed at the operand's source position. The subscript keeps its 
`getAt` target, the resolved method is still stored on the prefix or postfix 
expression, and errors such as `Cannot find matching method 
java.lang.Object#next()` are reported at the same position as before. With 
#3148 (GROOVY-12622) also in, a side-effecting index such as `l[i++]++` is 
evaluated once.
   
   ## Tests
   
   `testPostfixAndPrefixOnSubscriptOfNumber`, 
`testPostfixAndPrefixOnSubscriptOfNonNumber`, 
`testPostfixAndPrefixOnSubscriptOfCustomGetAtAndPutAt`, 
`testPostfixAndPrefixOnSubscriptOfMissingMapKey` and 
`testPostfixAndPrefixOnSubscriptInClosureLambdaAndStaticMethod` in 
`STCAssignmentTest` fail without the change when run under static compilation 
by `AssignmentsStaticCompileTest`. `testPostfixAndPrefixOnSubscriptOfObject` 
covers the error message. The `groovy.transform.stc`, 
`org.codehaus.groovy.classgen.asm.sc` and `groovy.operator` tests pass. The 
Checkstyle and CodeNarc gates pass.
   


-- 
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