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
commit e3441dc12e93c31886376562a1165ef2aa4cbd3d Author: Eric Milles <[email protected]> AuthorDate: Wed Feb 9 13:21:07 2022 -0600 GROOVY-10477: SC: optimize `for (item in array) { }` -- dynamic variable --- .../asm/sc/StaticTypesStatementWriter.java | 2 +- src/test/groovy/transform/stc/LoopsSTCTest.groovy | 32 +++++++++++----------- .../classgen/asm/sc/LoopsStaticCompileTest.groovy | 10 +++++-- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java index 69c09c3..ee5829b 100644 --- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java +++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java @@ -96,7 +96,7 @@ public class StaticTypesStatementWriter extends StatementWriter { int mark = operandStack.getStackLength(); Parameter loopVariable = loop.getVariable(); - if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) { + if (collectionType.isArray() && loopVariable.getType().equals(collectionType.getComponentType())) { writeOptimizedForEachLoop(loop, loopVariable, collectionExpression, collectionType); } else if (GeneralUtils.isOrImplements(collectionType, ENUMERATION_CLASSNODE)) { writeEnumerationBasedForEachLoop(loop, collectionExpression, collectionType); diff --git a/src/test/groovy/transform/stc/LoopsSTCTest.groovy b/src/test/groovy/transform/stc/LoopsSTCTest.groovy index 1d306f0..e91ec80 100644 --- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy +++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy @@ -18,8 +18,6 @@ */ package groovy.transform.stc -import groovy.transform.CompileStatic - /** * Unit tests for static type checking : loops. */ @@ -37,23 +35,25 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase { } } - // GROOVY-8882 - void testStringCollectionLoop() { - for (s in 'abc') assert s instanceof String - for (String s in 'abc') assert s instanceof String - - for (char c in 'abc') assert c instanceof Character - for (Character c in 'abc') assert c instanceof Character + void testForInLoopOnArray() { + assertScript ''' + String[] strings = ['a','b','c'] + for (string in strings) { + string.toUpperCase() + } + ''' } // GROOVY-8882 - @CompileStatic - void testStringCollectionLoopCS() { - for (s in 'abc') assert s instanceof String - for (String s in 'abc') assert s instanceof String - - for (char c in 'abc') assert c instanceof Character - for (Character c in 'abc') assert c instanceof Character + void testForInLoopOnString() { + assertScript ''' + for (s in 'abc') assert s instanceof String + for (String s in 'abc') assert s instanceof String + ''' + assertScript ''' + for (char c in 'abc') assert c instanceof Character + for (Character c in 'abc') assert c instanceof Character + ''' } void testMethodCallWithEachAndDefAndTwoFooMethods() { 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 d9669f7..614b833 100644 --- a/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy +++ b/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy @@ -23,6 +23,12 @@ import groovy.transform.stc.LoopsSTCTest /** * Unit tests for static type checking : miscellaneous tests. */ -class LoopsStaticCompileTest extends LoopsSTCTest implements StaticCompilationTestSupport { -} +final class LoopsStaticCompileTest extends LoopsSTCTest implements StaticCompilationTestSupport { + // GROOVY-10477 + void testForInLoopOnArray() { + super.testForInLoopOnArray() + def bytecode = astTrees.values()[0][1] + assert !bytecode.contains('INVOKESTATIC org/codehaus/groovy/runtime/DefaultGroovyMethods.iterator') + } +}
