diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java index e76ba7f337..79c07fa85f 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java @@ -723,6 +723,56 @@ public void testTypeChecked8103() { runNegativeTest(sources, ""); } + @Test + public void testTypeChecked8202() { + //@formatter:off + String[] sources = { + "Main.groovy", + "void proc() {\n" + + "}\n" + + "@groovy.transform.TypeChecked\n" + + "String test0(flag) {\n" + + " if (flag) {\n" + + " 'foo'\n" + + " } else {\n" + + " proc()\n" + + " }\n" + + "}\n" + + "@groovy.transform.TypeChecked\n" + + "String test1(flag) {\n" + + " Closure c = { ->\n" + + " if (flag) {\n" + + " 'bar'\n" + + " } else {\n" + + " proc()\n" + + " null\n" + + " }\n" + + " }\n" + + " c.call()\n" + + "}\n" + + "@groovy.transform.TypeChecked\n" + + "String test2(flag) {\n" + + " Closure c = { ->\n" + // Cannot assign Closure to Closure + " if (flag) {\n" + + " 'baz'\n" + + " } else {\n" + + " proc()\n" + + " }\n" + + " }\n" + + " c.call()\n" + + "}\n" + + "print test0(true)\n" + + "print test1(true)\n" + + "print test2(true)\n" + + "print test0(false)\n" + + "print test1(false)\n" + + "print test2(false)\n", + }; + //@formatter:on + + runConformTest(sources, "foobarbaznullnullnull"); + } + @Test public void testTypeChecked8909() { //@formatter:off diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index d0b151cc26..1fbca7f663 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -2677,10 +2677,11 @@ protected ClassNode checkReturnType(final ReturnStatement statement) { } protected void addClosureReturnType(ClassNode returnType) { - // GRECLIPSE add -- GROOVY-9971 + // GRECLIPSE add -- GROOVY-8202, GROOVY-9971 if (StaticTypeCheckingSupport.isGStringOrGStringStringLUB(returnType) && STRING_TYPE.equals( - getInferredReturnType(typeCheckingContext.getEnclosingClosure().getClosureExpression()))) - returnType = STRING_TYPE; + getInferredReturnType(typeCheckingContext.getEnclosingClosure().getClosureExpression()))) { + typeCheckingContext.getEnclosingClosure().addReturnType(STRING_TYPE); + } else if (!VOID_TYPE.equals(returnType)) // GRECLIPSE end typeCheckingContext.getEnclosingClosure().addReturnType(returnType); } diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index daf003a333..6c81fdbb4c 100644 --- a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -2410,11 +2410,11 @@ protected ClassNode checkReturnType(final ReturnStatement statement) { } protected void addClosureReturnType(final ClassNode returnType) { - // GRECLIPSE add -- GROOVY-9971 + // GRECLIPSE add -- GROOVY-8202, GROOVY-9971 if (StaticTypeCheckingSupport.isGStringOrGStringStringLUB(returnType) && STRING_TYPE.equals( getInferredReturnType(typeCheckingContext.getEnclosingClosure().getClosureExpression()))) { typeCheckingContext.getEnclosingClosure().addReturnType(STRING_TYPE); - } else + } else if (!VOID_TYPE.equals(returnType)) // GRECLIPSE end typeCheckingContext.getEnclosingClosure().addReturnType(returnType); } diff --git a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index e5a31b4c48..4606924f08 100644 --- a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -2326,6 +2326,9 @@ protected void addClosureReturnType(final ClassNode returnType) { && STRING_TYPE.equals(getInferredReturnType(enclosingClosure.getClosureExpression()))) { // GROOVY-9971: convert GString to String at the point of return enclosingClosure.addReturnType(STRING_TYPE); + // GRECLIPSE add -- GROOVY-8202 + } else if (VOID_TYPE.equals(returnType)) { + // GRECLIPSE end } else { enclosingClosure.addReturnType(returnType); }