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 2021/04/16 21:03:26 UTC

[groovy] branch GROOVY-9033 created (now 9b92fb8)

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

emilles pushed a change to branch GROOVY-9033
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 9b92fb8  GROOVY-9033: STC: resolve "def list = []" to List<Object> not List<E>

This branch includes the following new commits:

     new 9b92fb8  GROOVY-9033: STC: resolve "def list = []" to List<Object> not List<E>

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[groovy] 01/01: GROOVY-9033: STC: resolve "def list = []" to List not List Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 9b92fb861754a0faff8cf383a23c8037d2090366
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Apr 16 16:01:20 2021 -0500

    GROOVY-9033: STC: resolve "def list = []" to List<Object> not List<E>
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  3 +-
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 45 +++++++++++++---------
 .../classgen/asm/sc/BugsStaticCompileTest.groovy   |  2 +-
 .../ginq/provider/collection/GinqAstWalker.groovy  |  4 +-
 4 files changed, 32 insertions(+), 22 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 5daab43..96324c2 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -794,8 +794,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
 
             if (resultType == null) {
                 resultType = lType;
-            } else if (lType.isUsingGenerics() && isAssignment(op) && missesGenericsTypes(resultType)) {
+            } else if (isAssignment(op) && missesGenericsTypes(resultType) && (lType.isUsingGenerics() || lType.equals(OBJECT_TYPE))) {
                 // unchecked assignment
+                // def list = [] // GROOVY-9033
                 // List<Type> list = new LinkedList()
                 // Iterable<Type> iter = new LinkedList()
                 // Collection<Type> coll = Collections.emptyList()
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 4d4ce4e..04d116d 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -146,7 +146,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testReturnTypeInference() {
+    void testReturnTypeInference1() {
         assertScript '''
             class Foo<U> {
                 U method() { }
@@ -156,13 +156,12 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testReturnTypeInferenceWithDiamond() {
+    void testReturnTypeInference2() {
         assertScript '''
-            class Foo<U> {
-                U method() { }
-            }
-            Foo<Integer> foo = new Foo<>()
-            Integer result = foo.method()
+        Object m() {
+          def s = '1234'
+          println 'Hello'
+        }
         '''
     }
 
@@ -217,20 +216,39 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-9033
+    void testReturnTypeInferenceWithMethodGenerics8() {
+        shouldFailWithMessages '''
+            List<String> test() {
+              def x = [].each { }
+              x.add(new Object())
+              return x // List<E>
+            }
+        ''', 'Incompatible generic argument types. Cannot assign java.util.List <java.lang.Object> to: java.util.List <String>'
+    }
+
     void testDiamondInferrenceFromConstructor1() {
         assertScript '''
-            Set<Long> set = new HashSet<>()
+            class Foo<U> {
+                U method() { }
+            }
+            Foo<Integer> foo = new Foo<>()
+            Integer result = foo.method()
         '''
     }
 
     void testDiamondInferrenceFromConstructor2() {
         assertScript '''
-            new HashSet<>(Arrays.asList(0L))
+            Set<Long> set = new HashSet<>()
         '''
     }
 
     void testDiamondInferrenceFromConstructor3() {
         assertScript '''
+            new HashSet<>(Arrays.asList(0L))
+        '''
+
+        assertScript '''
             Set<Number> set = new HashSet<>(Arrays.asList(0L))
         '''
 
@@ -726,15 +744,6 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         ''', 'Cannot find matching method FooWithGenerics#say(java.lang.Object)'
     }
 
-    void testVoidReturnTypeInferrence() {
-        assertScript '''
-        Object m() {
-          def s = '1234'
-          println 'Hello'
-        }
-        '''
-    }
-
     // GROOVY-5237
     void testGenericTypeArgumentAsField() {
         assertScript '''
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
index 1bcca69..095c685 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
@@ -1171,7 +1171,7 @@ assert it.next() == 1G
                 def ift = node.getNodeMetaData(INFERRED_TYPE)
                 assert ift == make(Set)
                 assert ift.isUsingGenerics()
-                assert ift.genericsTypes[0].name=='K'
+                assert ift.genericsTypes[0].name=='java.lang.Object'
             })
             def set = map.keySet()
             def key = set[0]
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
index 15dffdc..785e8e2 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
@@ -820,7 +820,7 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
             }
         })).getExpression(0)
 
-        def extra = []
+        List<Expression> extra = []
         if (enableCount || rowNumberUsed) {
             currentGinqExpression.putNodeMetaData(__RN_USED, true)
             extra << callX(varX(rowNumberName), 'getAndIncrement')
@@ -873,7 +873,7 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
             validateWindowClause(classifierExpr, orderExpr, rowsExpr, rangeExpr, ignoredMethodCallExpressionList)
         }
 
-        def argumentExpressionList = []
+        List<Expression> argumentExpressionList = []
 
         if (classifierExpr) {
             List<Expression> expressionList = ((ArgumentListExpression) classifierExpr).getExpressions()