Repository: groovy Updated Branches: refs/heads/master 17067350c -> 1546b1918
GROOVY-8595: Expected parameter of type XXX but got YYY static compile error Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/1546b191 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/1546b191 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/1546b191 Branch: refs/heads/master Commit: 1546b1918fa1a5a58d54ac6636348ba4e8cf6229 Parents: 1706735 Author: sunlan <[email protected]> Authored: Wed May 23 08:48:13 2018 +0800 Committer: sunlan <[email protected]> Committed: Wed May 23 08:49:24 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/1546b191/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 ef3feaf..27b9d09 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() && !inNestedMethodCall()) { + if (null != typeCheckingContext.getEnclosingReturnStatement() && !isNestedOrSandwichedMethodCall()) { ClassNode inferredType = infer(returnType, typeCheckingContext.getEnclosingMethod().getReturnType()); if (null != inferredType) { returnType = inferredType; @@ -3318,7 +3318,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/1546b191/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 + } + ''' + } +}
