Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X 3f90c03f9 -> bfbb53324
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/bfbb5332 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/bfbb5332 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/bfbb5332 Branch: refs/heads/GROOVY_2_6_X Commit: bfbb5332426df3a9f6d32094d58d637c77c5cf3a Parents: 3f90c03 Author: John Wagenleitner <[email protected]> Authored: Sat May 19 22:32:20 2018 -0700 Committer: John Wagenleitner <[email protected]> Committed: Tue May 22 16:51:11 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/bfbb5332/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 1226c4b..4c0ee64 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/bfbb5332/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 + ''' + } + }
