This is an automated email from the ASF dual-hosted git repository. emilles pushed a commit to branch GROOVY_5_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_5_0_X by this push: new 76d306c74e GROOVY-11747: put back left type to choose from `POP`/`POP2` properly 76d306c74e is described below commit 76d306c74e04a70ddc941c449e968a3360b8d89c Author: Eric Milles <eric.mil...@thomsonreuters.com> AuthorDate: Tue Sep 2 08:34:05 2025 -0500 GROOVY-11747: put back left type to choose from `POP`/`POP2` properly --- .../groovy/classgen/asm/BinaryExpressionHelper.java | 2 ++ .../groovy/operator/TernaryOperatorsTest.groovy | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 5 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 274a6a9bcd..dfca7097a4 100644 --- a/src/main/java/org/codehaus/groovy/classgen/asm/BinaryExpressionHelper.java +++ b/src/main/java/org/codehaus/groovy/classgen/asm/BinaryExpressionHelper.java @@ -984,6 +984,7 @@ public class BinaryExpressionHelper { // load x, dup it and cast to boolean truePart.visit(controller.getAcg()); int top = operandStack.getStackLength(); + var type = operandStack.getTopOperand(); operandStack.dup(); operandStack.castToBool(top, true); Label l0 = operandStack.jump(IFEQ); @@ -995,6 +996,7 @@ public class BinaryExpressionHelper { // false path: drop x, load y and cast to T mv.visitLabel(l0); + operandStack.replace(type); // GROOVY-11747 operandStack.pop(); falsePart.visit(controller.getAcg()); operandStack.doGroovyCast(commonType); diff --git a/src/test/groovy/groovy/operator/TernaryOperatorsTest.groovy b/src/test/groovy/groovy/operator/TernaryOperatorsTest.groovy index edb90b580b..950f0727c2 100644 --- a/src/test/groovy/groovy/operator/TernaryOperatorsTest.groovy +++ b/src/test/groovy/groovy/operator/TernaryOperatorsTest.groovy @@ -18,7 +18,7 @@ */ package groovy.operator -import org.junit.Test +import org.junit.jupiter.api.Test import java.util.regex.Pattern @@ -42,9 +42,8 @@ final class TernaryOperatorsTest { assertCalledWithFoo(z < 100 ? "bar" : "foo") } - void assertCalledWithFoo(param) { - println "called with param ${param}" - assert param == "foo" + void assertCalledWithFoo(object) { + assert object == "foo" : "called with: ${object}" } @Test @@ -57,7 +56,7 @@ final class TernaryOperatorsTest { } @Test - void testElvisOperator() { + void testElvisOperator1() { def a = 1 def x = a?:2 assert x==a @@ -68,7 +67,10 @@ final class TernaryOperatorsTest { a = null x = a?:2 assert x==2 + } + @Test + void testElvisOperator2() { def list = ['a','b','c'] def index = 0 def ret = list[index++]?:"something else" @@ -79,6 +81,15 @@ final class TernaryOperatorsTest { assert ret2 == 'b' } + // GROOVY-11747 + @Test + void testElvisOperator3() { + long one = 1 + Long two = 2 + def wrap = one ?: two + assert wrap == 1L + } + @Test void testForType() { boolean b = false