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-8590: STC incorrectly infers type of nested method call used in a return stmt (closes #713)
Date Tue, 22 May 2018 23:50:14 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 416a69371 -> 17067350c


GROOVY-8590: STC incorrectly infers type of nested method call used in a return stmt (closes
#713)


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

Branch: refs/heads/master
Commit: 17067350c4e25a8a97cfcbb86682d904aa7e234e
Parents: 416a693
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Sat May 19 22:32:20 2018 -0700
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Tue May 22 16:48:38 2018 -0700

----------------------------------------------------------------------
 .../transform/stc/StaticTypeCheckingVisitor.java      |  6 +++++-
 src/test/groovy/transform/stc/BugsSTCTest.groovy      | 14 ++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/17067350/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 c538352..ef3feaf 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -3245,7 +3245,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
                         if (typeCheckMethodsWithGenericsOrFail(chosenReceiver.getType(),
args, mn.get(0), call)) {
                             returnType = adjustWithTraits(directMethodCallCandidate, chosenReceiver.getType(),
args, returnType);
 
-                            if (null != typeCheckingContext.getEnclosingReturnStatement())
{ // the method call is within return statement, we can try to infer type further
+                            if (null != typeCheckingContext.getEnclosingReturnStatement()
&& !inNestedMethodCall()) {
                                 ClassNode inferredType = infer(returnType, typeCheckingContext.getEnclosingMethod().getReturnType());
                                 if (null != inferredType) {
                                     returnType = inferredType;
@@ -3318,6 +3318,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
         }
     }
 
+    private boolean inNestedMethodCall() {
+        return typeCheckingContext.getEnclosingMethodCalls().size() > 1;
+    }
+
     /**
      * A special method handling the "withTrait" call for which the type checker knows more
than
      * what the type signature is able to tell. If "withTrait" is detected, then a new class
node

http://git-wip-us.apache.org/repos/asf/groovy/blob/17067350/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 ef570c6..37e0a99 100644
--- a/src/test/groovy/transform/stc/BugsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/BugsSTCTest.groovy
@@ -791,4 +791,18 @@ Printer
             assert foo.toString() == 'Foo([propWithGen], [notDecl], [fieldGen:42])'
         '''
     }
+
+    //GROOVY-8590
+    void testNestedMethodCallInferredTypeInReturnStmt() {
+        assertScript '''
+            class Source {
+                Object getValue() { '32' }
+            }
+            int m(Source src) {
+                return Integer.parseInt((String) src.getValue())
+            }
+            assert m(new Source()) == 32
+        '''
+    }
+
 }


Mime
View raw message