diff --git a/compiler/android-tests/tests/org/jetbrains/kotlin/android/tests/SpecialFiles.java b/compiler/android-tests/tests/org/jetbrains/kotlin/android/tests/SpecialFiles.java index 8a15e863f0d..7f8b58d0a18 100644 --- a/compiler/android-tests/tests/org/jetbrains/kotlin/android/tests/SpecialFiles.java +++ b/compiler/android-tests/tests/org/jetbrains/kotlin/android/tests/SpecialFiles.java @@ -52,6 +52,7 @@ public class SpecialFiles { excludedFiles.add("kt17091.kt"); excludedFiles.add("kt17091_2.kt"); excludedFiles.add("kt17091_3.kt"); + excludedFiles.add("kt22906.kt"); // Reflection is used to check full class name excludedFiles.add("native"); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperCodegen.java index 4fa81cd93e9..512717537e1 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperCodegen.java @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.codegen; import kotlin.collections.CollectionsKt; +import kotlin.text.StringsKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.backend.common.CodegenUtil; import org.jetbrains.kotlin.codegen.state.GenerationState; @@ -26,10 +27,8 @@ import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl; import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl; import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil; import org.jetbrains.kotlin.incremental.components.NoLookupLocation; -import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.Name; -import org.jetbrains.kotlin.psi.KtDeclaration; import org.jetbrains.kotlin.psi.KtFile; import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt; @@ -200,9 +199,10 @@ public class SamWrapperCodegen { outermostOwner = filePartFqName; } else { - ClassifierDescriptor outermostClassifier = DescriptorUtils.getParentOfType(contextDescriptor, ClassDescriptor.class); + ClassifierDescriptor outermostClassifier = getOutermostParentClass(contextDescriptor); if (outermostClassifier == null) throw new IllegalStateException("Can't find outermost parent class for " + contextDescriptor); - outermostOwner = filePartFqName.parent().child(outermostClassifier.getName()); + String internalName = typeMapper.mapType(outermostClassifier).getInternalName(); + outermostOwner = filePartFqName.parent().child(Name.identifier(StringsKt.substringAfterLast(internalName, '/', internalName))); } String shortName = String.format( @@ -213,4 +213,15 @@ public class SamWrapperCodegen { ); return outermostOwner.parent().child(Name.identifier(shortName)); } + + private static ClassDescriptor getOutermostParentClass(CallableMemberDescriptor contextDescriptor) { + ClassDescriptor parent = DescriptorUtils.getParentOfType(contextDescriptor, ClassDescriptor.class, true); + ClassDescriptor next; + do { + next = DescriptorUtils.getParentOfType(parent, ClassDescriptor.class, true); + if (next != null) parent = next; + } + while (next != null); + return parent; + } } diff --git a/compiler/testData/codegen/box/sam/kt17091_4.kt b/compiler/testData/codegen/box/sam/kt17091_4.kt new file mode 100644 index 00000000000..e41aad8b0de --- /dev/null +++ b/compiler/testData/codegen/box/sam/kt17091_4.kt @@ -0,0 +1,36 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME +// FILE: Foo.kt +@file:JvmMultifileClass +@file:JvmName("testX") +package test + +class A2 { + fun doWork(job: () -> Unit) { + Runnable(job) + Runnable(job) + Runnable(job) + } + + fun foo(job: () -> Unit) { + Runnable(job) + Runnable(job) + Runnable(job) + } +} + + +// FILE: kt17091_3.kt + +fun box(): String { + if (java.lang.Class.forName("Kt17091_3Kt\$sam\$java_util_concurrent_Callable$0") == null) return "fail: can't find sam wrapper" + + if (java.lang.Class.forName("test.A2\$sam\$java_lang_Runnable$0") == null) return "fail 2: can't find sam wrapper" + + return A().foo().call() +} + +class A { + val f = {"OK"} + fun foo() = java.util.concurrent.Callable(f) +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/sam/kt22906.kt b/compiler/testData/codegen/box/sam/kt22906.kt new file mode 100644 index 00000000000..16f799c8a9e --- /dev/null +++ b/compiler/testData/codegen/box/sam/kt22906.kt @@ -0,0 +1,26 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME +// FILE: kt22906_1.kt +package test + +class C { + fun startTemplate(): String { + val y = object { + fun foo(): String { + val job = { "OK" } + return java.util.concurrent.Callable(job).call() + } + + } + return y.foo() + } +} + +// FILE: kt22906_2.kt +import test.* + +fun box(): String { + if (java.lang.Class.forName("test.C\$sam\$java_util_concurrent_Callable\$0") == null) return "fail: can't find sam wrapper" + + return C().startTemplate() +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/sam/kt22906_2.kt b/compiler/testData/codegen/box/sam/kt22906_2.kt new file mode 100644 index 00000000000..1699464b814 --- /dev/null +++ b/compiler/testData/codegen/box/sam/kt22906_2.kt @@ -0,0 +1,36 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME +// FILE: kt22906_1.kt +package test + +class C { + fun startTemplate(): String { + val y = object { + fun foo(): String { + val job = { "OK" } + return java.util.concurrent.Callable(job).call() + } + + } + return y.foo() + } + + fun foo() { + val y = object { + fun foo(): String { + val job = { "OK2" } + return java.util.concurrent.Callable(job).call() + } + + } + } +} + +// FILE: kt22906_2.kt +import test.* + +fun box(): String { + if (java.lang.Class.forName("test.C\$sam\$java_util_concurrent_Callable\$0") == null) return "fail: can't find sam wrapper" + + return C().startTemplate() +} \ No newline at end of file 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 301fd16feef..180241ae5ff 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 @@ -19503,6 +19503,24 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("kt17091_4.kt") + public void testKt17091_4() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt17091_4.kt"); + doTest(fileName); + } + + @TestMetadata("kt22906.kt") + public void testKt22906() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906.kt"); + doTest(fileName); + } + + @TestMetadata("kt22906_2.kt") + public void testKt22906_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906_2.kt"); + doTest(fileName); + } + @TestMetadata("compiler/testData/codegen/box/sam/constructors") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index affe1b6849c..0486d9e4761 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -19503,6 +19503,24 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("kt17091_4.kt") + public void testKt17091_4() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt17091_4.kt"); + doTest(fileName); + } + + @TestMetadata("kt22906.kt") + public void testKt22906() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906.kt"); + doTest(fileName); + } + + @TestMetadata("kt22906_2.kt") + public void testKt22906_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906_2.kt"); + doTest(fileName); + } + @TestMetadata("compiler/testData/codegen/box/sam/constructors") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 342788b7718..a2a0e05e0e6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -19503,6 +19503,24 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("kt17091_4.kt") + public void testKt17091_4() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt17091_4.kt"); + doTest(fileName); + } + + @TestMetadata("kt22906.kt") + public void testKt22906() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906.kt"); + doTest(fileName); + } + + @TestMetadata("kt22906_2.kt") + public void testKt22906_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906_2.kt"); + doTest(fileName); + } + @TestMetadata("compiler/testData/codegen/box/sam/constructors") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)