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/05/16 15:44:45 UTC

[groovy] 02/02: GROOVY-10363: STC: fix target type check for type parameter

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

commit 44801fbde2d9cff8933cfd702b3b452b3a660a68
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon May 16 10:31:46 2022 -0500

    GROOVY-10363: STC: fix target type check for type parameter
---
 .../groovy/transform/stc/StaticTypeCheckingVisitor.java     |  4 ++--
 src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy | 13 ++++++++++++-
 2 files changed, 14 insertions(+), 3 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 b62e9e2717..3631754ce6 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4265,8 +4265,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
 
         if (expr instanceof ConstructorCallExpression) {
             inferDiamondType((ConstructorCallExpression) expr, targetType);
-        } else if (!isPrimitiveType(getUnwrapper(targetType))
-                && !OBJECT_TYPE.equals(targetType) && missesGenericsTypes(sourceType)) {
+        } else if (!isPrimitiveType(getUnwrapper(targetType)) && !targetType.equals(OBJECT_TYPE)
+                && !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());
diff --git a/src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy b/src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy
index 063e95a929..91c59fccc2 100644
--- a/src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TernaryOperatorSTCTest.groovy
@@ -132,7 +132,7 @@ class TernaryOperatorSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-10330
-    void testTypeParameterTypeParameter() {
+    void testTypeParameterTypeParameter1() {
         assertScript '''
             class C<T> {
                 T y
@@ -153,6 +153,17 @@ class TernaryOperatorSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-10363
+    void testTypeParameterTypeParameter2() {
+        assertScript '''
+            def <X extends java.util.function.Supplier<Number>> X m(X x, X y) {
+                X z = true ? x : y // z infers as Supplier<Object>
+                return z
+            }
+            assert m(null,null) == null
+        '''
+    }
+
     // GROOVY-10357
     void testAbstractMethodDefault() {
         assertScript '''