Skip to content

Commit 49c989e

Browse files
committed
GROOVY-9935
1 parent 00da3db commit 49c989e

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -817,4 +817,23 @@ public void testTypeChecked9915() {
817817

818818
runConformTest(sources, "[]");
819819
}
820+
821+
@Test
822+
public void testTypeChecked9935() {
823+
for (String type : new String[] {"def", "int", "Integer", "BigInteger", "BigDecimal"}) {
824+
//@formatter:off
825+
String[] sources = {
826+
"Main.groovy",
827+
"@groovy.transform.TypeChecked\n" +
828+
"Number f() {\n" +
829+
" " + type + " n = 42\n" +
830+
" return n\n" +
831+
"}\n" +
832+
"print f()\n",
833+
};
834+
//@formatter:on
835+
836+
runConformTest(sources, "42");
837+
}
838+
}
820839
}

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import static org.codehaus.groovy.ast.ClassHelper.GSTRING_TYPE;
9292
import static org.codehaus.groovy.ast.ClassHelper.Integer_TYPE;
9393
import static org.codehaus.groovy.ast.ClassHelper.Long_TYPE;
94+
import static org.codehaus.groovy.ast.ClassHelper.Number_TYPE;
9495
import static org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE;
9596
import static org.codehaus.groovy.ast.ClassHelper.STRING_TYPE;
9697
import static org.codehaus.groovy.ast.ClassHelper.Short_TYPE;
@@ -690,16 +691,16 @@ public static boolean checkCompatibleAssignmentTypes(ClassNode left, ClassNode r
690691
return checkCompatibleAssignmentTypes(leftRedirect.getComponentType(), rightRedirect.getComponentType(), rightExpression, false);
691692
}
692693

