Skip to content

Commit

Permalink
GROOVY-11572: STC: spread on non-iterable type
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Feb 22, 2025
1 parent f340e1a commit c7800e8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ public static Object[] despreadList(final Object[] args, final Object[] spreads,
} else if (value.getClass().isArray()) {
ret.addAll(DefaultTypeTransformation.primitiveArrayToList(value));
} else {
String error = "cannot spread the type " + value.getClass().getName() + " with value " + value;
String error = "Cannot spread the type " + value.getClass().getName() + " with value " + value;
if (value instanceof Map) {
error += ", did you mean to use the spread-map operator instead?";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5265,7 +5265,10 @@ protected ClassNode getType(final ASTNode node) {

if (node instanceof SpreadExpression) {
type = getType(((SpreadExpression) node).getExpression());
return inferComponentType(type, null); // for list literal
type = inferComponentType(type, null); // for list literal
if (type == null) // GROOVY-11572: not an iterable
type = UNKNOWN_PARAMETER_TYPE;
return type;
}

if (node instanceof UnaryPlusExpression) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/groovy/operator/SpreadListOperatorTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ class SpreadListOperatorTest extends GroovyTestCase {
def items = [1, 2, 3, 4]
items[*new Date()]
'''
assert message.contains('cannot spread the type java.util.Date')
assert message.contains('Cannot spread the type java.util.Date')

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,14 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
'''
}

// GROOVY-11572
void testListExpressionWithSpreadOnNonIterable() {
def err = shouldFail '''
def list = [0,*1]
'''
assert err =~ 'Cannot spread the type java.lang.Integer with value 1'
}

// GROOVY-6241
void testAsImmutable() {
assertScript '''
Expand Down
9 changes: 9 additions & 0 deletions src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,15 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
assert m(1, '2', *strings(), '5') == '12345'
'''

// GROOVY-11572
def err = shouldFail '''
def m(String... strings) {
strings?.join('')
}
def str = m(*1)
'''
assert err =~ 'Cannot spread the type java.lang.Integer with value 1'

shouldFailWithMessages '''
def foo(String one, String... zeroOrMore) {
}
Expand Down

0 comments on commit c7800e8

Please sign in to comment.