Generate sam wrappers in inlined lambda in same way as in inline function

#KT-24825 Fixed
This commit is contained in:
Mikhael Bogdanov
2018-07-09 09:48:10 +02:00
parent 93e183c2e4
commit 66e68fbb53
5 changed files with 67 additions and 2 deletions
@@ -16,7 +16,8 @@
package org.jetbrains.kotlin.codegen
import org.jetbrains.annotations.NotNull
import org.jetbrains.kotlin.codegen.context.CodegenContext
import org.jetbrains.kotlin.codegen.context.InlineLambdaContext
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.psi.KtFile
@@ -35,9 +36,19 @@ class SamWrapperClasses(private val state: GenerationState) {
expressionCodegen: ExpressionCodegen,
contextDescriptor: CallableMemberDescriptor
): Type {
val isInsideInline = InlineUtil.isInlineOrContainingInline(expressionCodegen.context.contextDescriptor)
val isInsideInline = InlineUtil.isInlineOrContainingInline(expressionCodegen.context.contextDescriptor) ||
isInsideInlineLambdaContext(expressionCodegen.context, state)
return samInterfaceToWrapperClass.getOrPut(WrapperKey(samType, file, isInsideInline)) {
SamWrapperCodegen(state, samType, expressionCodegen.parentCodegen, isInsideInline).genWrapper(file, contextDescriptor)
}
}
private fun isInsideInlineLambdaContext(context: CodegenContext<*>, state: GenerationState):Boolean {
var parent: CodegenContext<*>? = context
while (parent != null && parent != state.rootContext) {
if (parent is InlineLambdaContext) return true
parent = parent.parentContext
}
return false
}
}
+39
View File
@@ -0,0 +1,39 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: JavaClass.java
public class JavaClass {
public static void run(Runnable l) {
l.run();
}
public static void s(Runnable a, Runnable onError) {
a.run();
}
}
// FILE: main.kt
var state = ""
fun unit() {}
class B {
val s = mutableListOf<Unit>()
init {
s.add(JavaClass.s({ state += "O" }, ::unit))
Any().apply {
JavaClass.run {
s.add(JavaClass.s({ state += "K" }, ::unit))
}
}
}
}
fun box(): String {
B()
return state
}
@@ -19390,6 +19390,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/sam/kt22906_2.kt");
}
@TestMetadata("kt24825.kt")
public void testKt24825() throws Exception {
runTest("compiler/testData/codegen/box/sam/kt24825.kt");
}
@TestMetadata("partialSam.kt")
public void testPartialSam() throws Exception {
runTest("compiler/testData/codegen/box/sam/partialSam.kt");
@@ -19390,6 +19390,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/sam/kt22906_2.kt");
}
@TestMetadata("kt24825.kt")
public void testKt24825() throws Exception {
runTest("compiler/testData/codegen/box/sam/kt24825.kt");
}
@TestMetadata("partialSam.kt")
public void testPartialSam() throws Exception {
runTest("compiler/testData/codegen/box/sam/partialSam.kt");
@@ -19390,6 +19390,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/sam/kt22906_2.kt");
}
@TestMetadata("kt24825.kt")
public void testKt24825() throws Exception {
runTest("compiler/testData/codegen/box/sam/kt24825.kt");
}
@TestMetadata("partialSam.kt")
public void testPartialSam() throws Exception {
runTest("compiler/testData/codegen/box/sam/partialSam.kt");