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 dce3315e87 GROOVY-11229: correct operand type of `instanceof` variable
dce3315e87 is described below

commit dce3315e87793738b409af916a0c11844fee12a4
Author: Eric Milles <[email protected]>
AuthorDate: Sat Mar 29 10:30:07 2025 -0500

    GROOVY-11229: correct operand type of `instanceof` variable
---
 .../groovy/classgen/asm/BinaryExpressionHelper.java        |  7 +++++--
 src/test/groovy/InstanceofTest.groovy                      | 14 +++++++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/classgen/asm/BinaryExpressionHelper.java 
b/src/main/java/org/codehaus/groovy/classgen/asm/BinaryExpressionHelper.java
index 357da0930e..341938b2fd 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/BinaryExpressionHelper.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/BinaryExpressionHelper.java
@@ -112,6 +112,8 @@ import static 
org.codehaus.groovy.syntax.Types.RIGHT_SHIFT_UNSIGNED;
 import static org.codehaus.groovy.syntax.Types.RIGHT_SHIFT_UNSIGNED_EQUAL;
 import static org.objectweb.asm.Opcodes.ACONST_NULL;
 import static org.objectweb.asm.Opcodes.ALOAD;
+import static org.objectweb.asm.Opcodes.ASTORE;
+import static org.objectweb.asm.Opcodes.CHECKCAST;
 import static org.objectweb.asm.Opcodes.DCONST_0;
 import static org.objectweb.asm.Opcodes.DUP;
 import static org.objectweb.asm.Opcodes.DUP_X1;
@@ -796,8 +798,9 @@ public class BinaryExpressionHelper {
             mv.visitInsn(DUP_X1); // stack: ..., check, value, check
             Label l0 = operandStack.jump(IFEQ); // skip store if not instanceof
 
-            BytecodeHelper.store(mv, targetType, v.getIndex());
-            Label l1 = operandStack.jump(GOTO); // skip pop
+            mv.visitTypeInsn(CHECKCAST, typeName);
+            mv.visitVarInsn(ASTORE, v.getIndex());
+            Label l1 = operandStack.jump(GOTO);
 
             mv.visitLabel(l0); // stack: ..., check, value
             mv.visitInsn(POP);
diff --git a/src/test/groovy/InstanceofTest.groovy 
b/src/test/groovy/InstanceofTest.groovy
index e095a230c6..f3c75f83af 100644
--- a/src/test/groovy/InstanceofTest.groovy
+++ b/src/test/groovy/InstanceofTest.groovy
@@ -80,7 +80,7 @@ final class InstanceofTest {
     // GROOVY-11229
     @Test
     void testVariable() {
-        Number n = 12345;
+        def n = (Number) 12345
         if (n instanceof Integer i) {
             assert i.intValue() == 12345
         } else {
@@ -94,6 +94,18 @@ final class InstanceofTest {
         assert (n instanceof Integer i && i.intValue() == 12345)
     }
 
+    // GROOVY-11229
+    @Test
+    void testVariable2() {
+        assert transformString(null) == null
+        assert transformString(1234) == 1234
+        assert transformString('xx') == 'XX'
+    }
+
+    def transformString(o) {
+        o instanceof String s ? s.toUpperCase() : o
+    }
+
     // GROOVY-11229
     @Test
     void testVariableScope() {

Reply via email to