GROOVY-11192: Diamond inference should work on subclasses with less generic parameters #1970
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.
Bug Description
Something like
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
isjava.util.Map<java.lang.Long, java.lang.String> -> java.util.Map<K, V>
andcceType
isMapLong<> -> MapLong<V>
.After
adjustGenerics()
on line 1124,cceType
becomesMapLong<java.lang.Long, java.lang.String> -> MapLong<V>
, which is incorrect. It should beMapLong<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.