Skip to content

Commit 8b28d7d

Browse files
committed
GROOVY-11572: STC: spread on non-iterable type
4_0_X backport
1 parent e620885 commit 8b28d7d

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ public static Object[] despreadList(Object[] args, Object[] spreads, int[] posit
912912
} else if (value.getClass().isArray()) {
913913
ret.addAll(DefaultTypeTransformation.primitiveArrayToList(value));
914914
} else {
915-
String error = "cannot spread the type " + value.getClass().getName() + " with value " + value;
915+
String error = "Cannot spread the type " + value.getClass().getName() + " with value " + value;
916916
if (value instanceof Map) {
917917
error += ", did you mean to use the spread-map operator instead?";
918918
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -5307,7 +5307,10 @@ protected ClassNode getType(final ASTNode node) {
53075307
}
53085308
if (node instanceof SpreadExpression) {
53095309
type = getType(((SpreadExpression) node).getExpression());
5310-
return inferComponentType(type, null); // for list literal
5310+
type = inferComponentType(type, null); // for list literal
5311+
if (type == null) // GROOVY-11572: not an iterable
5312+
type = UNKNOWN_PARAMETER_TYPE;
5313+
return type;
53115314
}
53125315
if (node instanceof UnaryPlusExpression) {
53135316
return getType(((UnaryPlusExpression) node).getExpression());

src/test/groovy/operator/SpreadListOperatorTest.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ class SpreadListOperatorTest extends GroovyTestCase {
9494
def items = [1, 2, 3, 4]
9595
items[*new Date()]
9696
'''
97-
assert message.contains('cannot spread the type java.util.Date')
97+
assert message.contains('Cannot spread the type java.util.Date')
9898

9999
message = shouldFail IllegalArgumentException, '''
100100
def items = [1, 2, 3, 4]
101101
def map = [a: 1]
102102
items[*map]
103103
'''
104-
assert message.contains('cannot spread the type java.util.LinkedHashMap')
104+
assert message.contains('Cannot spread the type java.util.LinkedHashMap')
105105
assert message.contains('did you mean to use the spread-map operator instead?')
106106
}
107107

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

+9
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,15 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
860860
'''
861861
}
862862

863+
// GROOVY-11572
864+
void testListExpressionWithSpreadOnNonIterable() {
865+
assertScript '''import static groovy.test.GroovyAssert.*
866+
shouldFail IllegalArgumentException, {
867+
def list = [0,*1]
868+
}
869+
'''
870+
}
871+
863872
// GROOVY-6241
864873
void testAsImmutable() {
865874
assertScript '''

src/test/groovy/transform/stc/MethodCallsSTCTest.groovy

+8
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,14 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
12501250
'The spread operator cannot be used as argument of method or closure calls with static type checking because the number of arguments cannot be determined at compile time',
12511251
'The spread operator cannot be used as argument of method or closure calls with static type checking because the number of arguments cannot be determined at compile time',
12521252
'Cannot find matching method '
1253+
1254+
shouldFailWithMessages '''
1255+
def m(String... strings) {
1256+
strings?.join('')
1257+
}
1258+
def str = m(*1) // GROOVY-11572
1259+
''',
1260+
'The spread operator cannot be used as argument of method or closure calls with static type checking because the number of arguments cannot be determined at compile time'
12531261
}
12541262

12551263
void testSpreadArgsForbiddenInStaticMethodCall() {

0 commit comments

Comments
 (0)