Skip to content

Commit fe4aa20

Browse files
committed
GROOVY-11572: STC: spread on non-iterable type
3_0_X backport
1 parent f585923 commit fe4aa20

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ public static Object[] despreadList(Object[] args, Object[] spreads, int[] posit
811811
} else if (value.getClass().isArray()) {
812812
ret.addAll(DefaultTypeTransformation.primitiveArrayToList(value));
813813
} else {
814-
String error = "cannot spread the type " + value.getClass().getName() + " with value " + value;
814+
String error = "Cannot spread the type " + value.getClass().getName() + " with value " + value;
815815
if (value instanceof Map) {
816816
error += ", did you mean to use the spread-map operator instead?";
817817
}

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
@@ -836,6 +836,15 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
836836
'''
837837
}
838838

839+
// GROOVY-11572
840+
void testListExpressionWithSpreadOnNonIterable() {
841+
assertScript '''import static groovy.test.GroovyAssert.*
842+
shouldFail IllegalArgumentException, {
843+
def list = [0,*1]
844+
}
845+
'''
846+
}
847+
839848
// GROOVY-6241
840849
void testAsImmutable() {
841850
assertScript '''

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

+9
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,15 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
12821282
'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',
12831283
'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',
12841284
'Cannot find matching method '
1285+
1286+
shouldFailWithMessages '''
1287+
def m(String... strings) {
1288+
strings?.join('')
1289+
}
1290+
def str = m(*1) // GROOVY-11572
1291+
''',
1292+
'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',
1293+
'Cannot find matching method '
12851294
}
12861295

12871296
void testSpreadArgsForbiddenInStaticMethodCall() {

0 commit comments

Comments
 (0)