Skip to content

Commit da47adb

Browse files
eric-millesdaniellansun
authored andcommitted
GROOVY-9935: java.lang.Number can accommodate any number type
1 parent a315784 commit da47adb

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import static java.lang.Math.min;
7171
import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
7272
import static org.apache.groovy.ast.tools.ClassNodeUtils.samePackageName;
73+
import static org.apache.groovy.ast.tools.ExpressionUtils.isNullConstant;
7374
import static org.codehaus.groovy.ast.ClassHelper.BigDecimal_TYPE;
7475
import static org.codehaus.groovy.ast.ClassHelper.BigInteger_TYPE;
7576
import static org.codehaus.groovy.ast.ClassHelper.Boolean_TYPE;
@@ -84,6 +85,7 @@
8485
import static org.codehaus.groovy.ast.ClassHelper.GSTRING_TYPE;
8586
import static org.codehaus.groovy.ast.ClassHelper.Integer_TYPE;
8687
import static org.codehaus.groovy.ast.ClassHelper.Long_TYPE;
88+
import static org.codehaus.groovy.ast.ClassHelper.Number_TYPE;
8789
import static org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE;
8890
import static org.codehaus.groovy.ast.ClassHelper.STRING_TYPE;
8991
import static org.codehaus.groovy.ast.ClassHelper.Short_TYPE;
@@ -655,9 +657,9 @@ public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final
655657
return left == VOID_TYPE || left == void_WRAPPER_TYPE;
656658
}
657659

658-
if ((isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect))) {
659-
if (BigDecimal_TYPE == leftRedirect) {
660-
// any number can be assigned to a big decimal
660+
if (isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect)) {
661+
if (BigDecimal_TYPE == leftRedirect || Number_TYPE == leftRedirect) {
662+
// any number can be assigned to BigDecimal or Number
661663
return true;
662664
}
663665
if (BigInteger_TYPE == leftRedirect) {
@@ -666,7 +668,7 @@ public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final
666668
}
667669

668670
// if rightExpression is null and leftExpression is not a primitive type, it's ok
669-
boolean rightExpressionIsNull = (rightExpression instanceof ConstantExpression && ((ConstantExpression) rightExpression).isNullExpression());
671+
boolean rightExpressionIsNull = isNullConstant(rightExpression);
670672
if (rightExpressionIsNull && !isPrimitiveType(left)) {
671673
return true;
672674
}

src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
4141
'''
4242
}
4343

44+
// GROOVY-9935
45+
void testIntegerToNumber() {
46+
['def', 'int', 'Integer', 'BigInteger'].each { type ->
47+
assertScript """
48+
Number f() {
49+
$type n = 10
50+
return n
51+
}
52+
assert f() == 10
53+
"""
54+
}
55+
}
56+
4457
void testGStringMethods() {
4558
assertScript '''
4659
def myname = 'Cedric'

0 commit comments

Comments
 (0)