You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2025/02/22 20:23:53 UTC

(groovy) branch GROOVY_4_0_X updated: GROOVY-11572: STC: spread on non-iterable type

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new 8b28d7d90b GROOVY-11572: STC: spread on non-iterable type
8b28d7d90b is described below

commit 8b28d7d90b61a01b456bb04926ffd8d6d5650a2c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Feb 22 12:20:41 2025 -0600

    GROOVY-11572: STC: spread on non-iterable type
    
    4_0_X backport
---
 .../java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java  | 2 +-
 .../codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java | 5 ++++-
 src/test/groovy/operator/SpreadListOperatorTest.groovy           | 4 ++--
 src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy | 9 +++++++++
 src/test/groovy/transform/stc/MethodCallsSTCTest.groovy          | 8 ++++++++
 5 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java b/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java
index 599f6d2a49..66412e3ce2 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java
@@ -912,7 +912,7 @@ public class ScriptBytecodeAdapter {
             } 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?";
                 }
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index a62f93ba3e..0b8ba4d0b5 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -5307,7 +5307,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         }
         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) {
             return getType(((UnaryPlusExpression) node).getExpression());
diff --git a/src/test/groovy/operator/SpreadListOperatorTest.groovy b/src/test/groovy/operator/SpreadListOperatorTest.groovy
index 7eff8e8019..8fa20dbe14 100644
--- a/src/test/groovy/operator/SpreadListOperatorTest.groovy
+++ b/src/test/groovy/operator/SpreadListOperatorTest.groovy
@@ -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?')
     }
 
diff --git a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
index 0864efda0e..cc18ee3bee 100644
--- a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
@@ -860,6 +860,15 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-11572
+    void testListExpressionWithSpreadOnNonIterable() {
+        assertScript '''import static groovy.test.GroovyAssert.*
+            shouldFail IllegalArgumentException, {
+                def list = [0,*1]
+            }
+        '''
+    }
+
     // GROOVY-6241
     void testAsImmutable() {
         assertScript '''
diff --git a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
index 93e61129d1..e786427694 100644
--- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -1250,6 +1250,14 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
         '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',
         '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',
         'Cannot find matching method '
+
+        shouldFailWithMessages '''
+            def m(String... strings) {
+                strings?.join('')
+            }
+            def str = m(*1) // GROOVY-11572
+        ''',
+        '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'
     }
 
     void testSpreadArgsForbiddenInStaticMethodCall() {