Fix Kotlin default interface methods calls for inline classes

When mapping callable method signature for erased inline class methods,
use original function descriptor instead of super declaration
(otherwise it would map to a default interface method with mismatching
signature).

When generating delegates to Kotlin default interface methods, keep
track of the original Kotlin types for delegating method arguments and
interface method arguments.

'original' for value parameters of fake overrides points to the
overridden function value parameters instead of the value parameter of
the unsubstituted function. This causes inconsistent type mapping for
inline classes implementing generic interfaces with default methods.

 #KT-25295 Fixed Target versions 1.3.20
 #KT-26931 Fixed Target versions 1.3.20
This commit is contained in:
Dmitry Petrov
2018-09-20 12:40:01 +03:00
parent 8536ef5b43
commit 8d2b1950e6
14 changed files with 363 additions and 9 deletions
@@ -66,13 +66,16 @@ interface CallGenerator {
}
val value = codegen.gen(argumentExpression)
value.put(parameterType, valueParameterDescriptor.original.type, v)
value.put(parameterType, valueParameterDescriptor.unsubstitutedType, v)
if (isVarargInvoke) {
v.astore(OBJECT_TYPE)
}
}
private val ValueParameterDescriptor.unsubstitutedType
get() = containingDeclaration.original.valueParameters[index].type
override fun putCapturedValueOnStack(stackValue: StackValue, valueType: Type, paramIndex: Int) {
stackValue.put(stackValue.type, stackValue.kotlinType, codegen.v)
}
@@ -20,10 +20,12 @@ import org.jetbrains.kotlin.psi.synthetics.SyntheticClassOrObjectDescriptor;
import org.jetbrains.kotlin.psi.synthetics.SyntheticClassOrObjectDescriptorKt;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.InlineClassesUtilsKt;
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin;
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature;
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter;
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.org.objectweb.asm.Type;
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
import org.jetbrains.org.objectweb.asm.commons.Method;
@@ -274,15 +276,56 @@ public abstract class ClassBodyCodegen extends MemberCodegen<KtPureClassOrObject
InstructionAdapter iv = codegen.v;
Type[] argTypes = signature.getAsmMethod().getArgumentTypes();
Type[] originalArgTypes = traitMethod.getArgumentTypes();
assert originalArgTypes.length == argTypes.length + 1 :
"Invalid trait implementation signature: " + signature + " vs " + traitMethod + " for " + interfaceFun;
boolean isErasedInlineClass =
InlineClassesUtilsKt.isInlineClass(descriptor) && kind == OwnerKind.ERASED_INLINE_CLASS;
iv.load(0, OBJECT_TYPE);
for (int i = 0, reg = 1; i < argTypes.length; i++) {
StackValue.local(reg, argTypes[i]).put(originalArgTypes[i + 1], iv);
//noinspection AssignmentToForLoopParameter
reg += argTypes[i].getSize();
int argI = 0;
int reg = 0;
Type receiverType = typeMapper.mapType(descriptor);
KotlinType interfaceKotlinType = ((ClassDescriptor) inheritedFun.getContainingDeclaration()).getDefaultType();
StackValue.local(reg, receiverType, descriptor.getDefaultType()).put(OBJECT_TYPE, interfaceKotlinType, iv);
if (isErasedInlineClass) argI++;
reg += receiverType.getSize();
int originalArgI = 1;
List<ParameterDescriptor> argsDescriptors = getParameters(inheritedFun);
List<ParameterDescriptor> originalArgsDescriptors = getParameters(interfaceFun);
assert argsDescriptors.size() == originalArgsDescriptors.size() :
"Inconsistent value parameters between delegating fun " + inheritedFun +
"and interface fun " + interfaceFun;
Iterator<ParameterDescriptor> argsIterator = argsDescriptors.iterator();
Iterator<ParameterDescriptor> originalArgsIterator = originalArgsDescriptors.iterator();
for (; argI < argTypes.length; argI++, originalArgI++) {
Type argType = argTypes[argI];
KotlinType argKotlinType = argsIterator.next().getType();
Type originalArgType = originalArgTypes[originalArgI];
KotlinType originalArgKotlinType = originalArgsIterator.next().getType();
StackValue.local(reg, argType, argKotlinType)
.put(originalArgType, originalArgKotlinType, iv);
reg += argType.getSize();
}
assert originalArgI == originalArgTypes.length :
"Invalid trait implementation signature: " + signature + " vs " + traitMethod + " for " + interfaceFun;
}
private List<ParameterDescriptor> getParameters(FunctionDescriptor functionDescriptor) {
List<ParameterDescriptor> valueParameterDescriptors =
new ArrayList<>(functionDescriptor.getValueParameters().size() + 1);
ReceiverParameterDescriptor extensionReceiverParameter = functionDescriptor.getExtensionReceiverParameter();
if (extensionReceiverParameter != null) {
valueParameterDescriptors.add(extensionReceiverParameter);
}
valueParameterDescriptors.addAll(functionDescriptor.getValueParameters());
return valueParameterDescriptors;
}
}
);
@@ -49,6 +49,7 @@ class ErasedInlineClassBodyCodegen(
override fun generateSyntheticPartsAfterBody() {
super.generateSyntheticPartsAfterBody()
generateTraitMethods()
generateUnboxMethod()
generateFunctionsFromAny()
generateSpecializedEqualsStub()
@@ -890,7 +890,7 @@ public class KotlinTypeMapper {
: functionDescriptor.getOriginal();
signature = toInlinedErasedClass
? mapSignatureForInlineErasedClassSkipGeneric(functionToCall)
? mapSignatureForInlineErasedClassSkipGeneric(descriptor.getOriginal())
: mapSignatureSkipGeneric(functionToCall);
returnKotlinType = functionToCall.getReturnType();
@@ -0,0 +1,39 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
interface IFoo {
fun Long.foo() = bar()
fun bar(): String
}
inline class Z(val x: Int) : IFoo {
override fun bar(): String = "OK"
}
inline class L(val x: Long) : IFoo {
override fun bar(): String = "OK"
}
inline class S(val x: String) : IFoo {
override fun bar(): String = "OK"
}
fun Z.testZ() {
if (1L.foo() != "OK") throw AssertionError()
}
fun L.testL() {
if (1L.foo() != "OK") throw AssertionError()
}
fun S.testS() {
if (1L.foo() != "OK") throw AssertionError()
}
fun box(): String {
Z(42).testZ()
L(4L).testL()
S("").testS()
return "OK"
}
@@ -0,0 +1,27 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
interface IFoo {
fun foo() = bar()
fun bar(): String
}
inline class Z(val x: Int) : IFoo {
override fun bar(): String = "OK"
}
inline class L(val x: Long) : IFoo {
override fun bar(): String = "OK"
}
inline class S(val x: String) : IFoo {
override fun bar(): String = "OK"
}
fun box(): String {
if (Z(42).foo() != "OK") throw AssertionError()
if (L(4L).foo() != "OK") throw AssertionError()
if (S("").foo() != "OK") throw AssertionError()
return "OK"
}
@@ -0,0 +1,14 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
interface IFoo<T : IFoo<T>> {
fun foo(t: T): String = t.bar()
fun bar(): String
}
inline class Z(val x: Int) : IFoo<Z> {
override fun bar(): String = "OK"
}
fun box(): String =
Z(1).foo(Z(2))
@@ -0,0 +1,13 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
interface IFoo<T> {
fun foo(x: T): String
}
inline class Z(val x: Int) : IFoo<Z> {
override fun foo(x: Z) = "OK"
}
fun box(): String =
Z(1).foo(Z(2))
@@ -0,0 +1,24 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
interface IBase {
fun foo() = "BAD"
}
interface IFoo : IBase {
override fun foo() = "OK"
}
inline class Z(val x: Int) : IFoo
inline class L(val x: Long) : IFoo
inline class S(val x: String) : IFoo
fun box(): String {
if (Z(42).foo() != "OK") throw AssertionError()
if (L(4L).foo() != "OK") throw AssertionError()
if (S("").foo() != "OK") throw AssertionError()
return "OK"
}
@@ -12064,6 +12064,44 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/hiddenConstructor/secondaryConstructor.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InterfaceMethodCalls extends AbstractBlackBoxCodegenTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
public void testAllFilesPresentInInterfaceMethodCalls() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("defaultInterfaceExtensionFunCall.kt")
public void testDefaultInterfaceExtensionFunCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceExtensionFunCall.kt");
}
@TestMetadata("defaultInterfaceMethodCall.kt")
public void testDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceMethodCall.kt");
}
@TestMetadata("genericDefaultInterfaceMethodCall.kt")
public void testGenericDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericDefaultInterfaceMethodCall.kt");
}
@TestMetadata("genericInterfaceMethodCall.kt")
public void testGenericInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericInterfaceMethodCall.kt");
}
@TestMetadata("overriddenDefaultInterfaceMethodCall.kt")
public void testOverriddenDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/overriddenDefaultInterfaceMethodCall.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/box/innerNested")
@@ -12064,6 +12064,44 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inlineClasses/hiddenConstructor/secondaryConstructor.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InterfaceMethodCalls extends AbstractLightAnalysisModeTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
public void testAllFilesPresentInInterfaceMethodCalls() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("defaultInterfaceExtensionFunCall.kt")
public void testDefaultInterfaceExtensionFunCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceExtensionFunCall.kt");
}
@TestMetadata("defaultInterfaceMethodCall.kt")
public void testDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceMethodCall.kt");
}
@TestMetadata("genericDefaultInterfaceMethodCall.kt")
public void testGenericDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericDefaultInterfaceMethodCall.kt");
}
@TestMetadata("genericInterfaceMethodCall.kt")
public void testGenericInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericInterfaceMethodCall.kt");
}
@TestMetadata("overriddenDefaultInterfaceMethodCall.kt")
public void testOverriddenDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/overriddenDefaultInterfaceMethodCall.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/box/innerNested")
@@ -12069,6 +12069,44 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/hiddenConstructor/secondaryConstructor.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InterfaceMethodCalls extends AbstractIrBlackBoxCodegenTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath);
}
public void testAllFilesPresentInInterfaceMethodCalls() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("defaultInterfaceExtensionFunCall.kt")
public void testDefaultInterfaceExtensionFunCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceExtensionFunCall.kt");
}
@TestMetadata("defaultInterfaceMethodCall.kt")
public void testDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceMethodCall.kt");
}
@TestMetadata("genericDefaultInterfaceMethodCall.kt")
public void testGenericDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericDefaultInterfaceMethodCall.kt");
}
@TestMetadata("genericInterfaceMethodCall.kt")
public void testGenericInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericInterfaceMethodCall.kt");
}
@TestMetadata("overriddenDefaultInterfaceMethodCall.kt")
public void testOverriddenDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/overriddenDefaultInterfaceMethodCall.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/box/innerNested")
@@ -10589,6 +10589,44 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/hiddenConstructor/secondaryConstructor.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InterfaceMethodCalls extends AbstractIrJsCodegenBoxTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInInterfaceMethodCalls() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true);
}
@TestMetadata("defaultInterfaceExtensionFunCall.kt")
public void testDefaultInterfaceExtensionFunCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceExtensionFunCall.kt");
}
@TestMetadata("defaultInterfaceMethodCall.kt")
public void testDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceMethodCall.kt");
}
@TestMetadata("genericDefaultInterfaceMethodCall.kt")
public void testGenericDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericDefaultInterfaceMethodCall.kt");
}
@TestMetadata("genericInterfaceMethodCall.kt")
public void testGenericInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericInterfaceMethodCall.kt");
}
@TestMetadata("overriddenDefaultInterfaceMethodCall.kt")
public void testOverriddenDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/overriddenDefaultInterfaceMethodCall.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/box/innerNested")
@@ -11649,6 +11649,44 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/hiddenConstructor/secondaryConstructor.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InterfaceMethodCalls extends AbstractJsCodegenBoxTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath);
}
public void testAllFilesPresentInInterfaceMethodCalls() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@TestMetadata("defaultInterfaceExtensionFunCall.kt")
public void testDefaultInterfaceExtensionFunCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceExtensionFunCall.kt");
}
@TestMetadata("defaultInterfaceMethodCall.kt")
public void testDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/defaultInterfaceMethodCall.kt");
}
@TestMetadata("genericDefaultInterfaceMethodCall.kt")
public void testGenericDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericDefaultInterfaceMethodCall.kt");
}
@TestMetadata("genericInterfaceMethodCall.kt")
public void testGenericInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/genericInterfaceMethodCall.kt");
}
@TestMetadata("overriddenDefaultInterfaceMethodCall.kt")
public void testOverriddenDefaultInterfaceMethodCall() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/interfaceMethodCalls/overriddenDefaultInterfaceMethodCall.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/box/innerNested")