[
https://issues.apache.org/jira/browse/GROOVY-12625?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18135673#comment-18135673
]
ASF GitHub Bot commented on GROOVY-12625:
-----------------------------------------
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.
> SC fails with ArrayIndexOutOfBoundsException for ++ or -- on a subscript
> whose element type is not a primitive or wrapper number type
> -------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12625
> URL: https://issues.apache.org/jira/browse/GROOVY-12625
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> @groovy.transform.CompileStatic
> def f() {
> List<BigDecimal> l = [1.0]
> l[0]++
> l
> }
> assert f() == [2.0]
> {code}
> Actual: compilation fails with "General error during instruction selection:
> Index 1 out of bounds for length 1" (ArrayIndexOutOfBoundsException in
> StaticInvocationWriter.loadArguments). The same happens for prefix and
> postfix ++ and -- on a BigDecimal array, a map of BigDecimal values, a list
> of BigInteger, String, Character, char[], and a class with getAt/putAt
> returning BigDecimal.
> Expected: the code compiles and the assertion passes, as it does without the
> annotation and with @TypeChecked.
> The type checker resolves next() or previous() by passing the subscript
> expression to findMethodOrFail. StaticCompilationVisitor then stores next()
> as that expression's binary target, replacing getAt, so l[0] is compiled as a
> call to next with argument 0.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)