693-
if (right == VOID_TYPE || right == void_WRAPPER_TYPE) {
694-
return left == VOID_TYPE || left == void_WRAPPER_TYPE;
694+
if (rightRedirect == VOID_TYPE || rightRedirect == void_WRAPPER_TYPE) {
695+
return leftRedirect == VOID_TYPE || leftRedirect == void_WRAPPER_TYPE;
695696
}
696697

697-
if ((isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect))) {
698-
if (BigDecimal_TYPE == leftRedirect) {
698+
if (isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect)) {
699+
if (leftRedirect.equals(BigDecimal_TYPE) || leftRedirect.equals(Number_TYPE)) { // GRECLIPSE add -- GROOVY-9935
699700
// any number can be assigned to a big decimal
700701
return true;
701702
}
702-
if (BigInteger_TYPE == leftRedirect) {
703+
if (leftRedirect.equals(BigInteger_TYPE)) {
703704
return WideningCategories.isBigIntCategory(getUnwrapper(rightRedirect)) ||
704705
rightRedirect.isDerivedFrom(BigInteger_TYPE);
705706
}
@@ -716,7 +717,7 @@ public static boolean checkCompatibleAssignmentTypes(ClassNode left, ClassNode r
716717
// anything can be assigned to an Object, String, Boolean
717718
// or Class typed variable
718719
if (isWildcardLeftHandSide(leftRedirect)
719-
&& !(boolean_TYPE.equals(left) && rightExpressionIsNull)) return true;
720+
&& !(left.equals(boolean_TYPE) && rightExpressionIsNull)) return true;
720721

721722
// char as left expression
722723
if (leftRedirect == char_TYPE && rightRedirect == STRING_TYPE) {
@@ -731,7 +732,7 @@ public static boolean checkCompatibleAssignmentTypes(ClassNode left, ClassNode r
731732

732733
// if left is Enum and right is String or GString we do valueOf
733734
if (leftRedirect.isDerivedFrom(Enum_Type) &&
734-
(rightRedirect == GSTRING_TYPE || rightRedirect == STRING_TYPE)) {
735+
(rightRedirect == STRING_TYPE || rightRedirect.equals(GSTRING_TYPE))) {
735736
return true;
736737
}
737738

@@ -759,7 +760,7 @@ public static boolean checkCompatibleAssignmentTypes(ClassNode left, ClassNode r
759760
return true;
760761
}
761762

762-
if (GROOVY_OBJECT_TYPE.equals(leftRedirect) && isBeingCompiled(right)) {
763+
if (leftRedirect.equals(GROOVY_OBJECT_TYPE) && isBeingCompiled(right)) {
763764
return true;
764765
}
765766

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import static org.codehaus.groovy.ast.ClassHelper.GSTRING_TYPE;
8585
import static org.codehaus.groovy.ast.ClassHelper.Integer_TYPE;
8686
import static org.codehaus.groovy.ast.ClassHelper.Long_TYPE;
87+
import static org.codehaus.groovy.ast.ClassHelper.Number_TYPE;
8788
import static org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE;
8889
import static org.codehaus.groovy.ast.ClassHelper.STRING_TYPE;
8990
import static org.codehaus.groovy.ast.ClassHelper.Short_TYPE;
@@ -657,30 +658,30 @@ public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final
657658
return checkCompatibleAssignmentTypes(leftRedirect.getComponentType(), rightRedirect.getComponentType(), rightExpression, false);
658659
}
659660

660-
if (right == VOID_TYPE || right == void_WRAPPER_TYPE) {
661-
return left == VOID_TYPE || left == void_WRAPPER_TYPE;
661+
if (rightRedirect == VOID_TYPE || rightRedirect == void_WRAPPER_TYPE) {
662+
return leftRedirect == VOID_TYPE || leftRedirect == void_WRAPPER_TYPE;
662663
}
663664

664-
if ((isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect))) {
665-
if (BigDecimal_TYPE == leftRedirect) {
665+
if (isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect)) {
666+
if (leftRedirect.equals(BigDecimal_TYPE) || leftRedirect.equals(Number_TYPE)) { // GRECLIPSE add -- GROOVY-9935
666667
// any number can be assigned to a big decimal
667668
return true;
668669
}
669-
if (BigInteger_TYPE == leftRedirect) {
670+
if (leftRedirect.equals(BigInteger_TYPE)) {
670671
return WideningCategories.isBigIntCategory(getUnwrapper(rightRedirect)) || rightRedirect.isDerivedFrom(BigInteger_TYPE);
671672
}
672673
}
673674

674675
// if rightExpression is null and leftExpression is not a primitive type, it's ok
675-
boolean rightExpressionIsNull = (rightExpression instanceof ConstantExpression && ((ConstantExpression) rightExpression).isNullExpression());
676+
boolean rightExpressionIsNull = org.apache.groovy.ast.tools.ExpressionUtils.isNullConstant(rightExpression);
676677
if (rightExpressionIsNull && !isPrimitiveType(left)) {
677678
return true;
678679
}
679680

680681
// on an assignment everything that can be done by a GroovyCast is allowed
681682

682683
// anything can be assigned to an Object, String, Boolean or Class typed variable
683-
if (isWildcardLeftHandSide(leftRedirect) && !(boolean_TYPE.equals(left) && rightExpressionIsNull)) return true;
684+
if (isWildcardLeftHandSide(leftRedirect) && !(left.equals(boolean_TYPE) && rightExpressionIsNull)) return true;
684685

685686
// char as left expression
686687
if (leftRedirect == char_TYPE && rightRedirect == STRING_TYPE) {
@@ -694,7 +695,7 @@ public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final
694695
}
695696

696697
// if left is Enum and right is String or GString we do valueOf
697-
if (leftRedirect.isDerivedFrom(Enum_Type) && (rightRedirect == GSTRING_TYPE || rightRedirect == STRING_TYPE)) {
698+
if (leftRedirect.isDerivedFrom(Enum_Type) && (rightRedirect == STRING_TYPE || rightRedirect.equals(GSTRING_TYPE))) {
698699
return true;
699700
}
700701

@@ -721,7 +722,7 @@ public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final
721722
return true;
722723
}
723724

724-
if (GROOVY_OBJECT_TYPE.equals(leftRedirect) && isBeingCompiled(right)) {
725+
if (leftRedirect.equals(GROOVY_OBJECT_TYPE) && isBeingCompiled(right)) {
725726
return true;
726727
}
727728

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import static org.codehaus.groovy.ast.ClassHelper.GSTRING_TYPE;
8585
import static org.codehaus.groovy.ast.ClassHelper.Integer_TYPE;
8686
import static org.codehaus.groovy.ast.ClassHelper.Long_TYPE;
87+
import static org.codehaus.groovy.ast.ClassHelper.Number_TYPE;
8788
import static org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE;
8889
import static org.codehaus.groovy.ast.ClassHelper.STRING_TYPE;
8990
import static org.codehaus.groovy.ast.ClassHelper.Short_TYPE;
@@ -656,30 +657,30 @@ public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final
656657
return checkCompatibleAssignmentTypes(leftRedirect.getComponentType(), rightRedirect.getComponentType(), rightExpression, false);
657658
}
658659

659-
if (right == VOID_TYPE || right == void_WRAPPER_TYPE) {
660-
return left == VOID_TYPE || left == void_WRAPPER_TYPE;
660+
if (rightRedirect == VOID_TYPE || rightRedirect == void_WRAPPER_TYPE) {
661+
return leftRedirect == VOID_TYPE || leftRedirect == void_WRAPPER_TYPE;
661662
}
662663

663-
if ((isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect))) {
664-
if (BigDecimal_TYPE == leftRedirect) {
664+
if (isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect)) {
665+
if (leftRedirect.equals(BigDecimal_TYPE) || leftRedirect.equals(Number_TYPE)) { // GRECLIPSE add -- GROOVY-9935
665666
// any number can be assigned to a big decimal
666667
return true;
667668
}
668-
if (BigInteger_TYPE == leftRedirect) {
669+
if (leftRedirect.equals(BigInteger_TYPE)) {
669670
return WideningCategories.isBigIntCategory(getUnwrapper(rightRedirect)) || rightRedirect.isDerivedFrom(BigInteger_TYPE);
670671
}
671672
}
672673

673674
// if rightExpression is null and leftExpression is not a primitive type, it's ok
674-
boolean rightExpressionIsNull = (rightExpression instanceof ConstantExpression && ((ConstantExpression) rightExpression).isNullExpression());
675+
boolean rightExpressionIsNull = org.apache.groovy.ast.tools.ExpressionUtils.isNullConstant(rightExpression);
675676
if (rightExpressionIsNull && !isPrimitiveType(left)) {
676677
return true;
677678
}
678679

679680
// on an assignment everything that can be done by a GroovyCast is allowed
680681

681682
// anything can be assigned to an Object, String, Boolean or Class typed variable
682-
if (isWildcardLeftHandSide(leftRedirect) && !(boolean_TYPE.equals(left) && rightExpressionIsNull)) return true;
683+
if (isWildcardLeftHandSide(leftRedirect) && !(left.equals(boolean_TYPE) && rightExpressionIsNull)) return true;
683684

684685
// char as left expression
685686
if (leftRedirect == char_TYPE && rightRedirect == STRING_TYPE) {
@@ -693,7 +694,7 @@ public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final
693694
}
694695

695696
// if left is Enum and right is String or GString we do valueOf
696-
if (leftRedirect.isDerivedFrom(Enum_Type) && (rightRedirect == GSTRING_TYPE || rightRedirect == STRING_TYPE)) {
697+
if (leftRedirect.isDerivedFrom(Enum_Type) && (rightRedirect == STRING_TYPE || rightRedirect.equals(GSTRING_TYPE))) {
697698
return true;
698699
}
699700

@@ -720,7 +721,7 @@ public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final
720721
return true;
721722
}
722723

723-
if (GROOVY_OBJECT_TYPE.equals(leftRedirect) && isBeingCompiled(right)) {
724+
if (leftRedirect.equals(GROOVY_OBJECT_TYPE) && isBeingCompiled(right)) {
724725
return true;
725726
}
726727

0 commit comments

Comments
 (0)