You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2018/09/23 12:22:27 UTC

groovy git commit: GROOVY-8778: Cast short-hand breaks for empty map (better handling for @TypeChecked)

Repository: groovy
Updated Branches:
  refs/heads/master 35a97e9c6 -> 186bf012a


GROOVY-8778: Cast short-hand breaks for empty map (better handling for @TypeChecked)


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

Branch: refs/heads/master
Commit: 186bf012a05f76040bd35a1735fa8c33602832a0
Parents: 35a97e9
Author: Paul King <pa...@asert.com.au>
Authored: Sun Sep 23 22:22:13 2018 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Sun Sep 23 22:22:13 2018 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/control/ResolveVisitor.java |  6 ++--
 .../transform/stc/ConstructorsSTCTest.groovy    | 37 ++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/186bf012/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 99e9cd6..21e05e4 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -1092,7 +1092,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
                     return ce;
                 }
                 else {
-                    // may be we have C[k1:v1, k2:v2] -> should become (C)([k1:v1, k2:v2])
+                    // maybe we have C[k1:v1, k2:v2] -> should become (C)([k1:v1, k2:v2])
                     boolean map = true;
                     for (Expression expression : list.getExpressions()) {
                         if(!(expression instanceof MapEntryExpression)) {
@@ -1108,6 +1108,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
                         }
                         me.setSourcePosition(list);
                         final CastExpression ce = new CastExpression(left.getType(), me);
+                        ce.setCoerce(true);
                         ce.setSourcePosition(be);
                         return ce;
                     }
@@ -1116,7 +1117,8 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
                 // we have C[*:map] -> should become (C) map
                 SpreadMapExpression mapExpression = (SpreadMapExpression) be.getRightExpression();
                 Expression right = transform(mapExpression.getExpression());
-                Expression ce = new CastExpression(left.getType(), right);
+                CastExpression ce = new CastExpression(left.getType(), right);
+                ce.setCoerce(true);
                 ce.setSourcePosition(be);
                 return ce;
             }

http://git-wip-us.apache.org/repos/asf/groovy/blob/186bf012/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy b/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy
index 709ca32..69a62a6 100644
--- a/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy
@@ -105,6 +105,22 @@ class ConstructorsSTCTest extends StaticTypeCheckingTestCase {
             A a = [:]
             assert a != null
         '''
+        assertScript '''
+            class A {
+                int x
+                int y
+            }
+            def a = [:] as A
+            assert a != null
+        '''
+        assertScript '''
+            class A {
+                int x
+                int y
+            }
+            def a = A[:]
+            assert a != null
+        '''
     }
 
     void testConstructMap() {
@@ -128,6 +144,27 @@ class ConstructorsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    void testConstructFromCoercedMap() {
+        assertScript '''
+            class A {
+                int x
+                int y
+            }
+            def a = [x:100, y:200] as A
+            assert a.x == 100
+            assert a.y == 200
+        '''
+        assertScript '''
+            class A {
+                int x
+                int y
+            }
+            def a = A[x:100, y:200]
+            assert a.x == 100
+            assert a.y == 200
+        '''
+    }
+
     void testConstructWithNamedParams() {
         assertScript '''
             class A {