Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1467,8 +1467,12 @@ public void visitVariableExpression(final VariableExpression expression) {
}

protected void createInterfaceSyntheticStaticFields() {
if (referencedClasses.isEmpty()) return;
var icl = controller.getInterfaceClassLoadingClass();
// GROOVY-11982: also materialise the helper when there are call sites
// (e.g. dynamic code in default methods under indy=false), otherwise
// CallSiteWriter routes INVOKESTATIC at a class that was never emitted
boolean hasCallSites = !controller.getCallSiteWriter().getCallSites().isEmpty();
if (referencedClasses.isEmpty() && !hasCallSites) return;
addInnerClass(icl);
for (Map.Entry<String, ClassNode> entry : referencedClasses.entrySet()) {
// generate a field node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public void makeSiteEntry() {
if (controller.isNotClinit()) {
MethodVisitor mv = controller.getMethodVisitor();
mv.visitInsn(NOP); // GROOVY-9076: need this for debugger to support step into
mv.visitMethodInsn(INVOKESTATIC, controller.getInternalClassName(), GET_CALLSITE_METHOD, GET_CALLSITE_DESC, false);
// GROOVY-11982: for an interface, route to the helper inner class which
// owns $getCallSiteArray (interfaces themselves never have it, and an
// INVOKESTATIC Methodref against an interface owner throws ICCE)
mv.visitMethodInsn(INVOKESTATIC, controller.getClassName(), GET_CALLSITE_METHOD, GET_CALLSITE_DESC, false);
controller.getOperandStack().push(CALLSITE_ARRAY_TYPE);
callSiteArrayVarIndex = controller.getCompileStack().defineTemporaryVariable("$local$callSiteArray", CALLSITE_ARRAY_TYPE, true);
}
Expand Down
96 changes: 96 additions & 0 deletions src/test/groovy/bugs/Groovy11982.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package bugs

import org.codehaus.groovy.control.CompilerConfiguration
import org.junit.jupiter.api.Test

/**
* An interface default method whose body uses dynamic Groovy features (e.g. a
* {@code GString} or a dynamic method call) needs the call-site array. Under
* {@code indy=false} the bytecode prologue emitted at the top of the method
* is {@code INVOKESTATIC $getCallSiteArray()}; the owner of that call must be
* the synthetic helper class (not the interface itself), and the helper must
* actually be materialised. Otherwise the JVM throws
* {@code IncompatibleClassChangeError} at first invocation because a
* {@code Methodref} cannot resolve to an interface owner.
*/
final class Groovy11982 {

@Test
void testInterfaceDefaultMethodWithGStringNonIndy() {
CompilerConfiguration config = new CompilerConfiguration()
config.optimizationOptions.put('indy', false)
new GroovyShell(config).evaluate '''
interface IConfig {
String getName()
default String getDescription() {
"config[name=${getName()}]"
}
}
class ConfigImpl implements IConfig {
String getName() { 'impl' }
}
assert new ConfigImpl().description == 'config[name=impl]'
'''
}

@Test
void testInterfaceDefaultMethodCallingOtherDefaultNonIndy() {
CompilerConfiguration config = new CompilerConfiguration()
config.optimizationOptions.put('indy', false)
new GroovyShell(config).evaluate '''
interface IConfig {
String getName()
default String getDescription() {
"config[name=${getName()}]"
}
default String greet() {
"greeted as ${getDescription()}"
}
}
class ConfigImpl implements IConfig {
String getName() { 'impl' }
}
assert new ConfigImpl().greet() == 'greeted as config[name=impl]'
'''
}

@Test
void testStaticConsumerCallsDefaultMethodNonIndy() {
CompilerConfiguration config = new CompilerConfiguration()
config.optimizationOptions.put('indy', false)
new GroovyShell(config).evaluate '''
interface IConfig {
String getName()
default String getDescription() {
"config[name=${getName()}]"
}
}
class ConfigImpl implements IConfig {
String getName() { 'impl' }
}
@groovy.transform.CompileStatic
class StaticConsumer {
static String describe(IConfig c) { c.description }
}
assert StaticConsumer.describe(new ConfigImpl()) == 'config[name=impl]'
'''
}
}
Loading