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 2022/07/31 12:51:10 UTC
[groovy] branch GROOVY_2_5_X updated: GROOVY-8487, GROOVY-10712: SC/STC: for-in loop over iterator
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
new ad2e09fa2d GROOVY-8487, GROOVY-10712: SC/STC: for-in loop over iterator
ad2e09fa2d is described below
commit ad2e09fa2dc1b65aa8f30d8d74e0b24095315b94
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jul 31 07:35:41 2022 -0500
GROOVY-8487, GROOVY-10712: SC/STC: for-in loop over iterator
---
.../groovy/runtime/DefaultGroovyMethods.java | 3 +-
.../transform/stc/StaticTypeCheckingVisitor.java | 39 ++++++++++++----------
src/test/groovy/transform/stc/LoopsSTCTest.groovy | 11 ++++++
.../classgen/asm/sc/LoopsStaticCompileTest.groovy | 4 +--
4 files changed, 36 insertions(+), 21 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index e2bca6c3b1..d4747483e8 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -17517,7 +17517,8 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @see org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation#asCollection(java.lang.Object)
* @since 1.0
*/
- public static Iterator iterator(Object o) {
+ public static Iterator iterator(final Object o) {
+ if (o instanceof Iterator) return (Iterator)o; //GROOVY-8487
return DefaultTypeTransformation.asCollection(o).iterator();
}
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 6173a262ef..cbf76ed566 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1999,34 +1999,37 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
/**
- * Given a loop collection type, returns the inferred type of the loop element. Used, for
- * example, to infer the element type of a (for e in list) loop.
+ * Returns the inferred loop element type given a loop collection type. Used,
+ * for example, to infer the element type of a {@code for (e in list)} loop.
*
* @param collectionType the type of the collection
* @return the inferred component type
+ * @see #inferComponentType
*/
public static ClassNode inferLoopElementType(final ClassNode collectionType) {
ClassNode componentType = collectionType.getComponentType();
if (componentType == null) {
if (implementsInterfaceOrIsSubclassOf(collectionType, ITERABLE_TYPE)) {
- ClassNode intf = GenericsUtils.parameterizeType(collectionType, ITERABLE_TYPE);
- GenericsType[] genericsTypes = intf.getGenericsTypes();
- componentType = genericsTypes[0].getType();
- } else if (implementsInterfaceOrIsSubclassOf(collectionType, MAP_TYPE)) {
- // GROOVY-6240
- ClassNode intf = GenericsUtils.parameterizeType(collectionType, MAP_TYPE);
- GenericsType[] genericsTypes = intf.getGenericsTypes();
+ ClassNode col = GenericsUtils.parameterizeType(collectionType, ITERABLE_TYPE);
+ componentType = col.getGenericsTypes()[0].getType();
+
+ } else if (implementsInterfaceOrIsSubclassOf(collectionType, MAP_TYPE)) { // GROOVY-6240
+ ClassNode col = GenericsUtils.parameterizeType(collectionType, MAP_TYPE);
componentType = MAP_ENTRY_TYPE.getPlainNodeReference();
- componentType.setGenericsTypes(genericsTypes);
- } else if (STRING_TYPE.equals(collectionType)) {
- componentType = ClassHelper.STRING_TYPE;
- } else if (ENUMERATION_TYPE.equals(collectionType)) {
- // GROOVY-6123
- ClassNode intf = GenericsUtils.parameterizeType(collectionType, ENUMERATION_TYPE);
- GenericsType[] genericsTypes = intf.getGenericsTypes();
- componentType = genericsTypes[0].getType();
+ componentType.setGenericsTypes(col.getGenericsTypes());
+
+ } else if (implementsInterfaceOrIsSubclassOf(collectionType, ENUMERATION_TYPE)) { // GROOVY-6123
+ ClassNode col = GenericsUtils.parameterizeType(collectionType, ENUMERATION_TYPE);
+ componentType = col.getGenericsTypes()[0].getType();
+
+ } else if (implementsInterfaceOrIsSubclassOf(collectionType, Iterator_TYPE)) { // GROOVY-10712
+ ClassNode col = GenericsUtils.parameterizeType(collectionType, Iterator_TYPE);
+ componentType = col.getGenericsTypes()[0].getType();
+
+ } else if (collectionType.equals(STRING_TYPE)) {
+ componentType = STRING_TYPE;
} else {
- componentType = ClassHelper.OBJECT_TYPE;
+ componentType = OBJECT_TYPE;
}
}
return componentType;
diff --git a/src/test/groovy/transform/stc/LoopsSTCTest.groovy b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
index dd159a4a2a..4dd97a7226 100644
--- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
@@ -225,6 +225,17 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-8487, GROOVY-10712
+ void testForInLoopOnIterator() {
+ assertScript '''
+ def list = []
+ for (item in ['a','b','c'].iterator()) {
+ list.add(item.toUpperCase())
+ }
+ assert list == ['A','B','C']
+ '''
+ }
+
// GROOVY-6123
void testForInLoopOnEnumeration() {
assertScript '''
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy
index 4d08b38d0c..bb9005e5fa 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy
@@ -26,8 +26,8 @@ import groovy.transform.stc.LoopsSTCTest
final class LoopsStaticCompileTest extends LoopsSTCTest implements StaticCompilationTestSupport {
// GROOVY-10477
- void testForInLoopOnArray() {
- super.testForInLoopOnArray()
+ void testForInLoopOnArray2() {
+ super.testForInLoopOnArray2()
def bytecode = astTrees.values()[0][1]
assert !bytecode.contains('INVOKESTATIC org/codehaus/groovy/runtime/DefaultGroovyMethods.iterator')
}