Skip to content

Commit 54fff88

Browse files
committed
GROOVY-11621: STC: fix for list[idx] = null and map['key'] = null
1 parent 2d197ce commit 54fff88

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,12 +882,13 @@ public Expression transform(final Expression expr) {
882882
&& enclosingBinaryExpression != null
883883
&& enclosingBinaryExpression.getLeftExpression() == expression
884884
&& isAssignment(enclosingBinaryExpression.getOperation().getType())) {
885-
// left hand side of a subscript assignment: map['foo'] = ...
885+
// left hand side of a subscript assignment: map['key'] = ...
886886
Expression enclosingExpressionRHS = enclosingBinaryExpression.getRightExpression();
887887
if (!(enclosingExpressionRHS instanceof ClosureExpression)) {
888888
enclosingExpressionRHS.visit(this);
889889
}
890-
ClassNode[] arguments = {rType, getType(enclosingExpressionRHS)};
890+
ClassNode[] arguments = {rType, isNullConstant(enclosingExpressionRHS) // GROOVY-11621
891+
? UNKNOWN_PARAMETER_TYPE : getType(enclosingExpressionRHS)};
891892
List<MethodNode> methods = findMethod(lType, "putAt", arguments);
892893
if (methods.isEmpty()) {
893894
addNoMatchingMethodError(lType, "putAt", arguments, enclosingBinaryExpression);

src/test/groovy/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,45 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
945945
'Cannot find matching method java.util.Collection#putAt(int, java.lang.Object)'
946946
}
947947

948+
// GROOVY-11621
949+
void testListPutAt() {
950+
assertScript '''
951+
def list = ['a', 'b', 'c']
952+
list[0] = 'aa'
953+
assert list[0] == 'aa'
954+
955+
list.set(2, null)
956+
assert list[2] == null
957+
958+
list.putAt(0, null)
959+
assert list[0] == null
960+
961+
list[1] = null
962+
assert list[1] == null
963+
'''
964+
}
965+
966+
// GROOVY-11621
967+
void testMapPutAt() {
968+
assertScript '''
969+
def map = [a: 'foo', b: 'bar', c: 'baz']
970+
map['a'] = 'aa'
971+
assert map['a'] == 'aa'
972+
973+
map.put('c', null)
974+
assert map['c'] == null
975+
976+
map.putAt('a', null)
977+
assert map['a'] == null
978+
979+
map.d = null
980+
assert map['d'] == null
981+
982+
map['b'] = null
983+
assert map['b'] == null
984+
'''
985+
}
986+
948987
// GROOVY-6266
949988
void testMapGenerics() {
950989
assertScript '''

0 commit comments

Comments
 (0)