Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 74380b3da -> 63a1e44b2


GROOVY-8319: Improve smart type on list expressions (closes #602)

(cherry picked from commit 70ce561)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/63a1e44b
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/63a1e44b
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/63a1e44b

Branch: refs/heads/GROOVY_2_5_X
Commit: 63a1e44b2aba9e50774c0d6206baad4d3b359857
Parents: 74380b3
Author: alexey.afanasiev <alexey.afanas...@jetbrains.com>
Authored: Fri Sep 15 22:16:11 2017 +0800
Committer: sunlan <sun...@apache.org>
Committed: Sat Sep 16 00:30:34 2017 +0800

----------------------------------------------------------------------
 .../stc/StaticTypeCheckingVisitor.java          |  4 ++-
 .../transform/stc/STCAssignmentTest.groovy      | 28 ++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/63a1e44b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
diff --git 
a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java 
b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 380a952..3c6f24e 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -875,6 +875,8 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
             if (!isAssignableTo(elemType, tupleType)) {
                 addStaticTypeError("Cannot assign value of type " + 
elemType.toString(false) + " to variable of type " + tupleType.toString(false), 
rightExpression);
                 return false; // avoids too many errors
+            } else {
+                storeType(tupleExpression, elemType);
             }
         }
 
@@ -925,7 +927,7 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
             }
         } else if (rightExpression instanceof ListExpression) {
             for (Expression element : ((ListExpression) 
rightExpression).getExpressions()) {
-                ClassNode rightComponentType = element.getType().redirect();
+                ClassNode rightComponentType = this.getType(element);
                 if (!checkCompatibleAssignmentTypes(leftComponentType, 
rightComponentType)
                         && !(isNullConstant(element) && 
!isPrimitiveType(leftComponentType))) {
                     addStaticTypeError("Cannot assign value of type " + 
rightComponentType.toString(false) + " into array of type " + 
lhsType.toString(false), rightExpression);

http://git-wip-us.apache.org/repos/asf/groovy/blob/63a1e44b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy 
b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
index 7b40304..3c00185 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -844,5 +844,33 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase 
{
             assert fooParameterAssignment(null) == 42            
         '''
     }
+
+    void testIntegerArraySmartType() {
+        assertScript '''
+        def m() {
+            def a  = 1
+            Integer[] b = [a]
+        }            
+        '''
+    }
+
+    void testIntegerSecondDimArraySmartType() {
+        assertScript '''
+        def m() {
+            def a = new int[5]
+            int[][] b = [a]
+        }            
+        '''
+    }
+
+    void testMultiAssign() {
+        assertScript '''
+        def m() {
+            def row = ["", "", ""]
+            def (left, right) = [row[0], row[1]]
+            left.toUpperCase()
+        }            
+        '''
+    }
 }
 

Reply via email to