Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X d5bfebaac -> f4301ad11
GROOVY-8595: Expected parameter of type XXX but got YYY static compile error (cherry picked from commit 1546b19) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f4301ad1 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f4301ad1 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f4301ad1 Branch: refs/heads/GROOVY_2_5_X Commit: f4301ad112304f5842014a5d4a21ad0dca0fb438 Parents: d5bfeba Author: sunlan <[email protected]> Authored: Wed May 23 08:48:13 2018 +0800 Committer: sunlan <[email protected]> Committed: Wed May 23 09:27:36 2018 +0800 ---------------------------------------------------------------------- .../stc/StaticTypeCheckingVisitor.java | 9 +++- src/test/groovy/bugs/Groovy8595Bug.groovy | 43 ++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/f4301ad1/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 d6bdbdb..ea3c54b 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() && !inNestedMethodCall()) { + if (null != typeCheckingContext.getEnclosingReturnStatement() && !isNestedOrSandwichedMethodCall()) { ClassNode inferredType = infer(returnType, typeCheckingContext.getEnclosingMethod().getReturnType()); if (null != inferredType) { returnType = inferredType; @@ -3315,7 +3315,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport { } } - private boolean inNestedMethodCall() { + /** + * e.g. a(b()), b() is nested method call + * a().b().c(), a() and b() are sandwiched method call + * + */ + private boolean isNestedOrSandwichedMethodCall() { return typeCheckingContext.getEnclosingMethodCalls().size() > 1; } http://git-wip-us.apache.org/repos/asf/groovy/blob/f4301ad1/src/test/groovy/bugs/Groovy8595Bug.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/bugs/Groovy8595Bug.groovy b/src/test/groovy/bugs/Groovy8595Bug.groovy new file mode 100644 index 0000000..d577622 --- /dev/null +++ b/src/test/groovy/bugs/Groovy8595Bug.groovy @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package groovy.bugs + +import gls.CompilableTestSupport + +class Groovy8595Bug extends CompilableTestSupport { + void testGroovy8595() { + shouldCompile ''' + @groovy.transform.CompileStatic + class Test { + List<Foo> foo() { + return [] + } + + List<String> bar() { + return foo().collect { Foo it -> it.instanceId } + } + } + + @groovy.transform.CompileStatic + class Foo { + String instanceId + } + ''' + } +}
