groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject groovy git commit: GROOVY-7713: CompileStatic checking fails with null returns from Closures (closes #384)
Date Fri, 19 Aug 2016 02:33:20 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 5f86d08b0 -> 31778234c


GROOVY-7713: CompileStatic checking fails with null returns from Closures (closes #384)


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 31778234c9e29e685ef51b38b8cabb8d0b0438d9
Parents: 5f86d08
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Sat Aug 13 23:34:09 2016 -0700
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Thu Aug 18 19:23:45 2016 -0700

----------------------------------------------------------------------
 .../groovy/transform/stc/StaticTypeCheckingVisitor.java |  5 +++--
 src/test/groovy/transform/stc/GenericsSTCTest.groovy    | 12 ++++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/31778234/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 358fe55..0bb9409 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -170,8 +170,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
 
     protected final ReturnAdder.ReturnStatementListener returnListener = new ReturnAdder.ReturnStatementListener()
{
         public void returnStatementAdded(final ReturnStatement returnStatement) {
+            if (returnStatement.getExpression() == ConstantExpression.NULL) return;
+            if (isNullConstant(returnStatement.getExpression())) return;
             checkReturnType(returnStatement);
-            if (returnStatement.getExpression().equals(ConstantExpression.NULL)) return;
             if (typeCheckingContext.getEnclosingClosure()!=null) {
                 addClosureReturnType(getType(returnStatement.getExpression()));
             } else if (typeCheckingContext.getEnclosingMethod() != null) {
@@ -4158,7 +4159,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
     }
 
     protected static boolean isNullConstant(final Expression expression) {
-        return expression instanceof ConstantExpression && ((ConstantExpression)
expression).getValue() == null;
+        return expression instanceof ConstantExpression && ((ConstantExpression)
expression).isNullExpression();
     }
 
     protected ClassNode inferMapExpressionType(final MapExpression map) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/31778234/src/test/groovy/transform/stc/GenericsSTCTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index d5220ad..49697d3 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -1813,6 +1813,18 @@ assert result == 'ok'
         '''
     }
 
+    //GROOVY-7713
+    void testClosureReturnNull() {
+        assertScript '''
+            Closure<String> cl = {
+                if (hashCode() > 0) {
+                    return null
+                }
+                'foo'
+            }
+        '''
+    }
+
     static class MyList extends LinkedList<String> {}
 
     public static class ClassA<T> {


Mime
View raw message