Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X 4ca8d8a63 -> d5bfebaac
GROOVY-8590: STC incorrectly infers type of nested method call used in a return stmt (closes #713) (cherry picked from commit 17067350c4e25a8a97cfcbb86682d904aa7e234e) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/d5bfebaa Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d5bfebaa Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d5bfebaa Branch: refs/heads/GROOVY_2_5_X Commit: d5bfebaac8faa5ffe8cc031a4a7412477344d356 Parents: 4ca8d8a Author: John Wagenleitner <[email protected]> Authored: Sat May 19 22:32:20 2018 -0700 Committer: John Wagenleitner <[email protected]> Committed: Tue May 22 16:52:41 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/d5bfebaa/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 61d2f23..d6bdbdb 100644 --- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -3242,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 (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; @@ -3315,6 +3315,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/d5bfebaa/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 + ''' + } + }
