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/10/11 23:12:10 UTC

[groovy] branch master updated: GROOVY-10295: STC: support "Map m() { [a:1, b:'string'] }"

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 43dd6d5  GROOVY-10295: STC: support "Map<String,Object> m() { [a:1,b:'string'] }"
43dd6d5 is described below

commit 43dd6d5354a87a8e418f21a1a8acdeb89778c78c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Oct 11 18:01:39 2021 -0500

    GROOVY-10295: STC: support "Map<String,Object> m() { [a:1,b:'string'] }"
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  6 +++--
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 28 ++++++++++++++++++----
 2 files changed, 27 insertions(+), 7 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 9be0a80..05dbc53 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2228,7 +2228,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                     addStaticTypeError("Cannot return value of type " + prettyPrintType(type) + " on method returning type " + prettyPrintType(returnType), expression);
                 }
             } else if (implementsInterfaceOrIsSubclassOf(type, returnType)) {
-                checkTypeGenerics(returnType, type, expression);
+                BinaryExpression dummy = assignX(varX("{target}", returnType), expression, expression);
+                ClassNode resultType = getResultType(returnType, ASSIGN, type, dummy); // GROOVY-10295
+                checkTypeGenerics(returnType, resultType, expression);
             }
         }
         return null;
@@ -4404,7 +4406,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             if (leftExpression instanceof VariableExpression) {
                 ClassNode initialType = getOriginalDeclarationType(leftExpression);
 
-                if (isPrimitiveType(right) && initialType.isDerivedFrom(Number_TYPE)) {
+                if (isPrimitiveType(rightRedirect) && initialType.isDerivedFrom(Number_TYPE)) {
                     return getWrapper(right);
                 }
 
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 03de7e4..6ed4581 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -3642,12 +3642,30 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
 
     void testReturnTypeChecking() {
         shouldFailWithMessages '''
-            class Foo {
-                List<String> run() {
-                    [11, 12]
-                }
+            List<String> test() {
+                return [1,2,3]
+            }
+        ''', 'Cannot assign java.util.ArrayList<java.lang.Integer> to: java.util.List<java.lang.String>'
+
+        shouldFailWithMessages '''
+            List<CharSequence> test() {
+                return [1,2,3]
+            }
+        ''', 'Cannot assign java.util.ArrayList<java.lang.Integer> to: java.util.List<java.lang.CharSequence>'
+    }
+
+    // GROOVY-10295
+    void testReturnTypeChecking2() {
+        assertScript '''
+            List<CharSequence> test() {
+                return ['foo','bar','baz']
             }
-        ''', 'Incompatible generic argument types. Cannot assign java.util.List<java.lang.Integer> to: java.util.List<java.lang.String>'
+        '''
+        assertScript '''
+            Map<String,Object> test() {
+                return [date: new Date(), string: new String()]
+            }
+        '''
     }
 
     void testBoundedReturnTypeChecking() {