Skip to content

Commit

Permalink
GROOVY-10094, GROOVY-10104
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 10, 2021
1 parent c51fb47 commit 29ebe97
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6288,6 +6288,9 @@ public void testCompileStatic10072() {
" c = { p, q = 'baz' -> '' + p + q }\n" +
" assert c('foo', 'bar') == 'foobar'\n" +
" assert c('foo') == 'foobaz'\n" +
" c = { p, q = p.toString() -> '' + p + q }\n" +
" assert c('foo', 'bar') == 'foobar'\n" +
" assert c('foo') == 'foofoo'\n" +
"}\n" +
"test()\n",
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3000,6 +3000,26 @@ public void testTypeChecked10091() {
"----------\n");
}

@Test
public void testTypeChecked10094() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"void test(int i = 'error') {}\n" +
"test()\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 2)\n" +
"\tvoid test(int i = 'error') {}\n" +
"\t ^^^^^^^\n" +
"Groovy:[Static type checking] - Cannot assign value of type java.lang.String to variable of type int\n" +
"----------\n");
}

@Test
public void testTypeChecked10098() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.CodeVisitorSupport;
import org.codehaus.groovy.ast.ConstructorNode;
import org.codehaus.groovy.ast.DynamicVariable;
import org.codehaus.groovy.ast.FieldNode;
Expand Down Expand Up @@ -2611,6 +2612,21 @@ protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor)
typeCheckingContext.pushEnclosingMethod(node);
if (!isSkipMode(node) && !shouldSkipMethodNode(node)) {
super.visitConstructorOrMethod(node, isConstructor);
// GRECLIPSE add
if (node.hasDefaultValue())
for (Parameter parameter : node.getParameters()) {
if (!parameter.hasInitialExpression()) continue;
// GROOVY-10094: visit param default argument expression
visitInitialExpression(parameter.getInitialExpression(), varX(parameter), parameter);
// GROOVY-10104: remove direct target setting to prevent errors
parameter.getInitialExpression().visit(new CodeVisitorSupport() {
@Override
public void visitMethodCallExpression(final MethodCallExpression mce) {
mce.setMethodTarget(null); super.visitMethodCallExpression(mce);
}
});
}
// GRECLIPSE end
}
if (!isConstructor) {
returnAdder.visitMethod(node); // return statement added after visitConstructorOrMethod finished... we can not count these auto-generated return statements(GROOVY-7753), see `typeCheckingContext.pushEnclosingReturnStatement`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.CodeVisitorSupport;
import org.codehaus.groovy.ast.ConstructorNode;
import org.codehaus.groovy.ast.DynamicVariable;
import org.codehaus.groovy.ast.FieldNode;
Expand Down Expand Up @@ -2344,6 +2345,21 @@ protected void visitConstructorOrMethod(final MethodNode node, final boolean isC
typeCheckingContext.pushEnclosingMethod(node);
if (!isSkipMode(node) && !shouldSkipMethodNode(node)) {
super.visitConstructorOrMethod(node, isConstructor);
// GRECLIPSE add
if (node.hasDefaultValue())
for (Parameter parameter : node.getParameters()) {
if (!parameter.hasInitialExpression()) continue;
// GROOVY-10094: visit param default argument expression
visitInitialExpression(parameter.getInitialExpression(), varX(parameter), parameter);
// GROOVY-10104: remove direct target setting to prevent errors
parameter.getInitialExpression().visit(new CodeVisitorSupport() {
@Override
public void visitMethodCallExpression(final MethodCallExpression mce) {
mce.setMethodTarget(null); super.visitMethodCallExpression(mce);
}
});
}
// GRECLIPSE end
}
if (!isConstructor) {
returnAdder.visitMethod(node); // return statement added after visitConstructorOrMethod finished... we can not count these auto-generated return statements(GROOVY-7753), see `typeCheckingContext.pushEnclosingReturnStatement`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,21 @@ protected void visitConstructorOrMethod(final MethodNode node, final boolean isC
typeCheckingContext.pushEnclosingMethod(node);
if (!isSkipMode(node) && !shouldSkipMethodNode(node)) {
super.visitConstructorOrMethod(node, isConstructor);
// GRECLIPSE add
if (node.hasDefaultValue())
for (Parameter parameter : node.getParameters()) {
if (!parameter.hasInitialExpression()) continue;
// GROOVY-10094: visit param default argument expression
visitInitialExpression(parameter.getInitialExpression(), varX(parameter), parameter);
// GROOVY-10104: remove direct target setting to prevent errors
parameter.getInitialExpression().visit(new CodeVisitorSupport() {
@Override
public void visitMethodCallExpression(final MethodCallExpression mce) {
mce.setMethodTarget(null); super.visitMethodCallExpression(mce);
}
});
}
// GRECLIPSE end
}
if (!isConstructor) {
returnAdder.visitMethod(node); // return statement added after visitConstructorOrMethod finished... we can not count these auto-generated return statements(GROOVY-7753), see `typeCheckingContext.pushEnclosingReturnStatement`
Expand Down

0 comments on commit 29ebe97

Please sign in to comment.