groovy git commit: Minor refactoring: check whether visiting return statement

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: Minor refactoring: check whether visiting return statement

Daniel.Sun
Repository: groovy
Updated Branches:
  refs/heads/master 71c9616c6 -> 1f0c0ac6e


Minor refactoring: check whether visiting return statement


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

Branch: refs/heads/master
Commit: 1f0c0ac6ece046e56c8f8d3aefe09c4ca3c60af4
Parents: 71c9616
Author: sunlan <[hidden email]>
Authored: Tue May 15 12:17:21 2018 +0800
Committer: sunlan <[hidden email]>
Committed: Tue May 15 12:17:21 2018 +0800

----------------------------------------------------------------------
 .../stc/StaticTypeCheckingVisitor.java          | 11 +++-----
 .../transform/stc/TypeCheckingContext.java      | 29 ++++++++++++++++++++
 2 files changed, 33 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/1f0c0ac6/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
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 72cce39..0ab1ac5 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1986,22 +1986,19 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             super.visitConstructorOrMethod(node, isConstructor);
         }
         if (!isConstructor) {
-            returnAdder.visitMethod(node); // return statement added after visitConstructorOrMethod finished... we can not count these auto-generated return statements(GROOVY-7753), see `this.visitingReturnStatementCnt`
+            returnAdder.visitMethod(node); // return statement added after visitConstructorOrMethod finished... we can not count these auto-generated return statements(GROOVY-7753), see `typeCheckingContext.pushEnclosingReturnStatement`
         }
         typeCheckingContext.popEnclosingMethod();
     }
 
-    // GROOVY-7753 return statement added after visitConstructorOrMethod finished... current solution can not solve auto return
-    private int visitingReturnStatementCnt = 0;
-
     @Override
     public void visitReturnStatement(ReturnStatement statement) {
-        visitingReturnStatementCnt++;
+        typeCheckingContext.pushEnclosingReturnStatement(statement);
         try {
             super.visitReturnStatement(statement);
             returnListener.returnStatementAdded(statement);
         } finally {
-            visitingReturnStatementCnt--;
+            typeCheckingContext.popEnclosingReturnStatement();
         }
     }
 
@@ -3245,7 +3242,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                         if (typeCheckMethodsWithGenericsOrFail(chosenReceiver.getType(), args, mn.get(0), call)) {
                             returnType = adjustWithTraits(directMethodCallCandidate, chosenReceiver.getType(), args, returnType);
 
-                            if (1 == visitingReturnStatementCnt) { // the method call is within return statement, we can try to infer type further
+                            if (null != typeCheckingContext.getEnclosingReturnStatement()) { // the method call is within return statement, we can try to infer type further
                                 ClassNode inferredType = infer(returnType, typeCheckingContext.getEnclosingMethod().getReturnType());
                                 if (null != inferredType) {
                                     returnType = inferredType;

http://git-wip-us.apache.org/repos/asf/groovy/blob/1f0c0ac6/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingContext.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingContext.java b/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingContext.java
index 057403f..13588d0 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingContext.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingContext.java
@@ -28,6 +28,7 @@ import org.codehaus.groovy.ast.expr.MethodCallExpression;
 import org.codehaus.groovy.ast.expr.StaticMethodCallExpression;
 import org.codehaus.groovy.ast.expr.VariableExpression;
 import org.codehaus.groovy.ast.stmt.BlockStatement;
+import org.codehaus.groovy.ast.stmt.ReturnStatement;
 import org.codehaus.groovy.control.CompilationUnit;
 import org.codehaus.groovy.control.ErrorCollector;
 import org.codehaus.groovy.control.SourceUnit;
@@ -54,6 +55,7 @@ public class TypeCheckingContext {
     protected final LinkedList<MethodNode> enclosingMethods = new LinkedList<MethodNode>();
     protected final LinkedList<Expression> enclosingMethodCalls = new LinkedList<Expression>();
     protected final LinkedList<BlockStatement> enclosingBlocks = new LinkedList<BlockStatement>();
+    protected final LinkedList<ReturnStatement> enclosingReturnStatements = new LinkedList<ReturnStatement>();
 
 
     // used for closure return type inference
@@ -216,6 +218,33 @@ public class TypeCheckingContext {
         return enclosingMethods.getFirst();
     }
 
+
+    /**
+     * Pushes a return statement into the return statement stack.
+     * @param returnStatement the return statement to be pushed
+     */
+    public void pushEnclosingReturnStatement(ReturnStatement returnStatement) {
+        enclosingReturnStatements.addFirst(returnStatement);
+    }
+
+    /**
+     * Pops a return statement from the enclosing return statements stack.
+     * @return the popped return statement
+     */
+    public ReturnStatement popEnclosingReturnStatement() {
+        return enclosingReturnStatements.removeFirst();
+    }
+
+    /**
+     * Returns the return statement which is on the top of the stack, or null
+     * if there's no such element.
+     * @return the enclosing return statement on top of the stack, or null if no such element.
+     */
+    public ReturnStatement getEnclosingReturnStatement() {
+        if (enclosingReturnStatements.isEmpty()) return null;
+        return enclosingReturnStatements.getFirst();
+    }
+
     /**
      * Returns the current stack of enclosing methods. The first
      * element is the top of the stack, that is to say the last visited method.

Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun