Properly reorder arguments in anonymous object super class constructor
#KT-18356 Fixed
This commit is contained in:
@@ -1072,7 +1072,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
List<ResolvedValueArgument> valueArguments = new ArrayList<>(params);
|
||||
List<ValueParameterDescriptor> valueParameters = new ArrayList<>(params);
|
||||
List<Type> mappedTypes = new ArrayList<>(params);
|
||||
for (ValueParameterDescriptor parameter : superValueParameters) {
|
||||
for (ValueParameterDescriptor parameter : superCall.getValueArguments().keySet()) {
|
||||
ResolvedValueArgument argument = superCall.getValueArguments().get(parameter);
|
||||
if (!(argument instanceof DefaultValueArgument)) {
|
||||
valueArguments.add(argument);
|
||||
|
||||
+30
-12
@@ -16,24 +16,40 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
|
||||
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil.pushDefaultValueOnStack
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.resolve.calls.model.*
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature
|
||||
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
|
||||
|
||||
internal class ObjectSuperCallArgumentGenerator(
|
||||
private val parameters: List<JvmMethodParameterSignature>,
|
||||
private val iv: InstructionAdapter,
|
||||
private var offset: Int,
|
||||
offset: Int,
|
||||
superConstructorCall: ResolvedCall<ConstructorDescriptor>
|
||||
) : ArgumentGenerator() {
|
||||
|
||||
private val offsets = IntArray(parameters.size) { -1 }
|
||||
|
||||
init {
|
||||
var currentOffset = offset
|
||||
superConstructorCall.valueArguments.forEach {
|
||||
(descriptor, argument) ->
|
||||
if (argument !is DefaultValueArgument) {
|
||||
val index = descriptor.index
|
||||
offsets[index] = currentOffset
|
||||
currentOffset += parameters[index].asmType.size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun generate(
|
||||
valueArgumentsByIndex: List<ResolvedValueArgument>,
|
||||
actualArgs: List<ResolvedValueArgument>,
|
||||
calleeDescriptor: CallableDescriptor?
|
||||
): DefaultCallArgs = super.generate(valueArgumentsByIndex, valueArgumentsByIndex, calleeDescriptor)
|
||||
|
||||
public override fun generateExpression(i: Int, argument: ExpressionValueArgument) {
|
||||
generateSuperCallArgument(i)
|
||||
}
|
||||
@@ -49,8 +65,10 @@ internal class ObjectSuperCallArgumentGenerator(
|
||||
|
||||
private fun generateSuperCallArgument(i: Int) {
|
||||
val type = parameters[i].asmType
|
||||
iv.load(offset, type)
|
||||
offset += type.size
|
||||
if (offsets[i] == -1) {
|
||||
throw AssertionError("Unknown parameter value at index $i with type $type")
|
||||
}
|
||||
iv.load(offsets[i], type)
|
||||
}
|
||||
|
||||
override fun reorderArgumentsIfNeeded(args: List<ArgumentAndDeclIndex>) {
|
||||
|
||||
@@ -77,6 +77,7 @@ import org.jetbrains.org.objectweb.asm.commons.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jetbrains.kotlin.codegen.AsmUtil.isStaticMethod;
|
||||
import static org.jetbrains.kotlin.codegen.JvmCodegenUtil.*;
|
||||
@@ -1420,8 +1421,7 @@ public class KotlinTypeMapper {
|
||||
boolean hasOuter
|
||||
) {
|
||||
ConstructorDescriptor superDescriptor = SamCodegenUtil.resolveSamAdapter(superCall.getResultingDescriptor());
|
||||
List<ResolvedValueArgument> valueArguments = superCall.getValueArgumentsByIndex();
|
||||
assert valueArguments != null : "Failed to arrange value arguments by index: " + superDescriptor;
|
||||
Map<ValueParameterDescriptor, ResolvedValueArgument> valueArguments = superCall.getValueArguments();
|
||||
|
||||
List<JvmMethodParameterSignature> parameters = mapSignatureSkipGeneric(superDescriptor.getOriginal()).getValueParameters();
|
||||
|
||||
@@ -1444,10 +1444,11 @@ public class KotlinTypeMapper {
|
||||
|
||||
if (isAnonymousObject(descriptor.getContainingDeclaration())) {
|
||||
// For anonymous objects, also add all real non-default value arguments passed to the super constructor
|
||||
for (int i = 0; i < args; i++) {
|
||||
ResolvedValueArgument valueArgument = valueArguments.get(i);
|
||||
JvmMethodParameterSignature parameter = parameters.get(params - args + i);
|
||||
for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> argumentAndValue : valueArguments.entrySet()) {
|
||||
|
||||
ResolvedValueArgument valueArgument = argumentAndValue.getValue();
|
||||
if (!(valueArgument instanceof DefaultValueArgument)) {
|
||||
JvmMethodParameterSignature parameter = parameters.get(params - args + argumentAndValue.getKey().getIndex());
|
||||
writeParameter(sw, JvmMethodParameterKind.SUPER_CALL_PARAM, parameter.getAsmType());
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
var result = "fail"
|
||||
|
||||
open class Base(val o: String, val k: String)
|
||||
|
||||
fun box(): String {
|
||||
val obj1 = object : Base(k = { result = "O"; "K"}() , o = {result += "K"; "O"}()) {}
|
||||
|
||||
if (result != "OK") return "fail $result"
|
||||
return obj1.o + obj1.k
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
var result = "fail"
|
||||
|
||||
open class Base(val o: String, val k: String)
|
||||
class Derived : Base(k = { result = "O"; "K"}() , o = {result += "K"; "O"}()) {}
|
||||
|
||||
fun box(): String {
|
||||
val derived = Derived()
|
||||
|
||||
if (result != "OK") return "fail $result"
|
||||
return derived.o + derived.k
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
open class Base(val addr: Long, val name: String)
|
||||
|
||||
fun box(): String {
|
||||
val obj1 = object : Base(name = "OK", addr = 0x1234L) {}
|
||||
if (obj1.addr != 0x1234L) return "fail ${obj1.addr}"
|
||||
return obj1.name
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
abstract class Base(val s: String, vararg ints: Int)
|
||||
|
||||
fun foo(s: String, ints: IntArray) = object : Base(ints = *ints, s = s) {}
|
||||
|
||||
fun box(): String {
|
||||
return foo("OK", intArrayOf(1, 2)).s
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
open class Foo(val value: String) {
|
||||
|
||||
open inner class Inner(val d: Double = -1.0, val s: String, vararg val y: Int) {
|
||||
open fun result() = "Fail"
|
||||
}
|
||||
|
||||
val obj = object : Inner(s = "O") {
|
||||
override fun result() = s + value
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return Foo("K").obj.result()
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
fun box(): String {
|
||||
val capture = "O"
|
||||
|
||||
class Local {
|
||||
val captured = capture
|
||||
|
||||
open inner class Inner(val d: Double = -1.0, val s: String, vararg val y: Int) {
|
||||
open fun result() = "Fail"
|
||||
}
|
||||
|
||||
val obj = object : Inner(s = "K") {
|
||||
override fun result() = capture + s
|
||||
}
|
||||
}
|
||||
|
||||
return Local().obj.result()
|
||||
}
|
||||
+36
@@ -206,6 +206,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/argumentOrder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("argumentOrderInObjectSuperCall.kt")
|
||||
public void testArgumentOrderInObjectSuperCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/argumentOrderInObjectSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("argumentOrderInSuperCall.kt")
|
||||
public void testArgumentOrderInSuperCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/argumentOrderInSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("arguments.kt")
|
||||
public void testArguments() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/arguments.kt");
|
||||
@@ -18672,6 +18684,30 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt17464_linkedMapOf.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18356.kt")
|
||||
public void testKt18356() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt18356.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18356_2.kt")
|
||||
public void testKt18356_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt18356_2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExtendsInner.kt")
|
||||
public void testObjectExtendsInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/objectExtendsInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExtendsLocalInner.kt")
|
||||
public void testObjectExtendsLocalInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/objectExtendsLocalInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -206,6 +206,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/argumentOrder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("argumentOrderInObjectSuperCall.kt")
|
||||
public void testArgumentOrderInObjectSuperCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/argumentOrderInObjectSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("argumentOrderInSuperCall.kt")
|
||||
public void testArgumentOrderInSuperCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/argumentOrderInSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("arguments.kt")
|
||||
public void testArguments() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/arguments.kt");
|
||||
@@ -18672,6 +18684,30 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt17464_linkedMapOf.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18356.kt")
|
||||
public void testKt18356() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt18356.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18356_2.kt")
|
||||
public void testKt18356_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt18356_2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExtendsInner.kt")
|
||||
public void testObjectExtendsInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/objectExtendsInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExtendsLocalInner.kt")
|
||||
public void testObjectExtendsLocalInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/objectExtendsLocalInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -206,6 +206,18 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/argumentOrder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("argumentOrderInObjectSuperCall.kt")
|
||||
public void testArgumentOrderInObjectSuperCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/argumentOrderInObjectSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("argumentOrderInSuperCall.kt")
|
||||
public void testArgumentOrderInSuperCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/argumentOrderInSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("arguments.kt")
|
||||
public void testArguments() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/arguments.kt");
|
||||
@@ -18672,6 +18684,30 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt17464_linkedMapOf.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18356.kt")
|
||||
public void testKt18356() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt18356.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18356_2.kt")
|
||||
public void testKt18356_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt18356_2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExtendsInner.kt")
|
||||
public void testObjectExtendsInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/objectExtendsInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExtendsLocalInner.kt")
|
||||
public void testObjectExtendsLocalInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/objectExtendsLocalInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+36
@@ -332,6 +332,18 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/argumentOrder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
|
||||
}
|
||||
|
||||
@TestMetadata("argumentOrderInObjectSuperCall.kt")
|
||||
public void testArgumentOrderInObjectSuperCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/argumentOrderInObjectSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("argumentOrderInSuperCall.kt")
|
||||
public void testArgumentOrderInSuperCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/argumentOrderInSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("arguments.kt")
|
||||
public void testArguments() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/argumentOrder/arguments.kt");
|
||||
@@ -22650,6 +22662,30 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt17464_linkedMapOf.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18356.kt")
|
||||
public void testKt18356() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt18356.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18356_2.kt")
|
||||
public void testKt18356_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/kt18356_2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExtendsInner.kt")
|
||||
public void testObjectExtendsInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/objectExtendsInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExtendsLocalInner.kt")
|
||||
public void testObjectExtendsLocalInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/super/superConstructor/objectExtendsLocalInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user