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 18:20:18 UTC
[groovy] branch GROOVY_4_0_X updated: 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_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 fab723c705 GROOVY-10579: SC: write array component type for optimized for-each loop
fab723c705 is described below
commit fab723c705685c3a6cffb4f3d533300adb1465d6
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 | 8 ++--
.../transform/stc/StaticTypeCheckingVisitor.java | 3 +-
src/test/groovy/transform/stc/LoopsSTCTest.groovy | 54 +++++++++++++---------
.../classgen/asm/sc/LoopsStaticCompileTest.groovy | 2 +-
4 files changed, 40 insertions(+), 27 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 ee5829bc54..283b4df40d 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
@@ -107,20 +107,20 @@ public class StaticTypesStatementWriter extends StatementWriter {
compileStack.pop();
}
- private void writeOptimizedForEachLoop(final ForStatement loop, final Parameter loopVariable, final Expression collectionExpression, final ClassNode collectionType) {
+ private void writeOptimizedForEachLoop(final ForStatement loop, final Parameter loopVariable, final Expression arrayExpression, final ClassNode arrayType) {
CompileStack compileStack = controller.getCompileStack();
OperandStack operandStack = controller.getOperandStack();
MethodVisitor mv = controller.getMethodVisitor();
AsmClassGenerator acg = controller.getAcg();
- BytecodeVariable variable = compileStack.defineVariable(loopVariable, false);
+ BytecodeVariable variable = compileStack.defineVariable(loopVariable, arrayType.getComponentType(), false);
Label continueLabel = compileStack.getContinueLabel();
Label breakLabel = compileStack.getBreakLabel();
// 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 e006ad1a65..900bfed931 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1958,6 +1958,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 = getType(collectionExpression);
ClassNode forLoopVariableType = forLoop.getVariableType();
@@ -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 e91ec80f11..e8542e1181 100644
--- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
@@ -35,27 +35,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() }
@@ -193,6 +172,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 {