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/09/03 23:21:59 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-10114: STC: diamond inference: handle ternary without target type

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 7f618e0f71 GROOVY-10114: STC: diamond inference: handle ternary without target type
7f618e0f71 is described below

commit 7f618e0f7126eb63578fbf540a51132b79ea5441
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Mar 15 15:33:04 2022 -0500

    GROOVY-10114: STC: diamond inference: handle ternary without target type
---
 .../transform/stc/StaticTypeCheckingVisitor.java       | 18 +++++++++++++-----
 src/test/groovy/transform/stc/GenericsSTCTest.groovy   | 12 ++++++------
 2 files changed, 19 insertions(+), 11 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 1ab4edd184..198b113f4b 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4248,8 +4248,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
      * @param type the inferred type of {@code expr}
      */
     private ClassNode checkForTargetType(final Expression expr, final ClassNode type) {
-        ClassNode sourceType = Optional.ofNullable(getInferredReturnType(expr)).orElse(type);
-
+        ClassNode sourceType = type;
         ClassNode targetType = null;
         MethodNode enclosingMethod = typeCheckingContext.getEnclosingMethod();
         BinaryExpression enclosingExpression = typeCheckingContext.getEnclosingBinaryExpression();
@@ -4264,11 +4263,20 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
              targetType = enclosingMethod.getReturnType();
         }
 
-        if (targetType == null) return sourceType;
+        if (expr instanceof VariableExpression && hasInferredReturnType(expr)) {
+            sourceType = getInferredReturnType(expr);
+        }
 
-        if (expr instanceof ConstructorCallExpression) {
+        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();
             inferDiamondType((ConstructorCallExpression) expr, targetType);
-        } else if (!isPrimitiveType(getUnwrapper(targetType)) && !targetType.equals(OBJECT_TYPE)
+            return sourceType;
+        }
+
+        if (targetType == null) return sourceType;
+
+        if (!isPrimitiveType(getUnwrapper(targetType)) && !OBJECT_TYPE.equals(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
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index bd966006fb..f00967f7f5 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -526,7 +526,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-8638
-    void _testReturnTypeInferenceWithMethodGenerics18() {
+    void testReturnTypeInferenceWithMethodGenerics18() {
         assertScript '''
             @Grab('com.google.guava:guava:31.1-jre')
             import com.google.common.collect.*
@@ -1111,11 +1111,11 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         ''',
         'Cannot assign A <java.lang.Object> to: A <B>',
         'Cannot assign A <? extends java.lang.Object> to: A <B>',
-        'Cannot call C#m(A <B>) with arguments [A <java.lang.Object>]'/*,
-        'Cannot call C#m(A <B>) with arguments [A <? extends java.lang.Object>]'*/
+        'Cannot call C#m(A <B>) with arguments [A <java.lang.Object>]',
+        'Cannot call C#m(A <B>) with arguments [A <? extends java.lang.Object>]'
     }
 
-    @NotYetImplemented // GROOVY-10114
+    // GROOVY-10114
     void testDiamondInferrenceFromConstructor14a() {
         assertScript '''
             @groovy.transform.TupleConstructor(defaults=false)
@@ -1967,7 +1967,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-10525
-    void _testMethodCallWithClassParameterUnbounded2() {
+    void testMethodCallWithClassParameterUnbounded2() {
         assertScript '''
             @Grab('javax.validation:validation-api:1.1.0.Final')
             import javax.validation.Validator
@@ -4161,7 +4161,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-6760
-    void _testGenericsAtMethodLevelWithGenericsInTypeOfGenericType() {
+    void testGenericsAtMethodLevelWithGenericsInTypeOfGenericType() {
         assertScript '''
             @Grab('com.netflix.rxjava:rxjava-core:0.18.1')
             import rx.Observable