Skip to content

Commit

Permalink
GROOVY-9907
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 26, 2021
1 parent 623da1d commit b9cfd0a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -776,4 +776,24 @@ public void testTypeChecked9903() {
"that you either use an explicit class or @DelegatesTo.Target with a correct id\n" +
"----------\n");
}

@Test
public void testTypeChecked9907() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"Integer foo(x) {\n" +
" if (x instanceof Integer) {\n" +
" def bar = { -> return x }\n" +
" return bar.call()\n" +
" }\n" +
" return 0\n" +
"}\n" +
"println(foo(1))\n",
};
//@formatter:on

runConformTest(sources, "1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
protected final ReturnAdder.ReturnStatementListener returnListener = new ReturnAdder.ReturnStatementListener() {
public void returnStatementAdded(final ReturnStatement returnStatement) {
if (isNullConstant(returnStatement.getExpression())) return;
/* GRECLIPSE edit -- GROOVY-9907
checkReturnType(returnStatement);
if (typeCheckingContext.getEnclosingClosure() != null) {
addClosureReturnType(getType(returnStatement.getExpression()));
*/
ClassNode returnType = checkReturnType(returnStatement);
if (typeCheckingContext.getEnclosingClosure() != null) {
addClosureReturnType(returnType);
// GRECLIPSE end
} else if (typeCheckingContext.getEnclosingMethod() != null) {
} else {
throw new GroovyBugError("Unexpected return statement at "
Expand Down Expand Up @@ -692,7 +698,11 @@ public void visitVariableExpression(VariableExpression vexp) {
*/

if (!(accessedVariable instanceof DynamicVariable)) {
/* GRECLIPSE edit -- GROOVY-9907
if (typeCheckingContext.getEnclosingClosure() == null) {
*/
{
// GRECLIPSE end
VariableExpression variable = null;
if (accessedVariable instanceof Parameter) {
variable = new ParameterVariableExpression((Parameter) accessedVariable);
Expand Down Expand Up @@ -2332,6 +2342,7 @@ private ClassNode infer(ClassNode target, ClassNode source) {

protected ClassNode checkReturnType(final ReturnStatement statement) {
Expression expression = statement.getExpression();
/* GRECLIPSE edit -- GROOVY-9907
ClassNode type = getType(expression);
if (typeCheckingContext.getEnclosingClosure() != null) {
Expand All @@ -2341,8 +2352,19 @@ protected ClassNode checkReturnType(final ReturnStatement statement) {
if ((expression instanceof VariableExpression) && hasInferredReturnType(expression)) {
type = expression.getNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE);
}
*/
ClassNode type;
if (expression instanceof VariableExpression && hasInferredReturnType(expression)) {
type = expression.getNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE);
} else {
type = getType(expression);
}
if (typeCheckingContext.getEnclosingClosure() != null) {
return type;
}
// GRECLIPSE end
MethodNode enclosingMethod = typeCheckingContext.getEnclosingMethod();
if (enclosingMethod != null && typeCheckingContext.getEnclosingClosure() == null) {
if (enclosingMethod != null) {
if (!enclosingMethod.isVoidMethod()
&& !type.equals(void_WRAPPER_TYPE)
&& !type.equals(VOID_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
@Override
public void returnStatementAdded(final ReturnStatement returnStatement) {
if (isNullConstant(returnStatement.getExpression())) return;
/* GRECLIPSE edit -- GROOVY-9907
checkReturnType(returnStatement);
if (typeCheckingContext.getEnclosingClosure() != null) {
addClosureReturnType(getType(returnStatement.getExpression()));
*/
ClassNode returnType = checkReturnType(returnStatement);
if (typeCheckingContext.getEnclosingClosure() != null) {
addClosureReturnType(returnType);
// GRECLIPSE end
} else if (typeCheckingContext.getEnclosingMethod() == null) {
throw new GroovyBugError("Unexpected return statement at " + returnStatement.getLineNumber() + ":" + returnStatement.getColumnNumber() + " " + returnStatement.getText());
}
Expand Down Expand Up @@ -630,7 +636,11 @@ public void visitVariableExpression(final VariableExpression vexp) {
}
}
}
/* GRECLIPSE edit -- GROOVY-9907
} else if (enclosingClosure == null) {
*/
} else {
// GRECLIPSE end
VariableExpression localVariable;
if (accessedVariable instanceof Parameter) {
Parameter parameter = (Parameter) accessedVariable;
Expand Down Expand Up @@ -2161,6 +2171,7 @@ private ClassNode infer(final ClassNode target, final ClassNode source) {

protected ClassNode checkReturnType(final ReturnStatement statement) {
Expression expression = statement.getExpression();
/* GRECLIPSE edit -- GROOVY-9907
ClassNode type = getType(expression);
if (typeCheckingContext.getEnclosingClosure() != null) {
Expand All @@ -2170,8 +2181,19 @@ protected ClassNode checkReturnType(final ReturnStatement statement) {
if ((expression instanceof VariableExpression) && hasInferredReturnType(expression)) {
type = expression.getNodeMetaData(INFERRED_RETURN_TYPE);
}
*/
ClassNode type;
if (expression instanceof VariableExpression && hasInferredReturnType(expression)) {
type = expression.getNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE);
} else {
type = getType(expression);
}
if (typeCheckingContext.getEnclosingClosure() != null) {
return type;
}
// GRECLIPSE end
MethodNode enclosingMethod = typeCheckingContext.getEnclosingMethod();
if (enclosingMethod != null && typeCheckingContext.getEnclosingClosure() == null) {
if (enclosingMethod != null) {
if (!enclosingMethod.isVoidMethod()
&& !type.equals(void_WRAPPER_TYPE)
&& !type.equals(VOID_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
@Override
public void returnStatementAdded(final ReturnStatement returnStatement) {
if (isNullConstant(returnStatement.getExpression())) return;
/* GRECLIPSE edit -- GROOVY-9907
checkReturnType(returnStatement);
if (typeCheckingContext.getEnclosingClosure() != null) {
addClosureReturnType(getType(returnStatement.getExpression()));
*/
ClassNode returnType = checkReturnType(returnStatement);
if (typeCheckingContext.getEnclosingClosure() != null) {
addClosureReturnType(returnType);
// GRECLIPSE end
} else if (typeCheckingContext.getEnclosingMethod() == null) {
throw new GroovyBugError("Unexpected return statement at " + returnStatement.getLineNumber() + ":" + returnStatement.getColumnNumber() + " " + returnStatement.getText());
}
Expand Down Expand Up @@ -630,7 +636,11 @@ public void visitVariableExpression(final VariableExpression vexp) {
}
}
}
/* GRECLIPSE edit -- GROOVY-9907
} else if (enclosingClosure == null) {
*/
} else {
// GRECLIPSE end
VariableExpression localVariable;
if (accessedVariable instanceof Parameter) {
Parameter parameter = (Parameter) accessedVariable;
Expand Down Expand Up @@ -2148,6 +2158,7 @@ private ClassNode infer(final ClassNode target, final ClassNode source) {

protected ClassNode checkReturnType(final ReturnStatement statement) {
Expression expression = statement.getExpression();
/* GRECLIPSE edit -- GROOVY-9907
ClassNode type = getType(expression);
if (typeCheckingContext.getEnclosingClosure() != null) {
Expand All @@ -2157,8 +2168,19 @@ protected ClassNode checkReturnType(final ReturnStatement statement) {
if ((expression instanceof VariableExpression) && hasInferredReturnType(expression)) {
type = expression.getNodeMetaData(INFERRED_RETURN_TYPE);
}
*/
ClassNode type;
if (expression instanceof VariableExpression && hasInferredReturnType(expression)) {
type = expression.getNodeMetaData(StaticTypesMarker.INFERRED_RETURN_TYPE);
} else {
type = getType(expression);
}
if (typeCheckingContext.getEnclosingClosure() != null) {
return type;
}
// GRECLIPSE end
MethodNode enclosingMethod = typeCheckingContext.getEnclosingMethod();
if (enclosingMethod != null && typeCheckingContext.getEnclosingClosure() == null) {
if (enclosingMethod != null) {
if (!enclosingMethod.isVoidMethod()
&& !type.equals(void_WRAPPER_TYPE)
&& !type.equals(VOID_TYPE)
Expand Down

0 comments on commit b9cfd0a

Please sign in to comment.