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/04/13 19:12:57 UTC

[groovy] 02/02: GROOVY-10579: SC: write array component type for optimized for-each loop

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

commit 2b18ef64eed2ebe08fc1be68911c06de68d7d74f
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Apr 13 12:05:20 2022 -0500

    GROOVY-10579: SC: write array component type for optimized for-each loop
---
 .../asm/sc/StaticTypesStatementWriter.java         | 11 ++---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  3 +-
 src/test/groovy/transform/stc/LoopsSTCTest.groovy  | 54 +++++++++++++---------
 .../classgen/asm/sc/LoopsStaticCompileTest.groovy  |  2 +-
 4 files changed, 41 insertions(+), 29 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 540f9b4ed8..50b636f56e 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
@@ -115,20 +115,19 @@ public class StaticTypesStatementWriter extends StatementWriter {
             OperandStack operandStack,
             MethodVisitor mv,
             ForStatement loop,
-            Expression collectionExpression,
-            ClassNode collectionType,
+            Expression arrayExpression,
+            ClassNode arrayType,
             Parameter loopVariable) {
-        BytecodeVariable variable = compileStack.defineVariable(loopVariable, false);
-
+        BytecodeVariable variable = compileStack.defineVariable(loopVariable, arrayType.getComponentType(), false);
         Label continueLabel = compileStack.getContinueLabel();
         Label breakLabel = compileStack.getBreakLabel();
 
         AsmClassGenerator acg = controller.getAcg();
 
         // load array on stack
-        collectionExpression.visit(acg);
+        arrayExpression.visit(acg);
         mv.visitInsn(DUP);
-        int array = compileStack.defineTemporaryVariable("$arr", collectionType, true);
+        int array = compileStack.defineTemporaryVariable("$arr", arrayType, true);
         mv.visitJumpInsn(IFNULL, breakLabel);
 
         // $len = array.length
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 37445a392f..9e9a3e7a00 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1953,6 +1953,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             // for (int i=0; i<...; i++) style loop
             super.visitForLoop(forLoop);
         } else {
+            visitStatement(forLoop);
             collectionExpression.visit(this);
             ClassNode collectionType;
             if (collectionExpression instanceof VariableExpression && hasInferredReturnType(collectionExpression)) {
@@ -1981,7 +1982,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             }
             typeCheckingContext.controlStructureVariables.put(forLoop.getVariable(), componentType);
             try {
-                super.visitForLoop(forLoop);
+                forLoop.getLoopBlock().visit(this);
             } finally {
                 typeCheckingContext.controlStructureVariables.remove(forLoop.getVariable());
             }
diff --git a/src/test/groovy/transform/stc/LoopsSTCTest.groovy b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
index b4dd7182a5..d4824ce9b2 100644
--- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
@@ -43,27 +43,6 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testForInLoopOnArray() {
-        assertScript '''
-            String[] strings = ['a','b','c']
-            for (string in strings) {
-                string.toUpperCase()
-            }
-        '''
-    }
-
-    // GROOVY-8882
-    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() {
         shouldFailWithMessages '''
             Date foo(Integer x) { new Date() }
@@ -196,6 +175,39 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    void testForInLoopOnArray() {
+        assertScript '''
+            String[] strings = ['a','b','c']
+            for (string in strings) {
+                string.toUpperCase()
+            }
+        '''
+    }
+
+    // GROOVY-10579
+    void testForInLoopOnArray2() {
+        assertScript '''
+            int[] numbers = [1,2,3,4,5]
+            int sum = 0
+            for (i in numbers) {
+                sum += i
+            }
+            assert sum == 15
+        '''
+    }
+
+    // GROOVY-8882
+    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
+        '''
+    }
+
     // 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 614b833f86..4d08b38d0c 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy
@@ -21,7 +21,7 @@ package org.codehaus.groovy.classgen.asm.sc
 import groovy.transform.stc.LoopsSTCTest
 
 /**
- * Unit tests for static type checking : miscellaneous tests.
+ * Unit tests for static compilation : loops.
  */
 final class LoopsStaticCompileTest extends LoopsSTCTest implements StaticCompilationTestSupport {