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/30 22:21:07 UTC
[groovy] branch GROOVY_3_0_X 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 GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new 50c52c71a1 GROOVY-8643: SC: null-safe for-in loop
50c52c71a1 is described below
commit 50c52c71a1c83135265d52b2e2798246581a8f03
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Jul 30 17:04:45 2022 -0500
GROOVY-8643: SC: null-safe for-in loop
---
.../groovy/classgen/asm/StatementWriter.java | 22 ++++++++++++----------
.../asm/sc/StaticTypesStatementWriter.java | 1 +
src/test/groovy/transform/stc/LoopsSTCTest.groovy | 10 ++++++++++
3 files changed, 23 insertions(+), 10 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 7714d0a109..6ed2199d91 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
@@ -60,6 +60,7 @@ import static org.objectweb.asm.Opcodes.ATHROW;
import static org.objectweb.asm.Opcodes.CHECKCAST;
import static org.objectweb.asm.Opcodes.GOTO;
import static org.objectweb.asm.Opcodes.IFEQ;
+import static org.objectweb.asm.Opcodes.IFNULL;
import static org.objectweb.asm.Opcodes.MONITORENTER;
import static org.objectweb.asm.Opcodes.MONITOREXIT;
import static org.objectweb.asm.Opcodes.NOP;
@@ -149,28 +150,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 fb1d11365e..d6e9f6d66a 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
@@ -226,6 +226,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 7291043f50..4dd97a7226 100644
--- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
@@ -172,6 +172,16 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-8643
+ void testForInLoopOnList() {
+ assertScript '''
+ List<String> list = null
+ for (item in list) {
+ item.toUpperCase()
+ }
+ '''
+ }
+
// GROOVY-8643
void testForInLoopOnArray() {
assertScript '''