Skip to content

Commit 67def1b

Browse files
committed
GROOVY-11872: SC: propagate STATIC_COMPILE_NODE metadata in Verifier
4_0_X backport
1 parent bbc4071 commit 67def1b

2 files changed

Lines changed: 88 additions & 51 deletions

File tree

src/main/java/org/codehaus/groovy/classgen/Verifier.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpec;
123123
import static org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec;
124124
import static org.codehaus.groovy.ast.tools.PropertyNodeUtils.adjustPropertyModifiersForMethod;
125+
import static org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys.STATIC_COMPILE_NODE;
125126

126127
/**
127128
* Verifies the AST node and adds any default AST code before bytecode generation occurs.
@@ -980,6 +981,7 @@ public void visitConstructorCallExpression(final ConstructorCallExpression call)
980981

981982
addPropertyMethod(newMethod);
982983
newMethod.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
984+
newMethod.putNodeMetaData(STATIC_COMPILE_NODE, method.getNodeMetaData(STATIC_COMPILE_NODE));
983985
});
984986
}
985987

src/test/groovy/org/codehaus/groovy/classgen/asm/sc/CombinedIndyAndStaticCompilationTest.groovy

Lines changed: 86 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,67 +21,102 @@ package org.codehaus.groovy.classgen.asm.sc
2121
import org.codehaus.groovy.classgen.asm.AbstractBytecodeTestCase
2222

2323
import static org.codehaus.groovy.control.CompilerConfiguration.DEFAULT as config
24+
import static org.junit.Assume.assumeTrue
2425

2526
/**
26-
* Tests for combined static compilation and indy code
27+
* Tests for combined static compilation and indy code.
2728
*/
28-
class CombinedIndyAndStaticCompilationTest extends AbstractBytecodeTestCase {
29-
void testArrayAccess() {
30-
if (!config.indyEnabled) return;
31-
["byte", "short", "int", "long", "float", "double", "boolean", "char"].each { type->
32-
//array get
33-
compile ("""
34-
@groovy.transform.CompileStatic
35-
def foo() {
36-
${type}[] array = new ${type}[10]
37-
$type x = array[0]
38-
}
39-
""").hasSequence(["INVOKEDYNAMIC"])
40-
//array set
41-
compile ("""
42-
@groovy.transform.CompileStatic
43-
def foo() {
44-
${type}[] array = new ${type}[10]
45-
array[0] = 1
46-
}
47-
""").hasSequence(["INVOKEDYNAMIC"])
29+
final class CombinedIndyAndStaticCompilationTest extends AbstractBytecodeTestCase {
30+
31+
void testArrayRead() {
32+
assumeTrue(config.indyEnabled)
33+
34+
for (String type : ['byte','short','int','long','float','double','boolean','char']) {
35+
def bytecode = compile(method:'test', """
36+
@groovy.transform.CompileStatic
37+
void test() {
38+
${type}[] array = new ${type}[10]
39+
${type} x = array[0]
40+
}
41+
""")
42+
int offset = bytecode.indexOf('--BEGIN--') + 4
43+
assert bytecode.indexOf('INVOKEDYNAMIC', offset) > offset
44+
assert bytecode.indexOf('INVOKEDYNAMIC', offset) < bytecode.indexOf('--END--')
45+
}
46+
}
47+
48+
void testArrayWrite() {
49+
assumeTrue(config.indyEnabled)
50+
51+
for (String type : ['byte','short','int','long','float','double','boolean','char']) {
52+
def bytecode = compile(method:'test', """
53+
@groovy.transform.CompileStatic
54+
void test() {
55+
${type}[] array = new ${type}[10]
56+
array[0] = 1
57+
}
58+
""")
59+
int offset = bytecode.indexOf('--BEGIN--') + 4
60+
assert bytecode.indexOf('INVOKEDYNAMIC', offset) > offset
61+
assert bytecode.indexOf('INVOKEDYNAMIC', offset) < bytecode.indexOf('--END--')
4862
}
4963
}
5064

51-
void testNegativeAccess() {
52-
["byte", "short", "int", "long", "float", "double", "char"].each { type ->
65+
void testNegativeIndex() {
66+
for (String type : ['byte','short','int','long','float','double','char']) {
5367
assertScript """
54-
@groovy.transform.CompileStatic
55-
def foo() {
56-
${type}[] array = [0,1,2]
57-
assert array[0] == 0
58-
assert array[1] == 1
59-
assert array[2] == 2
60-
assert array[-1] == 2
61-
assert array[-2] == 1
62-
array[0] = 9
63-
assert array[0] == 9
64-
array[-1] = 8
65-
assert array[2] == 8
66-
}
67-
foo()
68+
@groovy.transform.CompileStatic
69+
void test() {
70+
${type}[] array = [0,1,2]
71+
assert array[0] == 0
72+
assert array[1] == 1
73+
assert array[2] == 2
74+
assert array[-1] == 2
75+
assert array[-2] == 1
76+
array[0] = 9
77+
assert array[0] == 9
78+
array[-1] = 8
79+
assert array[2] == 8
80+
}
81+
test()
6882
"""
6983
}
70-
assertScript """
84+
assertScript '''
85+
@groovy.transform.CompileStatic
86+
void test() {
87+
boolean[] array = [false, false, true]
88+
assert array[0] == false
89+
assert array[1] == false
90+
assert array[2] == true
91+
assert array[-1] == true
92+
assert array[-2] == false
93+
array[0] = true
94+
assert array[0] == true
95+
array[-1] = false
96+
assert array[2] == false
97+
}
98+
test()
99+
'''
100+
}
101+
102+
// GROOVY-11872
103+
void testCompileStaticAndDefaultParameter() {
104+
def bytecode = compile '''
105+
class Foo {
71106
@groovy.transform.CompileStatic
72-
def foo() {
73-
boolean[] array = [false, false, true]
74-
assert array[0] == false
75-
assert array[1] == false
76-
assert array[2] == true
77-
assert array[-1] == true
78-
assert array[-2] == false
79-
array[0] = true
80-
assert array[0] == true
81-
array[-1] = false
82-
assert array[2] == false
107+
void bar(List list = baz()) {
108+
for (item in list) {
109+
println item
110+
}
83111
}
84-
foo()
85-
"""
112+
List baz() {
113+
['fizz','buzz']
114+
}
115+
}
116+
'''
117+
int offset = bytecode.indexOf('public bar()')
118+
assert bytecode.indexOf('INVOKEDYNAMIC', offset) < 0
119+
assert bytecode.indexOf('INVOKEVIRTUAL', offset) > offset
120+
assert bytecode.indexOf('INVOKEVIRTUAL', offset) < bytecode.indexOf('RETURN', offset)
86121
}
87122
}

0 commit comments

Comments
 (0)