This is an automated email from the ASF dual-hosted git repository. emilles pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push: new 9435d9613e GROOVY-9074, GROOVY-10651: STC: propagate generics to super class 9435d9613e is described below commit 9435d9613e912d018efe567fd4f9e600f55b69a0 Author: Eric Milles <eric.mil...@thomsonreuters.com> AuthorDate: Mon Nov 18 12:23:32 2024 -0600 GROOVY-9074, GROOVY-10651: STC: propagate generics to super class --- .../groovy/transform/stc/StaticTypeCheckingSupport.java | 17 +++++++++++++++-- .../java/org/codehaus/groovy/vmplugin/v8/Java8.java | 8 ++++++++ .../transform/stc/ArraysAndCollectionsSTCTest.groovy | 8 ++++---- .../stc/ClosureParamTypeInferenceSTCTest.groovy | 2 +- src/test/groovy/transform/stc/GenericsSTCTest.groovy | 4 ++-- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java index f79c62daac..1530e0ab24 100644 --- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java +++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java @@ -102,6 +102,7 @@ import static org.codehaus.groovy.ast.ClassHelper.char_TYPE; 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; @@ -1753,8 +1754,20 @@ public abstract class StaticTypeCheckingSupport { 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); } } diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java b/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java index 0b674c117a..5e2901eb03 100644 --- a/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java +++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java @@ -215,6 +215,14 @@ public class Java8 implements VMPlugin { 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; } diff --git a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy index e01f8875f6..5e6fc3ee25 100644 --- a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy +++ b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy @@ -946,11 +946,11 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase { } // GROOVY-6266 - void testMapKeyGenerics() { + void testMapGenerics() { assertScript ''' - Map<String, ? extends List<? extends List>> map = new HashMap<>() - map.get('key',[['val1'],['val2']]) - assert map.'key'[0] == ['val1'] + Map<String, List<List>> map = new HashMap<>() + map.get('key', [(List)['val1'],['val2']]) + assert map.key[0] == ['val1'] ''' } diff --git a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy index 64a4e9a68e..d06982d27c 100644 --- a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy +++ b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy @@ -1264,7 +1264,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 -> diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy index f1dc0c97a9..6f75b44d11 100644 --- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy +++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy @@ -117,10 +117,10 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { void testAddOnListUsingLeftShift3() { shouldFailWithMessages ''' - def list = (List<? extends Number>) [] + List<? extends Number> list = [] list << 1 ''', - 'Cannot call <T> org.codehaus.groovy.runtime.DefaultGroovyMethods#leftShift(java.util.List<T>, T) with arguments [java.util.List<? extends java.lang.Number>, int]' + 'Cannot call <T> org.codehaus.groovy.runtime.DefaultGroovyMethods#leftShift(java.util.List<T>, T) with arguments [java.util.ArrayList<? extends java.lang.Number>, int]' } void testAddOnListWithDiamondUsingLeftShift() {