Skip to content

Commit

Permalink
GROOVY-9074, GROOVY-10651: STC: propagate generics to super class
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 18, 2024
1 parent 02a8d04 commit 62f666b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import static org.codehaus.groovy.ast.ClassHelper.double_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.findSAM;
import static org.codehaus.groovy.ast.ClassHelper.float_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.getNextSuperClass;
import static org.codehaus.groovy.ast.ClassHelper.getUnwrapper;
import static org.codehaus.groovy.ast.ClassHelper.getWrapper;
import static org.codehaus.groovy.ast.ClassHelper.int_TYPE;
Expand Down Expand Up @@ -1759,8 +1760,20 @@ static void extractGenericsConnections(final Map<GenericsTypeName, GenericsType>
extractGenericsConnections(connections, type.getNodeMetaData("outer.class"), target.getOuterClass()); //GROOVY-10646

} else if (implementsInterfaceOrIsSubclassOf(type, target)) {
ClassNode goal = GenericsUtils.parameterizeType(type, target);
extractGenericsConnections(connections, goal.getGenericsTypes(), target.getGenericsTypes());
ClassNode superClass = getNextSuperClass(type, target);
if (GenericsUtils.hasUnresolvedGenerics(superClass)) {
// propagate type arguments to the super class or interface
Map<GenericsTypeName, GenericsType> spec = new HashMap<>();
if (type.getGenericsTypes() != null) {
extractGenericsConnections(spec, type, type.redirect());
} else if (type.redirect().getGenericsTypes() != null) {
for (GenericsType tp : type.redirect().getGenericsTypes()) {
spec.put(new GenericsTypeName(tp.getName()), new GenericsType(getCombinedBoundType(tp))); //GROOVY-10651
}
}
superClass = applyGenericsContext(spec, superClass);
}
extractGenericsConnections(connections, superClass, target);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ private ClassNode configureWildcardType(final WildcardType wildcardType) {
private ClassNode configureParameterizedType(final ParameterizedType parameterizedType) {
ClassNode base = configureType(parameterizedType.getRawType());
GenericsType[] gts = configureTypeArguments(parameterizedType.getActualTypeArguments());
// fix erasure : ResolveVisitor#resolveWildcardBounding
final int n; if (gts != null && (n = gts.length) > 0) {
for (int i = 0; i < n; i += 1) { GenericsType gt = gts[i];
if (!gt.isWildcard() || gt.getUpperBounds() != null) continue;
ClassNode[] ubs = base.redirect().getGenericsTypes()[i].getUpperBounds();
if (ubs != null && !ClassHelper.OBJECT_TYPE.equals(ubs[0])) gt.getType().setRedirect(ubs[0]);
}
}
base.setGenericsTypes(gts);
return base;
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -804,12 +804,12 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
}

// GROOVY-6266
void testMapKeyGenerics() {
assertScript """
HashMap<String,List<List>> AR=new HashMap<String,List<List>>()
AR.get('key',[['val1'],['val2']])
assert AR.'key'[0] == ['val1']
"""
void testMapGenerics() {
assertScript '''
Map<String, List<List>> map = new HashMap<>()
map.get('key', [(List)['val1'],['val2']])
assert map.key[0] == ['val1']
'''
}

// GROOVY-6311
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
void testDGM_eachWithIndexOnRecursiveIterable() { // GROOVY-10651
['', '<?>'].each { args ->
for (args in ['','<?>']) {
assertScript """
void proc(groovy.transform.stc.TreeNode$args node) {
node.eachWithIndex { child, index ->
Expand Down

0 comments on commit 62f666b

Please sign in to comment.