You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/07/26 14:22:12 UTC
[groovy] 03/05: GROOVY-10688: STC: common generics resolver for assign and elvis/ternary
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 9508db373fd5fe47d048e78e0e7edeb508bd6d5f
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Jul 25 14:23:46 2022 -0500
GROOVY-10688: STC: common generics resolver for assign and elvis/ternary
---
.../transform/stc/StaticTypeCheckingVisitor.java | 78 +++++++++++-----------
.../transform/stc/TernaryOperatorSTCTest.groovy | 20 ++++++
2 files changed, 59 insertions(+), 39 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index a38c74f094..0ac085a786 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -841,33 +841,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (rightExpression instanceof ConstructorCallExpression)
inferDiamondType((ConstructorCallExpression) rightExpression, lType);
- if (lType.isUsingGenerics()
- && missesGenericsTypes(resultType)
- // GROOVY-10324, GROOVY-10342, et al.
- && !resultType.isGenericsPlaceHolder()) {
- // unchecked assignment
- // List<Type> list = new LinkedList()
- // Iterable<Type> iter = new LinkedList()
- // Collection<Type> coll = Collections.emptyList()
- // Collection<Type> view = ConcurrentHashMap.newKeySet()
-
- // the inferred type of the binary expression is the type of the RHS
- // "completed" with generics type information available from the LHS
- if (lType.equals(resultType)) {
- // GROOVY-6126, GROOVY-6558, GROOVY-6564, et al.
- if (!lType.isGenericsPlaceHolder()) resultType = lType;
- } else {
- // GROOVY-5640, GROOVY-9033, GROOVY-10220, GROOVY-10235, et al.
- Map<GenericsTypeName, GenericsType> gt = new HashMap<>();
- extractGenericsConnections(gt, resultType, resultType.redirect());
- ClassNode sc = resultType;
- do { sc = getNextSuperClass(sc, lType);
- } while (sc != null && !sc.equals(lType));
- extractGenericsConnections(gt, lType, sc);
-
- resultType = applyGenericsContext(gt, resultType.redirect());
- }
- }
+ // handle unchecked assignment: List<Type> list = []
+ resultType = adjustForTargetType(resultType, lType);
ClassNode originType = getOriginalDeclarationType(leftExpression);
typeCheckAssignment(expression, leftExpression, originType, rightExpression, resultType);
@@ -4245,7 +4220,6 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
* @param type the inferred type of {@code expr}
*/
private ClassNode checkForTargetType(final Expression expr, final ClassNode type) {
- ClassNode sourceType = type;
ClassNode targetType = null;
MethodNode enclosingMethod = typeCheckingContext.getEnclosingMethod();
MethodCall enclosingMethodCall = (MethodCall)typeCheckingContext.getEnclosingMethodCall();
@@ -4266,25 +4240,51 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
if (expr instanceof ClosureExpression) { // GROOVY-10271, GROOVY-10272
- return isSAMType(targetType) ? targetType : sourceType;
+ return isSAMType(targetType) ? targetType : type;
}
- if (expr instanceof ConstructorCallExpression) { // GROOVY-9972, GROOVY-9983
- // GROOVY-10114: type parameter(s) could be inferred from call arguments
- if (targetType == null) targetType = sourceType.getPlainNodeReference();
+ if (targetType == null)
+ targetType = type.getPlainNodeReference();
+ if (type == UNKNOWN_PARAMETER_TYPE) return targetType;
+
+ if (expr instanceof ConstructorCallExpression) { // GROOVY-9972, GROOVY-9983, GROOVY-10114
inferDiamondType((ConstructorCallExpression) expr, targetType);
}
- if (targetType == null) return sourceType;
+ return adjustForTargetType(type, targetType);
+ }
+
+ private static ClassNode adjustForTargetType(final ClassNode resultType, final ClassNode targetType) {
+ if (targetType.isUsingGenerics()
+ && missesGenericsTypes(resultType)
+ // GROOVY-10324, GROOVY-10342, et al.
+ && !resultType.isGenericsPlaceHolder()) {
+ // unchecked assignment
+ // List<Type> list = new LinkedList()
+ // Iterable<Type> iter = new LinkedList()
+ // Collection<Type> col1 = Collections.emptyList()
+ // Collection<Type> col2 = Collections.emptyList() ?: []
+ // Collection<Type> view = ConcurrentHashMap.newKeySet()
+
+ // the inferred type of the binary expression is the type of the RHS
+ // "completed" with generics type information available from the LHS
+ if (targetType.equals(resultType)) {
+ // GROOVY-6126, GROOVY-6558, GROOVY-6564, et al.
+ if (!targetType.isGenericsPlaceHolder()) return targetType;
+ } else {
+ // GROOVY-5640, GROOVY-9033, GROOVY-10220, GROOVY-10235, GROOVY-10688, et al.
+ Map<GenericsTypeName, GenericsType> gt = new HashMap<>();
+ extractGenericsConnections(gt, resultType, resultType.redirect());
+ ClassNode sc = resultType;
+ do { sc = getNextSuperClass(sc, targetType);
+ } while (sc != null && !sc.equals(targetType));
+ extractGenericsConnections(gt, targetType, sc);
- if (!isPrimitiveType(getUnwrapper(targetType)) && !isObjectType(targetType)
- && !sourceType.isGenericsPlaceHolder() && missesGenericsTypes(sourceType)) {
- // unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
- // the inferred type is the RHS type "completed" with generics information from LHS
- return GenericsUtils.parameterizeType(targetType, sourceType.getPlainNodeReference());
+ return applyGenericsContext(gt, resultType.redirect());
+ }
}
- return sourceType != UNKNOWN_PARAMETER_TYPE ? sourceType : targetType;
+ return resultType;
}
private static boolean isTypeSource(final Expression expr, final Expression right) {
diff --git a/src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy b/src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy
index 61deb89ff1..1a8acde043 100644
--- a/src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy
@@ -166,6 +166,20 @@ class TernaryOperatorSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-10688
+ void testTypeParameterTypeParameter3() {
+ assertScript '''
+ class A<T,U> {
+ }
+ <T> void test(
+ A<Double, ? extends T> x) {
+ A<Double, ? extends T> y = x
+ A<Double, ? extends T> z = true ? y : x
+ }
+ test(null)
+ '''
+ }
+
// GROOVY-10271
void testFunctionalInterfaceTarget1() {
for (flag in ['true', 'false']) {
@@ -274,6 +288,12 @@ class TernaryOperatorSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ void testCommonInterface3() {
+ assertScript '''import static java.util.concurrent.ConcurrentHashMap.*
+ Set<Integer> integers = false ? new HashSet<>() : newKeySet()
+ '''
+ }
+
// GROOVY-10130
void testInstanceofGuard() {
assertScript '''