You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/02/10 19:52:07 UTC
[groovy] 01/01: GROOVY-9935: java.lang.Number can accommodate any
number type
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-9935
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 9688041036cab4565886756f7b521a75c6d079b5
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Feb 10 13:50:02 2021 -0600
GROOVY-9935: java.lang.Number can accommodate any number type
---
.../groovy/transform/stc/StaticTypeCheckingSupport.java | 10 ++++++----
src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy | 13 +++++++++++++
2 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 76944d5..07a73e2 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -70,6 +70,7 @@ import java.util.regex.Matcher;
import static java.lang.Math.min;
import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
import static org.apache.groovy.ast.tools.ClassNodeUtils.samePackageName;
+import static org.apache.groovy.ast.tools.ExpressionUtils.isNullConstant;
import static org.codehaus.groovy.ast.ClassHelper.BigDecimal_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.BigInteger_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.Boolean_TYPE;
@@ -84,6 +85,7 @@ import static org.codehaus.groovy.ast.ClassHelper.GROOVY_OBJECT_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.GSTRING_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.Integer_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.Long_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.Number_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.STRING_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.Short_TYPE;
@@ -655,9 +657,9 @@ public abstract class StaticTypeCheckingSupport {
return left == VOID_TYPE || left == void_WRAPPER_TYPE;
}
- if ((isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect))) {
- if (BigDecimal_TYPE == leftRedirect) {
- // any number can be assigned to a big decimal
+ if (isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect)) {
+ if (BigDecimal_TYPE == leftRedirect || Number_TYPE == leftRedirect) {
+ // any number can be assigned to BigDecimal or Number
return true;
}
if (BigInteger_TYPE == leftRedirect) {
@@ -666,7 +668,7 @@ public abstract class StaticTypeCheckingSupport {
}
// if rightExpression is null and leftExpression is not a primitive type, it's ok
- boolean rightExpressionIsNull = (rightExpression instanceof ConstantExpression && ((ConstantExpression) rightExpression).isNullExpression());
+ boolean rightExpressionIsNull = isNullConstant(rightExpression);
if (rightExpressionIsNull && !isPrimitiveType(left)) {
return true;
}
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index 18fb268..3190f6a 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -41,6 +41,19 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-9935
+ void testIntegerToNumber() {
+ ['def', 'int', 'Integer', 'BigInteger'].each { type ->
+ assertScript """
+ Number f() {
+ $type n = 10
+ return n
+ }
+ assert f() == 10
+ """
+ }
+ }
+
void testGStringMethods() {
assertScript '''
def myname = 'Cedric'