groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-8382: Target Typing for empty collections should work for field/property initialisers (closes #637)
Date Thu, 16 Nov 2017 21:13:59 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 762609627 -> c3ccb9c35


GROOVY-8382: Target Typing for empty collections should work for field/property initialisers
(closes #637)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: c3ccb9c3549287f198664cefa04e511cf7a0cf31
Parents: 7626096
Author: paulk <paulk@asert.com.au>
Authored: Thu Nov 16 23:40:14 2017 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Fri Nov 17 07:13:45 2017 +1000

----------------------------------------------------------------------
 .../stc/StaticTypeCheckingVisitor.java          | 23 +++++++++++++++-----
 .../groovy/transform/stc/BugsSTCTest.groovy     | 19 +++++++++++++++-
 2 files changed, 35 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/c3ccb9c3/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 63df8a0..7a983f6 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -194,6 +194,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
 
     protected TypeCheckingContext typeCheckingContext;
     protected DefaultTypeCheckingExtension extension;
+    protected FieldNode currentField;
+    protected PropertyNode currentProperty;
 
     public StaticTypeCheckingVisitor(SourceUnit source, ClassNode cn) {
         this.typeCheckingContext = new TypeCheckingContext(this);
@@ -1580,8 +1582,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
         final boolean osc = typeCheckingContext.isInStaticContext;
         try {
             typeCheckingContext.isInStaticContext = node.isInStaticContext();
+            currentProperty = node;
             super.visitProperty(node);
         } finally {
+            currentProperty = null;
             typeCheckingContext.isInStaticContext = osc;
         }
     }
@@ -1591,6 +1595,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
         final boolean osc = typeCheckingContext.isInStaticContext;
         try {
             typeCheckingContext.isInStaticContext = node.isInStaticContext();
+            currentField = node;
             super.visitField(node);
             Expression init = node.getInitialExpression();
             if (init != null) {
@@ -1607,6 +1612,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
                 }
             }
         } finally {
+            currentField = null;
             typeCheckingContext.isInStaticContext = osc;
         }
     }
@@ -3470,12 +3476,17 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
     // currently just for empty literals, not for e.g. Collections.emptyList() at present
     /// it seems attractive to want to do this for more cases but perhaps not all cases
     private ClassNode checkForTargetType(final Expression expr, final ClassNode type) {
-        if (typeCheckingContext.getEnclosingBinaryExpression() != null && isEmptyCollection(expr))
{
-            int op = typeCheckingContext.getEnclosingBinaryExpression().getOperation().getType();
-            if (isAssignment(op)) {
-                VariableExpression target = (VariableExpression) typeCheckingContext.getEnclosingBinaryExpression().getLeftExpression();
-                return adjustForTargetType(target.getType(), type);
-            }
+        BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
+        if (enclosingBinaryExpression != null && enclosingBinaryExpression instanceof
DeclarationExpression
+                && isEmptyCollection(expr) && isAssignment(enclosingBinaryExpression.getOperation().getType()))
{
+            VariableExpression target = (VariableExpression) enclosingBinaryExpression.getLeftExpression();
+            return adjustForTargetType(target.getType(), type);
+        }
+        if (currentField != null) {
+            return adjustForTargetType(currentField.getType(), type);
+        }
+        if (currentProperty != null) {
+            return adjustForTargetType(currentProperty.getType(), type);
         }
         return type;
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3ccb9c3/src/test/groovy/transform/stc/BugsSTCTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/BugsSTCTest.groovy b/src/test/groovy/transform/stc/BugsSTCTest.groovy
index a6578d4..721f11d 100644
--- a/src/test/groovy/transform/stc/BugsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/BugsSTCTest.groovy
@@ -745,7 +745,7 @@ Printer
         '''
     }
 
-    // GROOVY-8255
+    // GROOVY-8255 and GROOVY-8382
     void testTargetTypingEmptyCollectionLiterals() {
         assertScript '''
             class Foo {
@@ -773,5 +773,22 @@ Printer
             }
             assert new Foo().bar() == [[x:1], [y:2]]
         '''
+        assertScript '''
+            import groovy.transform.*
+            @ToString(includeFields=true)
+            class Foo {
+                List<String> propWithGen = ['propWithGen'] ?: []
+                List propNoGen = ['propNoGen'] ?: []
+                private Map<String, Integer> fieldGen = [fieldGen:42] ?: [:]
+                def bar() {
+                    this.propNoGen = ['notDecl'] ?: [] // not applicable here
+                    List<String> localVar = ['localVar'] ?: []
+                    localVar
+                }
+            }
+            def foo = new Foo()
+            assert foo.bar() == ['localVar']
+            assert foo.toString() == 'Foo([propWithGen], [notDecl], [fieldGen:42])'
+        '''
     }
 }


Mime
View raw message