This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new 67def1b53c GROOVY-11872: SC: propagate `STATIC_COMPILE_NODE` metadata
in `Verifier`
67def1b53c is described below
commit 67def1b53c62d190f7671c9857899efb2e8b92b5
Author: Eric Milles <[email protected]>
AuthorDate: Wed Mar 11 11:30:13 2026 -0500
GROOVY-11872: SC: propagate `STATIC_COMPILE_NODE` metadata in `Verifier`
4_0_X backport
---
.../org/codehaus/groovy/classgen/Verifier.java | 2 +
.../sc/CombinedIndyAndStaticCompilationTest.groovy | 137 +++++++++++++--------
2 files changed, 88 insertions(+), 51 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index 54e54b4bfe..b79b0f4bcc 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -122,6 +122,7 @@ import static
org.codehaus.groovy.ast.tools.GenericsUtils.addMethodGenerics;
import static
org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpec;
import static org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec;
import static
org.codehaus.groovy.ast.tools.PropertyNodeUtils.adjustPropertyModifiersForMethod;
+import static
org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys.STATIC_COMPILE_NODE;
/**
* Verifies the AST node and adds any default AST code before bytecode
generation occurs.
@@ -980,6 +981,7 @@ public class Verifier implements GroovyClassVisitor,
Opcodes {
addPropertyMethod(newMethod);
newMethod.putNodeMetaData(DEFAULT_PARAMETER_GENERATED,
Boolean.TRUE);
+ newMethod.putNodeMetaData(STATIC_COMPILE_NODE,
method.getNodeMetaData(STATIC_COMPILE_NODE));
});
}
diff --git
a/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/CombinedIndyAndStaticCompilationTest.groovy
b/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/CombinedIndyAndStaticCompilationTest.groovy
index 87a9233e38..6e9888292b 100644
---
a/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/CombinedIndyAndStaticCompilationTest.groovy
+++
b/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/CombinedIndyAndStaticCompilationTest.groovy
@@ -21,67 +21,102 @@ package org.codehaus.groovy.classgen.asm.sc
import org.codehaus.groovy.classgen.asm.AbstractBytecodeTestCase
import static org.codehaus.groovy.control.CompilerConfiguration.DEFAULT as
config
+import static org.junit.Assume.assumeTrue
/**
- * Tests for combined static compilation and indy code
+ * Tests for combined static compilation and indy code.
*/
-class CombinedIndyAndStaticCompilationTest extends AbstractBytecodeTestCase {
- void testArrayAccess() {
- if (!config.indyEnabled) return;
- ["byte", "short", "int", "long", "float", "double", "boolean",
"char"].each { type->
- //array get
- compile ("""
- @groovy.transform.CompileStatic
- def foo() {
- ${type}[] array = new ${type}[10]
- $type x = array[0]
- }
- """).hasSequence(["INVOKEDYNAMIC"])
- //array set
- compile ("""
- @groovy.transform.CompileStatic
- def foo() {
- ${type}[] array = new ${type}[10]
- array[0] = 1
- }
- """).hasSequence(["INVOKEDYNAMIC"])
+final class CombinedIndyAndStaticCompilationTest extends
AbstractBytecodeTestCase {
+
+ void testArrayRead() {
+ assumeTrue(config.indyEnabled)
+
+ for (String type :
['byte','short','int','long','float','double','boolean','char']) {
+ def bytecode = compile(method:'test', """
+ @groovy.transform.CompileStatic
+ void test() {
+ ${type}[] array = new ${type}[10]
+ ${type} x = array[0]
+ }
+ """)
+ int offset = bytecode.indexOf('--BEGIN--') + 4
+ assert bytecode.indexOf('INVOKEDYNAMIC', offset) > offset
+ assert bytecode.indexOf('INVOKEDYNAMIC', offset) <
bytecode.indexOf('--END--')
+ }
+ }
+
+ void testArrayWrite() {
+ assumeTrue(config.indyEnabled)
+
+ for (String type :
['byte','short','int','long','float','double','boolean','char']) {
+ def bytecode = compile(method:'test', """
+ @groovy.transform.CompileStatic
+ void test() {
+ ${type}[] array = new ${type}[10]
+ array[0] = 1
+ }
+ """)
+ int offset = bytecode.indexOf('--BEGIN--') + 4
+ assert bytecode.indexOf('INVOKEDYNAMIC', offset) > offset
+ assert bytecode.indexOf('INVOKEDYNAMIC', offset) <
bytecode.indexOf('--END--')
}
}
- void testNegativeAccess() {
- ["byte", "short", "int", "long", "float", "double", "char"].each {
type ->
+ void testNegativeIndex() {
+ for (String type :
['byte','short','int','long','float','double','char']) {
assertScript """
- @groovy.transform.CompileStatic
- def foo() {
- ${type}[] array = [0,1,2]
- assert array[0] == 0
- assert array[1] == 1
- assert array[2] == 2
- assert array[-1] == 2
- assert array[-2] == 1
- array[0] = 9
- assert array[0] == 9
- array[-1] = 8
- assert array[2] == 8
- }
- foo()
+ @groovy.transform.CompileStatic
+ void test() {
+ ${type}[] array = [0,1,2]
+ assert array[0] == 0
+ assert array[1] == 1
+ assert array[2] == 2
+ assert array[-1] == 2
+ assert array[-2] == 1
+ array[0] = 9
+ assert array[0] == 9
+ array[-1] = 8
+ assert array[2] == 8
+ }
+ test()
"""
}
- assertScript """
+ assertScript '''
+ @groovy.transform.CompileStatic
+ void test() {
+ boolean[] array = [false, false, true]
+ assert array[0] == false
+ assert array[1] == false
+ assert array[2] == true
+ assert array[-1] == true
+ assert array[-2] == false
+ array[0] = true
+ assert array[0] == true
+ array[-1] = false
+ assert array[2] == false
+ }
+ test()
+ '''
+ }
+
+ // GROOVY-11872
+ void testCompileStaticAndDefaultParameter() {
+ def bytecode = compile '''
+ class Foo {
@groovy.transform.CompileStatic
- def foo() {
- boolean[] array = [false, false, true]
- assert array[0] == false
- assert array[1] == false
- assert array[2] == true
- assert array[-1] == true
- assert array[-2] == false
- array[0] = true
- assert array[0] == true
- array[-1] = false
- assert array[2] == false
+ void bar(List list = baz()) {
+ for (item in list) {
+ println item
+ }
}
- foo()
- """
+ List baz() {
+ ['fizz','buzz']
+ }
+ }
+ '''
+ int offset = bytecode.indexOf('public bar()')
+ assert bytecode.indexOf('INVOKEDYNAMIC', offset) < 0
+ assert bytecode.indexOf('INVOKEVIRTUAL', offset) > offset
+ assert bytecode.indexOf('INVOKEVIRTUAL', offset) <
bytecode.indexOf('RETURN', offset)
}
}