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/12 20:12:49 UTC

[groovy] branch master updated: GROOVY-8643: SC: null-safe for-in loop

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

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


The following commit(s) were added to refs/heads/master by this push:
     new deb32b0f67 GROOVY-8643: SC: null-safe for-in loop
deb32b0f67 is described below

commit deb32b0f672550fe6f2d634ea45d4b390987f246
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Jul 12 14:57:36 2022 -0500

    GROOVY-8643: SC: null-safe for-in loop
---
 .../groovy/classgen/asm/StatementWriter.java       | 21 ++++++++--------
 .../asm/sc/StaticTypesStatementWriter.java         | 16 +++++++-----
 src/test/groovy/transform/stc/LoopsSTCTest.groovy  | 29 ++++++++++++++++++++--
 3 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
index 8075892112..7c83e3b183 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
@@ -123,28 +123,29 @@ public class StatementWriter {
         BytecodeVariable variable = compileStack.defineVariable(statement.getVariable(), false);
 
         // get the iterator and generate the loop control
-        int iteratorIndex = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
-        Label continueLabel = compileStack.getContinueLabel();
-        Label breakLabel = compileStack.getBreakLabel();
+        int iterator = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
+        Label breakLabel = compileStack.getBreakLabel(), continueLabel = compileStack.getContinueLabel();
+
+        mv.visitVarInsn(ALOAD, iterator);
+        mv.visitJumpInsn(IFNULL, breakLabel);
 
         mv.visitLabel(continueLabel);
-        mv.visitVarInsn(ALOAD, iteratorIndex);
+
+        mv.visitVarInsn(ALOAD, iterator);
         writeIteratorHasNext(mv);
-        // note: ifeq tests for ==0, a boolean is 0 if it is false
-        mv.visitJumpInsn(IFEQ, breakLabel);
+        mv.visitJumpInsn(IFEQ, breakLabel); // jump if zero (aka false)
 
-        mv.visitVarInsn(ALOAD, iteratorIndex);
+        mv.visitVarInsn(ALOAD, iterator);
         writeIteratorNext(mv);
         operandStack.push(ClassHelper.OBJECT_TYPE);
         operandStack.storeVar(variable);
 
         // generate the loop body
         statement.getLoopBlock().visit(controller.getAcg());
-
         mv.visitJumpInsn(GOTO, continueLabel);
-        mv.visitLabel(breakLabel);
 
-        compileStack.removeVar(iteratorIndex);
+        mv.visitLabel(breakLabel);
+        compileStack.removeVar(iterator);
     }
 
     protected void writeIteratorHasNext(final MethodVisitor mv) {
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 5b8dc2e056..0e7db374a1 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
@@ -196,22 +196,25 @@ public class StaticTypesStatementWriter extends StatementWriter {
 
         collectionExpression.visit(controller.getAcg());
 
-        int enumIdx = compileStack.defineTemporaryVariable("$enum", ENUMERATION_CLASSNODE, true);
+        int enumeration = compileStack.defineTemporaryVariable("$enum", ENUMERATION_CLASSNODE, true);
+
+        mv.visitVarInsn(ALOAD, enumeration);
+        mv.visitJumpInsn(IFNULL, breakLabel);
 
         mv.visitLabel(continueLabel);
-        mv.visitVarInsn(ALOAD, enumIdx);
+
+        mv.visitVarInsn(ALOAD, enumeration);
         ENUMERATION_HASMORE_METHOD.call(mv);
-        // note: ifeq tests for ==0, a boolean is 0 if it is false
-        mv.visitJumpInsn(IFEQ, breakLabel);
+        mv.visitJumpInsn(IFEQ, breakLabel); // jump if zero (aka false)
 
-        mv.visitVarInsn(ALOAD, enumIdx);
+        mv.visitVarInsn(ALOAD, enumeration);
         ENUMERATION_NEXT_METHOD.call(mv);
         operandStack.push(ClassHelper.OBJECT_TYPE);
         operandStack.storeVar(variable);
 
         loop.getLoopBlock().visit(controller.getAcg());
-
         mv.visitJumpInsn(GOTO, continueLabel);
+
         mv.visitLabel(breakLabel);
     }
 
@@ -227,6 +230,7 @@ public class StaticTypesStatementWriter extends StatementWriter {
             MethodCallExpression call = GeneralUtils.callX(collectionExpression, "iterator");
             call.setImplicitThis(false);
             call.setMethodTarget(iterator);
+            call.setSafe(true);//GROOVY-8643
             call.visit(controller.getAcg());
         } else {
             collectionExpression.visit(controller.getAcg());
diff --git a/src/test/groovy/transform/stc/LoopsSTCTest.groovy b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
index ea816ae039..792466212c 100644
--- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
@@ -172,7 +172,27 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-8643
+    void testForInLoopOnList() {
+        assertScript '''
+            List<String> list = null
+            for (item in list) {
+                item.toUpperCase()
+            }
+        '''
+    }
+
+    // GROOVY-8643
     void testForInLoopOnArray() {
+        assertScript '''
+            String[] strings = null
+            for (string in strings) {
+                string.toUpperCase()
+            }
+        '''
+    }
+
+    void testForInLoopOnArray2() {
         assertScript '''
             String[] strings = ['a','b','c']
             for (string in strings) {
@@ -182,7 +202,7 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-10579
-    void testForInLoopOnArray2() {
+    void testForInLoopOnArray3() {
         assertScript '''
             int[] numbers = [1,2,3,4,5]
             int sum = 0
@@ -210,7 +230,7 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         assertScript '''
             Vector<String> v = new Vector<>()
             v.add('ooo')
-            def en = v.elements()
+            Enumeration<String> en = v.elements()
             for (e in en) {
                 assert e.toUpperCase() == 'OOO'
             }
@@ -226,6 +246,11 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
                 assert e.toUpperCase() in ['OOO','GROOVY']
                 if (e=='ooo') continue
             }
+
+            en = null
+            for (e in en) {
+                e.toUpperCase()
+            }
         '''
     }