Skip to content

Commit 009ed38

Browse files
committed
GROOVY-9972, GROOVY-9973
1 parent f3a1511 commit 009ed38

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -6026,4 +6026,25 @@ public void testCompileStatic9967() {
60266026

60276027
runConformTest(sources, "");
60286028
}
6029+
6030+
@Test
6031+
public void testCompileStatic9973() {
6032+
//@formatter:off
6033+
String[] sources = {
6034+
"Main.groovy",
6035+
"@groovy.transform.CompileStatic\n" +
6036+
"class C {\n" +
6037+
" private int f\n" +
6038+
" int getP() { f }\n" +
6039+
" Integer calc() { 123456 - p }\n" +
6040+
" Integer calc(int i) { i - p }\n" +
6041+
"}\n" +
6042+
"def c = new C()\n" +
6043+
"print c.calc()\n" +
6044+
"print c.calc(123)\n",
6045+
};
6046+
//@formatter:on
6047+
6048+
runConformTest(sources, "123456123");
6049+
}
60296050
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -1221,4 +1221,26 @@ public void testTypeChecked9972b() {
12211221

12221222
runConformTest(sources, "b#f");
12231223
}
1224+
1225+
@Test
1226+
public void testTypeChecked9972c() {
1227+
//@formatter:off
1228+
String[] sources = {
1229+
"Main.groovy",
1230+
"@groovy.transform.TupleConstructor\n" +
1231+
"class A {\n" +
1232+
" List<B> bees\n" +
1233+
"}\n" +
1234+
"class B {\n" +
1235+
"}\n" +
1236+
"@groovy.transform.TypeChecked\n" +
1237+
"void test(A... args) {\n" +
1238+
" List<B> bees = args.collectMany { it.bees ?: [] }\n" +
1239+
"}\n" +
1240+
"test(new A(), new A(bees: [new B()]))\n",
1241+
};
1242+
//@formatter:on
1243+
1244+
runConformTest(sources, "");
1245+
}
12241246
}

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -4444,9 +4444,11 @@ public void visitTernaryExpression(final TernaryExpression expression) {
44444444
*/
44454445
if (isNullConstant(trueExpression) && isNullConstant(falseExpression)) { // GROOVY-5523
44464446
resultType = checkForTargetType(expression, UNKNOWN_PARAMETER_TYPE);
4447-
} else if (isNullConstant(trueExpression)) {
4447+
} else if (isNullConstant(trueExpression) || (isEmptyCollection(trueExpression)
4448+
&& isOrImplements(typeOfTrue, typeOfFalse))) { // [] : List/Collection/Iterable
44484449
resultType = wrapTypeIfNecessary(checkForTargetType(falseExpression, typeOfFalse));
4449-
} else if (isNullConstant(falseExpression)) {
4450+
} else if (isNullConstant(falseExpression) || (isEmptyCollection(falseExpression)
4451+
&& isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
44504452
resultType = wrapTypeIfNecessary(checkForTargetType(trueExpression, typeOfTrue));
44514453
} else {
44524454
typeOfFalse = checkForTargetType(falseExpression, typeOfFalse);
@@ -4499,7 +4501,8 @@ && isTypeSource(expr, currentProperty.getInitialExpression())) {
44994501
if (expr instanceof ConstructorCallExpression) {
45004502
if (targetType == null) targetType = sourceType;
45014503
inferDiamondType((ConstructorCallExpression) expr, targetType);
4502-
} else if (targetType != null && missesGenericsTypes(sourceType)) {
4504+
} else if (targetType != null && !isPrimitiveType(getUnwrapper(targetType))
4505+
&& !targetType.equals(OBJECT_TYPE) && missesGenericsTypes(sourceType)) {
45034506
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
45044507
// the inferred type is the RHS type "completed" with generics information from LHS
45054508
return GenericsUtils.parameterizeType(targetType, sourceType.getPlainNodeReference());

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -4274,9 +4274,11 @@ public void visitTernaryExpression(final TernaryExpression expression) {
42744274
*/
42754275
if (isNullConstant(trueExpression) && isNullConstant(falseExpression)) { // GROOVY-5523
42764276
resultType = checkForTargetType(expression, UNKNOWN_PARAMETER_TYPE);
4277-
} else if (isNullConstant(trueExpression)) {
4277+
} else if (isNullConstant(trueExpression) || (isEmptyCollection(trueExpression)
4278+
&& isOrImplements(typeOfTrue, typeOfFalse))) { // [] : List/Collection/Iterable
42784279
resultType = wrapTypeIfNecessary(checkForTargetType(falseExpression, typeOfFalse));
4279-
} else if (isNullConstant(falseExpression)) {
4280+
} else if (isNullConstant(falseExpression) || (isEmptyCollection(falseExpression)
4281+
&& isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
42804282
resultType = wrapTypeIfNecessary(checkForTargetType(trueExpression, typeOfTrue));
42814283
} else {
42824284
typeOfFalse = checkForTargetType(falseExpression, typeOfFalse);
@@ -4329,7 +4331,8 @@ && isTypeSource(expr, currentProperty.getInitialExpression())) {
43294331
if (expr instanceof ConstructorCallExpression) {
43304332
if (targetType == null) targetType = sourceType;
43314333
inferDiamondType((ConstructorCallExpression) expr, targetType);
4332-
} else if (targetType != null && missesGenericsTypes(sourceType)) {
4334+
} else if (targetType != null && !isPrimitiveType(getUnwrapper(targetType))
4335+
&& !targetType.equals(OBJECT_TYPE) && missesGenericsTypes(sourceType)) {
43334336
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
43344337
// the inferred type is the RHS type "completed" with generics information from LHS
43354338
return GenericsUtils.parameterizeType(targetType, sourceType.getPlainNodeReference());

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -4240,9 +4240,11 @@ public void visitTernaryExpression(final TernaryExpression expression) {
42404240
*/
42414241
if (isNullConstant(trueExpression) && isNullConstant(falseExpression)) { // GROOVY-5523
42424242
resultType = checkForTargetType(expression, UNKNOWN_PARAMETER_TYPE);
4243-
} else if (isNullConstant(trueExpression)) {
4243+
} else if (isNullConstant(trueExpression) || (isEmptyCollection(trueExpression)
4244+
&& isOrImplements(typeOfTrue, typeOfFalse))) { // [] : List/Collection/Iterable
42444245
resultType = wrapTypeIfNecessary(checkForTargetType(falseExpression, typeOfFalse));
4245-
} else if (isNullConstant(falseExpression)) {
4246+
} else if (isNullConstant(falseExpression) || (isEmptyCollection(falseExpression)
4247+
&& isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
42464248
resultType = wrapTypeIfNecessary(checkForTargetType(trueExpression, typeOfTrue));
42474249
} else {
42484250
typeOfFalse = checkForTargetType(falseExpression, typeOfFalse);
@@ -4295,7 +4297,8 @@ && isTypeSource(expr, currentProperty.getInitialExpression())) {
42954297
if (expr instanceof ConstructorCallExpression) {
42964298
if (targetType == null) targetType = sourceType;
42974299
inferDiamondType((ConstructorCallExpression) expr, targetType);
4298-
} else if (targetType != null && missesGenericsTypes(sourceType)) {
4300+
} else if (targetType != null && !isPrimitiveType(getUnwrapper(targetType))
4301+
&& !targetType.equals(OBJECT_TYPE) && missesGenericsTypes(sourceType)) {
42994302
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
43004303
// the inferred type is the RHS type "completed" with generics information from LHS
43014304
return GenericsUtils.parameterizeType(targetType, sourceType.getPlainNodeReference());

0 commit comments

Comments
 (0)