diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperClasses.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperClasses.kt index d8a61af19dd..202a995dcaa 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperClasses.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperClasses.kt @@ -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 + } } diff --git a/compiler/testData/codegen/box/sam/kt24825.kt b/compiler/testData/codegen/box/sam/kt24825.kt new file mode 100644 index 00000000000..a51fe3483a9 --- /dev/null +++ b/compiler/testData/codegen/box/sam/kt24825.kt @@ -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() + + 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 +} diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index b0057917604..367144e9bcd 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index dff25db7b90..851a8e6c6eb 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 24ee9140e56..1ca40cef10f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -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");