Properly find invoke method on default lambda inlining

In general case parameter type could differ from actual default lambda type.
  E.g.: fun inlineFun(s: (Child) -> Base = { a: Base -> a as Child}),
  where type of default lambda is '(Base) -> Child'.
  In such case we should find somehow actual invoke method in bytecode knowing
  only name, number of parameters and that's actual invoke is non-synthetic
  regardless of bridge one.

  #KT-21946 Fixed
This commit is contained in:
Mikhael Bogdanov
2018-12-27 15:03:40 +01:00
parent 3040a2b145
commit 6bf70a5dd2
14 changed files with 332 additions and 18 deletions
@@ -157,17 +157,18 @@ class DefaultLambda(
isBoundCallableReference = (isFunctionReference || isPropertyReference) && capturedVars.isNotEmpty()
invokeMethod = Method(
(if (isPropertyReference) OperatorNameConventions.GET else OperatorNameConventions.INVOKE).asString(),
sourceCompiler.state.typeMapper.mapSignatureSkipGeneric(invokeMethodDescriptor).asmMethod.descriptor
)
val methodName = (if (isPropertyReference) OperatorNameConventions.GET else OperatorNameConventions.INVOKE).asString()
val signature = sourceCompiler.state.typeMapper.mapSignatureSkipGeneric(invokeMethodDescriptor).asmMethod.descriptor
node = getMethodNode(
classReader.b,
invokeMethod.name,
invokeMethod.descriptor,
lambdaClassType
) ?: error("Can't find method '${invokeMethod.name}${invokeMethod.descriptor}' in '${classReader.className}'")
methodName,
signature,
lambdaClassType,
signatureAmbiguity = true
) ?: error("Can't find method '$methodName$signature' in '${classReader.className}'")
invokeMethod = Method(node.node.name, node.node.desc)
if (needReification) {
//nested classes could also require reification
@@ -88,7 +88,8 @@ internal fun getMethodNode(
classData: ByteArray,
methodName: String,
methodDescriptor: String,
classType: Type
classType: Type,
signatureAmbiguity: Boolean = false
): SMAPAndMethodNode? {
val cr = ClassReader(classData)
var node: MethodNode? = null
@@ -112,17 +113,28 @@ internal fun getMethodNode(
signature: String?,
exceptions: Array<String>?
): MethodVisitor? {
if (methodName == name && methodDescriptor == desc) {
node = object : MethodNode(Opcodes.API_VERSION, access, name, desc, signature, exceptions) {
override fun visitLineNumber(line: Int, start: Label) {
super.visitLineNumber(line, start)
lines[0] = Math.min(lines[0], line)
lines[1] = Math.max(lines[1], line)
}
if (methodName != name || (signatureAmbiguity && access.and(Opcodes.ACC_SYNTHETIC) != 0)) return null
if (methodDescriptor != desc) {
val sameNumberOfParameters = Type.getArgumentTypes(methodDescriptor).size == Type.getArgumentTypes(desc).size
if (!signatureAmbiguity || !sameNumberOfParameters) {
return null
}
return node
}
return null
node?.let { existing ->
throw AssertionError("Can't find proper '$name' method for inline: ambiguity between '${existing.name + existing.desc}' and '${name + desc}'")
}
return object : MethodNode(Opcodes.API_VERSION, access, name, desc, signature, exceptions) {
override fun visitLineNumber(line: Int, start: Label) {
super.visitLineNumber(line, start)
lines[0] = Math.min(lines[0], line)
lines[1] = Math.max(lines[1], line)
}
}.also {
node = it
}
}
}, ClassReader.SKIP_FRAMES or if (GENERATE_SMAP) 0 else ClassReader.SKIP_DEBUG)
@@ -0,0 +1,22 @@
// IGNORE_BACKEND: JVM_IR
// FILE: 1.kt
// SKIP_INLINE_CHECK_IN: inlineFun$default
package test
open class Base
class Child(val value: String) : Base()
fun foo(a: Base): Child = a as Child
inline fun inlineFun(s: (Child) -> Base = ::foo): Base {
return s(Child("OK"))
}
// FILE: 2.kt
import test.*
fun box(): String {
return (inlineFun() as Child).value
}
@@ -0,0 +1,17 @@
// IGNORE_BACKEND: JVM_IR
// FILE: 1.kt
// SKIP_INLINE_CHECK_IN: inlineFun$default
package test
fun foo(a: Number): String = "OK"
inline fun inlineFun(s: (Double) -> String = ::foo): String {
return s(1.0)
}
// FILE: 2.kt
import test.*
fun box(): String {
return inlineFun()
}
@@ -0,0 +1,19 @@
// IGNORE_BACKEND: JVM_IR
// FILE: 1.kt
// SKIP_INLINE_CHECK_IN: inlineFun$default
package test
open class Base
class Child(val value: String): Base()
inline fun inlineFun(s: (Child) -> Base = { a: Base -> a as Child}): Base {
return s(Child("OK"))
}
// FILE: 2.kt
import test.*
fun box(): String {
return (inlineFun() as Child).value
}
@@ -0,0 +1,21 @@
// IGNORE_BACKEND: JVM_IR
// FILE: 1.kt
// NO_CHECK_LAMBDA_INLINING
package test
interface Call {
fun run(): String
}
inline fun test(p: String, s: () -> Call = {
object : Call {
override fun run() = p
}
}) = s()
// FILE: 2.kt
import test.*
fun box(): String {
return test("OK").run()
}
@@ -0,0 +1,17 @@
// IGNORE_BACKEND: JVM_IR
// FILE: 1.kt
// SKIP_INLINE_CHECK_IN: inlineFun$default
package test
class Item
inline fun inlineFun(number: String, getItem: ((String) -> String?) = { null }): String {
return number + (getItem(number) ?: "")
}
// FILE: 2.kt
import test.*
fun box(): String {
return inlineFun("OK")
}
@@ -0,0 +1,15 @@
// IGNORE_BACKEND: JVM_IR
// FILE: 1.kt
// SKIP_INLINE_CHECK_IN: inlineFun$default
package test
inline fun inlineFun(action: () -> Any = { "OK" }): Any {
return action()
}
// FILE: 2.kt
import test.*
fun box(): String {
return inlineFun() as String
}
@@ -0,0 +1,25 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM_IR
// WITH_RUNTIME
// FILE: 1.kt
// SKIP_INLINE_CHECK_IN: enumOrThrow$default
package test
enum class TarEnum {
OK
}
inline fun <reified T : Enum<T>> String?.enumOrNull(): T? {
this ?: return null
return enumValues<T>().firstOrNull { it.name == this }
}
inline fun <reified T : Enum<T>> String?.enumOrThrow(handleNull: () -> Throwable = { IllegalArgumentException("Enum type ${T::class.java} not contain value=$this") }): T {
return this.enumOrNull<T>() ?: throw handleNull()
}
// FILE: 2.kt
import test.*
fun box(): String {
return "OK".enumOrThrow<TarEnum>()!!.name
}
@@ -1214,6 +1214,11 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/defaultLambdaInNoInline.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/differentInvokeSignature.kt");
}
@TestMetadata("genericLambda.kt")
public void testGenericLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/genericLambda.kt");
@@ -1239,6 +1244,26 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21827.kt");
}
@TestMetadata("kt21946.kt")
public void testKt21946() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21946.kt");
}
@TestMetadata("kt24477.kt")
public void testKt24477() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt24477.kt");
}
@TestMetadata("kt25106.kt")
public void testKt25106() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt25106.kt");
}
@TestMetadata("kt26636.kt")
public void testKt26636() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt26636.kt");
}
@TestMetadata("noInline.kt")
public void testNoInline() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/noInline.kt");
@@ -1356,6 +1381,16 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/constuctorReference.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature.kt");
}
@TestMetadata("differentInvokeSignature2.kt")
public void testDifferentInvokeSignature2() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature2.kt");
}
@TestMetadata("functionImportedFromObject.kt")
public void testFunctionImportedFromObject() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/functionImportedFromObject.kt");
@@ -1214,6 +1214,11 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/defaultLambdaInNoInline.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/differentInvokeSignature.kt");
}
@TestMetadata("genericLambda.kt")
public void testGenericLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/genericLambda.kt");
@@ -1239,6 +1244,26 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21827.kt");
}
@TestMetadata("kt21946.kt")
public void testKt21946() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21946.kt");
}
@TestMetadata("kt24477.kt")
public void testKt24477() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt24477.kt");
}
@TestMetadata("kt25106.kt")
public void testKt25106() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt25106.kt");
}
@TestMetadata("kt26636.kt")
public void testKt26636() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt26636.kt");
}
@TestMetadata("noInline.kt")
public void testNoInline() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/noInline.kt");
@@ -1356,6 +1381,16 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/constuctorReference.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature.kt");
}
@TestMetadata("differentInvokeSignature2.kt")
public void testDifferentInvokeSignature2() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature2.kt");
}
@TestMetadata("functionImportedFromObject.kt")
public void testFunctionImportedFromObject() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/functionImportedFromObject.kt");
@@ -1214,6 +1214,11 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/defaultLambdaInNoInline.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/differentInvokeSignature.kt");
}
@TestMetadata("genericLambda.kt")
public void testGenericLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/genericLambda.kt");
@@ -1239,6 +1244,26 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21827.kt");
}
@TestMetadata("kt21946.kt")
public void testKt21946() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21946.kt");
}
@TestMetadata("kt24477.kt")
public void testKt24477() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt24477.kt");
}
@TestMetadata("kt25106.kt")
public void testKt25106() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt25106.kt");
}
@TestMetadata("kt26636.kt")
public void testKt26636() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt26636.kt");
}
@TestMetadata("noInline.kt")
public void testNoInline() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/noInline.kt");
@@ -1356,6 +1381,16 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/constuctorReference.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature.kt");
}
@TestMetadata("differentInvokeSignature2.kt")
public void testDifferentInvokeSignature2() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature2.kt");
}
@TestMetadata("functionImportedFromObject.kt")
public void testFunctionImportedFromObject() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/functionImportedFromObject.kt");
@@ -176,6 +176,11 @@ public class InlineDefaultValuesTestsGenerated extends AbstractInlineDefaultValu
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/defaultLambdaInNoInline.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/differentInvokeSignature.kt");
}
@TestMetadata("genericLambda.kt")
public void testGenericLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/genericLambda.kt");
@@ -196,6 +201,21 @@ public class InlineDefaultValuesTestsGenerated extends AbstractInlineDefaultValu
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21827.kt");
}
@TestMetadata("kt21946.kt")
public void testKt21946() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21946.kt");
}
@TestMetadata("kt24477.kt")
public void testKt24477() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt24477.kt");
}
@TestMetadata("kt25106.kt")
public void testKt25106() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt25106.kt");
}
@TestMetadata("noInline.kt")
public void testNoInline() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/noInline.kt");
@@ -313,6 +333,16 @@ public class InlineDefaultValuesTestsGenerated extends AbstractInlineDefaultValu
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/constuctorReference.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature.kt");
}
@TestMetadata("differentInvokeSignature2.kt")
public void testDifferentInvokeSignature2() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature2.kt");
}
@TestMetadata("functionImportedFromObject.kt")
public void testFunctionImportedFromObject() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/functionImportedFromObject.kt");
@@ -176,6 +176,11 @@ public class IrInlineDefaultValuesTestsGenerated extends AbstractIrInlineDefault
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/defaultLambdaInNoInline.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/differentInvokeSignature.kt");
}
@TestMetadata("genericLambda.kt")
public void testGenericLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/genericLambda.kt");
@@ -196,6 +201,21 @@ public class IrInlineDefaultValuesTestsGenerated extends AbstractIrInlineDefault
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21827.kt");
}
@TestMetadata("kt21946.kt")
public void testKt21946() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt21946.kt");
}
@TestMetadata("kt24477.kt")
public void testKt24477() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt24477.kt");
}
@TestMetadata("kt25106.kt")
public void testKt25106() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/kt25106.kt");
}
@TestMetadata("noInline.kt")
public void testNoInline() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/noInline.kt");
@@ -313,6 +333,16 @@ public class IrInlineDefaultValuesTestsGenerated extends AbstractIrInlineDefault
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/constuctorReference.kt");
}
@TestMetadata("differentInvokeSignature.kt")
public void testDifferentInvokeSignature() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature.kt");
}
@TestMetadata("differentInvokeSignature2.kt")
public void testDifferentInvokeSignature2() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/differentInvokeSignature2.kt");
}
@TestMetadata("functionImportedFromObject.kt")
public void testFunctionImportedFromObject() throws Exception {
runTest("compiler/testData/codegen/boxInline/defaultValues/lambdaInlining/callableReferences/functionImportedFromObject.kt");