Skip to content

Commit

Permalink
GROOVY-8202
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed May 9, 2021
1 parent 776c91f commit 3c7b1b3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> c = { ->\n" + // Cannot assign Closure<Object> to Closure<String>
" 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 3c7b1b3

Please sign in to comment.