Skip to content

Commit 70ce561

Browse files
avafanasievdaniellansun
authored andcommitted
GROOVY-8319: Improve smart type on list expressions (closes #602)
1 parent 10cba2a commit 70ce561

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,8 @@ private boolean typeCheckMultipleAssignmentAndContinue(Expression leftExpression
873873
if (!isAssignableTo(elemType, tupleType)) {
874874
addStaticTypeError("Cannot assign value of type " + elemType.toString(false) + " to variable of type " + tupleType.toString(false), rightExpression);
875875
return false; // avoids too many errors
876+
} else {
877+
storeType(tupleExpression, elemType);
876878
}
877879
}
878880

@@ -923,7 +925,7 @@ private void addPrecisionErrors(ClassNode leftRedirect, ClassNode lhsType, Class
923925
}
924926
} else if (rightExpression instanceof ListExpression) {
925927
for (Expression element : ((ListExpression) rightExpression).getExpressions()) {
926-
ClassNode rightComponentType = element.getType().redirect();
928+
ClassNode rightComponentType = this.getType(element);
927929
if (!checkCompatibleAssignmentTypes(leftComponentType, rightComponentType)
928930
&& !(isNullConstant(element) && !isPrimitiveType(leftComponentType))) {
929931
addStaticTypeError("Cannot assign value of type " + rightComponentType.toString(false) + " into array of type " + lhsType.toString(false), rightExpression);

src/test/groovy/transform/stc/STCAssignmentTest.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,5 +844,33 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
844844
assert fooParameterAssignment(null) == 42
845845
'''
846846
}
847+
848+
void testIntegerArraySmartType() {
849+
assertScript '''
850+
def m() {
851+
def a = 1
852+
Integer[] b = [a]
853+
}
854+
'''
855+
}
856+
857+
void testIntegerSecondDimArraySmartType() {
858+
assertScript '''
859+
def m() {
860+
def a = new int[5]
861+
int[][] b = [a]
862+
}
863+
'''
864+
}
865+
866+
void testMultiAssign() {
867+
assertScript '''
868+
def m() {
869+
def row = ["", "", ""]
870+
def (left, right) = [row[0], row[1]]
871+
left.toUpperCase()
872+
}
873+
'''
874+
}
847875
}
848876

0 commit comments

Comments
 (0)