You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/10/20 08:28:34 UTC

(groovy) branch master updated: GROOVY-12453: ArrayIndexOutOfBoundsException during compilation with an assert and map-constructor-call

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

paulk-asert 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 3096022662 GROOVY-12453: ArrayIndexOutOfBoundsException during compilation with an assert and map-constructor-call
3096022662 is described below

commit 309602266264fc46a407549d56d38fb0cf578480
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Oct 16 05:39:06 2026 +1000

    GROOVY-12453: ArrayIndexOutOfBoundsException during compilation with an assert and map-constructor-call
---
 .../groovy/classgen/AsmClassGenerator.java         | 14 +++++++++---
 .../asm/sc/StaticCompileConstructorsTest.groovy    | 26 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
index 7ec82f392e..ba6c0eb06c 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -1023,7 +1023,7 @@ public class AsmClassGenerator extends ClassGenerator {
     public void visitBinaryExpression(final BinaryExpression expression) {
         onLineNumber(expression, "visitBinaryExpression: " + expression.getOperation().getText());
         controller.getBinaryExpressionHelper().eval(expression);
-        controller.getAssertionWriter().record(expression.getOperation());
+        if (!isValueElided(expression)) controller.getAssertionWriter().record(expression.getOperation());
         doPostVisit(expression); // GROOVY-5746
     }
 
@@ -1248,7 +1248,7 @@ public class AsmClassGenerator extends ClassGenerator {
     public void visitMethodCallExpression(final MethodCallExpression call) {
         onLineNumber(call, "visitMethodCallExpression: \"" + call.getMethod() + "\":");
         controller.getInvocationWriter().writeInvokeMethod(call);
-        controller.getAssertionWriter().record(call.getMethod());
+        if (!isValueElided(call)) controller.getAssertionWriter().record(call.getMethod());
     }
 
     /**
@@ -1258,7 +1258,7 @@ public class AsmClassGenerator extends ClassGenerator {
     public void visitStaticMethodCallExpression(final StaticMethodCallExpression call) {
         onLineNumber(call, "visitStaticMethodCallExpression: \"" + call.getMethod() + "\":");
         controller.getInvocationWriter().writeInvokeStaticMethod(call);
-        controller.getAssertionWriter().record(call);
+        if (!isValueElided(call)) controller.getAssertionWriter().record(call);
         maybeInnerClassEntry(call.getOwnerType());
     }
 
@@ -2745,6 +2745,14 @@ public class AsmClassGenerator extends ClassGenerator {
         if (callback != null) callback.accept(controller);
     }
 
+    /**
+     * GROOVY-12453: an expression whose value was elided left nothing on the
+     * operand stack, so there is no value for a power assert to record.
+     */
+    private static boolean isValueElided(final Expression exp) {
+        return Boolean.TRUE.equals(exp.getNodeMetaData(ELIDE_EXPRESSION_VALUE));
+    }
+
     private static boolean isParameterReference(Expression exp) {
         return (exp instanceof VariableExpression && ((VariableExpression) exp).getAccessedVariable() instanceof Parameter);
     }
diff --git a/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/StaticCompileConstructorsTest.groovy b/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/StaticCompileConstructorsTest.groovy
index b937f9e7cc..279ededbf3 100644
--- a/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/StaticCompileConstructorsTest.groovy
+++ b/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/StaticCompileConstructorsTest.groovy
@@ -61,6 +61,32 @@ final class StaticCompileConstructorsTest extends ConstructorsSTCTest implements
         assert method.count('ACONST_NULL') == 0
     }
 
+    // GROOVY-12453
+    @Test
+    void testMapConstructorWithinPowerAssert() {
+        assertScript '''
+            class Person {
+                def name, age
+            }
+            def fred = 'fred', n = 25
+            assert new Person([name: fred, age: n])
+            assert new Person(name: fred, age: n).name == 'fred'
+            assert Objects.equals(fred, new Person(name: fred, age: n).name) // operand already on stack
+
+            def err = null
+            try {
+                assert new Person(name: fred, age: n).age == 26
+            } catch (AssertionError e) {
+                err = e
+            }
+            def lines = err.message.normalize().readLines()*.stripTrailing()
+            int i = lines.indexOf('assert new Person(name: fred, age: n).age == 26')
+            assert i >= 0
+            assert lines[i + 1] == '                        |          |  |'
+            assert lines[i + 2] == "                        'fred'     25 25"
+        '''
+    }
+
     @Test
     void testPrivateConstructorFromClosure() {
         assertScript '''