[
https://issues.apache.org/jira/browse/GROOVY-11192?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17776801#comment-17776801
]
ASF GitHub Bot commented on GROOVY-11192:
-----------------------------------------
yuhengfdada opened a new pull request, #1970:
URL: https://github.com/apache/groovy/pull/1970
# Bug Description
Something like
```
interface A<K, V> {
}
class B<V> implements A<Long, V>{
}
A<Long, String> a = new B<>()
```
would cause groovyc to fail.
# Analysis
https://issues.apache.org/jira/browse/GROOVY-11192
There is a bug in
`org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor#inferDiamondType`.
The `inferredType` is `java.util.Map<java.lang.Long, java.lang.String> ->
java.util.Map<K, V> `and `cceType` is `MapLong<> -> MapLong<V>`.
After `adjustGenerics()` on line 1124, `cceType` becomes
`MapLong<java.lang.Long, java.lang.String> -> MapLong<V>`, which is incorrect.
It should be `MapLong<java.lang.String> -> MapLong<V>`.
# Fix
If we see (# of generic types for A) > (# of generic types for B's
redirect), then only keep the generic types that correspond to B's redirect
generic types.
A.genericTypes = [Long, String]
B.genericTypes = []
A.redirect().genericTypes = [K, V]
B.redirect().genericTypes = [V]
In this case, find the index of `V` in A.redirect().genericTypes, then find
the corresponding type in A.genericTypes, which is String.
> Code that causes Groovy Compiler Crash
> --------------------------------------
>
> Key: GROOVY-11192
> URL: https://issues.apache.org/jira/browse/GROOVY-11192
> Project: Groovy
> Issue Type: Bug
> Components: Compiler
> Affects Versions: 4.0.15
> Environment: I am running on MacBook Pro, but I think this is a
> probably regardless of environment. I have been able to repeat this bug for
> Groovy versions from 3.0.10 to 4.0.15 and all in between.
> Reporter: John DeRegnaucourt
> Priority: Major
> Labels: Generics, compiler, crash, parametize, type
> Attachments: MapLong-3.groovy,
> TestCompilerCrashOnTemplateArgCount.groovy
>
>
> When attempting to compile the code below, it causes the Groovy compiler to
> crash with:
> java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
> at
> org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec(GenericsUtils.java:494)
> at
> org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec(GenericsUtils.java:480)
> at
> org.codehaus.groovy.ast.tools.GenericsUtils.parameterizeType(GenericsUtils.java:293)
>
> The crash is caused because the GenericType parameter is missing on line 9 in
> TestCompilerCrashOnTemplateArgCount:
> private Map<Long, String> map = new MapLong<>() // causes compiler crash
> ...but
> private Map<Long, String> map2 = new MapLong<String>() // doest not cause
> compiler crash
>
> The compiler is expecting one argument, but a no argument array was
> allocated. When it attempts to access the array at element [0], the compiler
> hits ArrayOutOfBoundsException.
>
> This is super easy to repeat with the two tiny source files attached.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)