Skip to content

Commit 0873c66

Browse files
committed
GROOVY-5502 (pt.1)
1 parent f849ff3 commit 0873c66

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,38 @@ public void testTypeChecked5450() {
526526
"----------\n");
527527
}
528528

529+
@Test
530+
public void testTypeChecked5502() {
531+
for (String val : new String[] {"null", "new A()", "new B()", "new C()"/*TODO:, "new Object()"*/}) {
532+
//@formatter:off
533+
String[] sources = {
534+
"Main.groovy",
535+
"class A {\n" +
536+
" void m() {\n" +
537+
" }\n" +
538+
"}\n" +
539+
"class B extends A {\n" +
540+
"}\n" +
541+
"class C extends A {\n" +
542+
"}\n" +
543+
"@groovy.transform.TypeChecked\n" +
544+
"void test(boolean flag) {\n" +
545+
" def var = " + val + "\n" +
546+
" if (flag) {\n" +
547+
" var = new B()\n" +
548+
" } else {\n" +
549+
" var = new C()\n" +
550+
" }\n" +
551+
" var.m()\n" + // Cannot find matching method Object#m()
552+
"}\n" +
553+
"test(true)\n",
554+
};
555+
//@formatter:on
556+
557+
runConformTest(sources);
558+
}
559+
}
560+
529561
@Test
530562
public void testTypeChecked5523() {
531563
//@formatter:off

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+5
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,12 @@ && isAssignment(enclosingBinaryExpression.getOperation().getType())) {
977977
}
978978
}
979979

980+
/* GRECLIPSE edit -- GROOVY-5502
980981
boolean isEmptyDeclaration = expression instanceof DeclarationExpression && rightExpression instanceof EmptyExpression;
982+
*/
983+
boolean isEmptyDeclaration = (expression instanceof DeclarationExpression
984+
&& (rightExpression instanceof EmptyExpression || rType == UNKNOWN_PARAMETER_TYPE));
985+
// GRECLIPSE end
981986
if (!isEmptyDeclaration && isAssignment(op)) {
982987
if (rightExpression instanceof ConstructorCallExpression) {
983988
inferDiamondType((ConstructorCallExpression) rightExpression, lType);

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+5
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,12 @@ && isAssignment(enclosingBinaryExpression.getOperation().getType())) {
862862
}
863863
}
864864

865+
/* GRECLIPSE edit -- GROOVY-5502
865866
boolean isEmptyDeclaration = (expression instanceof DeclarationExpression && rightExpression instanceof EmptyExpression);
867+
*/
868+
boolean isEmptyDeclaration = (expression instanceof DeclarationExpression
869+
&& (rightExpression instanceof EmptyExpression || rType == UNKNOWN_PARAMETER_TYPE));
870+
// GRECLIPSE end
866871
if (isAssignment(op) && !isEmptyDeclaration) {
867872
if (rightExpression instanceof ConstructorCallExpression) {
868873
inferDiamondType((ConstructorCallExpression) rightExpression, lType);

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,8 @@ && isAssignment(enclosingBinaryExpression.getOperation().getType())) {
856856
}
857857
}
858858

859-
boolean isEmptyDeclaration = (expression instanceof DeclarationExpression && rightExpression instanceof EmptyExpression);
859+
boolean isEmptyDeclaration = (expression instanceof DeclarationExpression
860+
&& (rightExpression instanceof EmptyExpression || rType == UNKNOWN_PARAMETER_TYPE));
860861
if (!isEmptyDeclaration && isAssignment(op)) {
861862
if (rightExpression instanceof ConstructorCallExpression)
862863
inferDiamondType((ConstructorCallExpression) rightExpression, lType);

0 commit comments

Comments
 (0)