Skip to content

Commit

Permalink
GROOVY-9972, GROOVY-9973
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 9, 2021
1 parent f3a1511 commit 009ed38
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6026,4 +6026,25 @@ public void testCompileStatic9967() {

runConformTest(sources, "");
}

@Test
public void testCompileStatic9973() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"class C {\n" +
" private int f\n" +
" int getP() { f }\n" +
" Integer calc() { 123456 - p }\n" +
" Integer calc(int i) { i - p }\n" +
"}\n" +
"def c = new C()\n" +
"print c.calc()\n" +
"print c.calc(123)\n",
};
//@formatter:on

runConformTest(sources, "123456123");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1221,4 +1221,26 @@ public void testTypeChecked9972b() {

runConformTest(sources, "b#f");
}

@Test
public void testTypeChecked9972c() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TupleConstructor\n" +
"class A {\n" +
" List<B> bees\n" +
"}\n" +
"class B {\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test(A... args) {\n" +
" List<B> bees = args.collectMany { it.bees ?: [] }\n" +
"}\n" +
"test(new A(), new A(bees: [new B()]))\n",
};
//@formatter:on

runConformTest(sources, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4444,9 +4444,11 @@ public void visitTernaryExpression(final TernaryExpression expression) {
*/
if (isNullConstant(trueExpression) && isNullConstant(falseExpression)) { // GROOVY-5523
resultType = checkForTargetType(expression, UNKNOWN_PARAMETER_TYPE);
} else if (isNullConstant(trueExpression)) {
} else if (isNullConstant(trueExpression) || (isEmptyCollection(trueExpression)
&& isOrImplements(typeOfTrue, typeOfFalse))) { // [] : List/Collection/Iterable
resultType = wrapTypeIfNecessary(checkForTargetType(falseExpression, typeOfFalse));
} else if (isNullConstant(falseExpression)) {
} else if (isNullConstant(falseExpression) || (isEmptyCollection(falseExpression)
&& isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
resultType = wrapTypeIfNecessary(checkForTargetType(trueExpression, typeOfTrue));
} else {
typeOfFalse = checkForTargetType(falseExpression, typeOfFalse);
Expand Down Expand Up @@ -4499,7 +4501,8 @@ && isTypeSource(expr, currentProperty.getInitialExpression())) {
if (expr instanceof ConstructorCallExpression) {
if (targetType == null) targetType = sourceType;
inferDiamondType((ConstructorCallExpression) expr, targetType);
} else if (targetType != null && missesGenericsTypes(sourceType)) {
} else if (targetType != null && !isPrimitiveType(getUnwrapper(targetType))
&& !targetType.equals(OBJECT_TYPE) && missesGenericsTypes(sourceType)) {
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
// the inferred type is the RHS type "completed" with generics information from LHS
return GenericsUtils.parameterizeType(targetType, sourceType.getPlainNodeReference());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4274,9 +4274,11 @@ public void visitTernaryExpression(final TernaryExpression expression) {
*/
if (isNullConstant(trueExpression) && isNullConstant(falseExpression)) { // GROOVY-5523
resultType = checkForTargetType(expression, UNKNOWN_PARAMETER_TYPE);
} else if (isNullConstant(trueExpression)) {
} else if (isNullConstant(trueExpression) || (isEmptyCollection(trueExpression)
&& isOrImplements(typeOfTrue, typeOfFalse))) { // [] : List/Collection/Iterable
resultType = wrapTypeIfNecessary(checkForTargetType(falseExpression, typeOfFalse));
} else if (isNullConstant(falseExpression)) {
} else if (isNullConstant(falseExpression) || (isEmptyCollection(falseExpression)
&& isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
resultType = wrapTypeIfNecessary(checkForTargetType(trueExpression, typeOfTrue));
} else {
typeOfFalse = checkForTargetType(falseExpression, typeOfFalse);
Expand Down Expand Up @@ -4329,7 +4331,8 @@ && isTypeSource(expr, currentProperty.getInitialExpression())) {
if (expr instanceof ConstructorCallExpression) {
if (targetType == null) targetType = sourceType;
inferDiamondType((ConstructorCallExpression) expr, targetType);
} else if (targetType != null && missesGenericsTypes(sourceType)) {
} else if (targetType != null && !isPrimitiveType(getUnwrapper(targetType))
&& !targetType.equals(OBJECT_TYPE) && missesGenericsTypes(sourceType)) {
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
// the inferred type is the RHS type "completed" with generics information from LHS
return GenericsUtils.parameterizeType(targetType, sourceType.getPlainNodeReference());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4240,9 +4240,11 @@ public void visitTernaryExpression(final TernaryExpression expression) {
*/
if (isNullConstant(trueExpression) && isNullConstant(falseExpression)) { // GROOVY-5523
resultType = checkForTargetType(expression, UNKNOWN_PARAMETER_TYPE);
} else if (isNullConstant(trueExpression)) {
} else if (isNullConstant(trueExpression) || (isEmptyCollection(trueExpression)
&& isOrImplements(typeOfTrue, typeOfFalse))) { // [] : List/Collection/Iterable
resultType = wrapTypeIfNecessary(checkForTargetType(falseExpression, typeOfFalse));
} else if (isNullConstant(falseExpression)) {
} else if (isNullConstant(falseExpression) || (isEmptyCollection(falseExpression)
&& isOrImplements(typeOfFalse, typeOfTrue))) { // List/Collection/Iterable : []
resultType = wrapTypeIfNecessary(checkForTargetType(trueExpression, typeOfTrue));
} else {
typeOfFalse = checkForTargetType(falseExpression, typeOfFalse);
Expand Down Expand Up @@ -4295,7 +4297,8 @@ && isTypeSource(expr, currentProperty.getInitialExpression())) {
if (expr instanceof ConstructorCallExpression) {
if (targetType == null) targetType = sourceType;
inferDiamondType((ConstructorCallExpression) expr, targetType);
} else if (targetType != null && missesGenericsTypes(sourceType)) {
} else if (targetType != null && !isPrimitiveType(getUnwrapper(targetType))
&& !targetType.equals(OBJECT_TYPE) && missesGenericsTypes(sourceType)) {
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
// the inferred type is the RHS type "completed" with generics information from LHS
return GenericsUtils.parameterizeType(targetType, sourceType.getPlainNodeReference());
Expand Down

0 comments on commit 009ed38

Please sign in to comment.