From c1c8660e55bade52176b8ea7d0bf09f61a13d892 Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Wed, 7 Mar 2018 12:34:45 +0100 Subject: [PATCH] Don't try to transform sam wrappers in same module #KT-22304 Fixed --- .../kotlin/codegen/SamWrapperCodegen.java | 3 ++- .../kotlin/codegen/inline/MethodInliner.kt | 2 +- .../codegen/inline/inlineCodegenUtils.kt | 13 +++++++--- compiler/testData/cli/jvm/kt22304.args | 5 ++++ compiler/testData/cli/jvm/kt22304.out | 1 + compiler/testData/cli/jvm/kt22304_1.kt | 11 ++++++++ compiler/testData/cli/jvm/kt22304_2.kt | 12 +++++++++ compiler/testData/cli/jvm/kt22304_3.kt | 12 +++++++++ .../boxInline/anonymousObject/sam/kt22304.kt | 26 +++++++++++++++++++ .../copySamOnInline2.kt | 2 +- .../IrBlackBoxInlineCodegenTestGenerated.java | 6 +++++ ...otlinAgainstInlineKotlinTestGenerated.java | 6 +++++ .../kotlin/cli/CliTestGenerated.java | 6 +++++ .../BlackBoxInlineCodegenTestGenerated.java | 6 +++++ ...otlinAgainstInlineKotlinTestGenerated.java | 6 +++++ 15 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/cli/jvm/kt22304.args create mode 100644 compiler/testData/cli/jvm/kt22304.out create mode 100644 compiler/testData/cli/jvm/kt22304_1.kt create mode 100644 compiler/testData/cli/jvm/kt22304_2.kt create mode 100644 compiler/testData/cli/jvm/kt22304_3.kt create mode 100644 compiler/testData/codegen/boxInline/anonymousObject/sam/kt22304.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperCodegen.java index 512717537e1..ac68eb4b943 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/SamWrapperCodegen.java @@ -55,6 +55,7 @@ public class SamWrapperCodegen { private final SamType samType; private final MemberCodegen parentCodegen; private final int visibility; + public static final String SAM_WRAPPER_SUFFIX = "$0"; public SamWrapperCodegen( @NotNull GenerationState state, @@ -206,7 +207,7 @@ public class SamWrapperCodegen { } String shortName = String.format( - "%s$sam%s$%s$0", + "%s$sam%s$%s" + SAM_WRAPPER_SUFFIX, outermostOwner.shortName().asString(), (isInsideInline ? "$i" : ""), DescriptorUtils.getFqNameSafe(samType.getJavaClassDescriptor()).asString().replace('.', '_') diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt index de00e7c03ff..c5a07593f8d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt @@ -185,7 +185,7 @@ class MethodInliner( } override fun anew(type: Type) { - if (isOldSamWrapper(type.internalName) || isAnonymousClass(type.internalName)) { + if (isSamWrapper(type.internalName) || isAnonymousClass(type.internalName)) { handleAnonymousObjectRegeneration() } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/inlineCodegenUtils.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/inlineCodegenUtils.kt index 25560074250..d1e41eaa371 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/inlineCodegenUtils.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/inlineCodegenUtils.kt @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.codegen.AsmUtil import org.jetbrains.kotlin.codegen.BaseExpressionCodegen import org.jetbrains.kotlin.codegen.ExpressionCodegen import org.jetbrains.kotlin.codegen.JvmCodegenUtil +import org.jetbrains.kotlin.codegen.SamWrapperCodegen.SAM_WRAPPER_SUFFIX import org.jetbrains.kotlin.codegen.`when`.WhenByEnumsMapping import org.jetbrains.kotlin.codegen.binding.CodegenBinding import org.jetbrains.kotlin.codegen.context.CodegenContext @@ -226,14 +227,18 @@ internal fun isAnonymousSingletonLoad(internalName: String, fieldName: String): * hash * ); */ -internal fun isOldSamWrapper(internalName: String) = - internalName.contains("\$sam$") && internalName.substringAfter("\$i$", "").run { length == 8 && toLongOrNull(16) != null } +private fun isOldSamWrapper(internalName: String) = + internalName.contains("\$sam$") && internalName.substringAfter("\$i$", "").run { length == 8 && toLongOrNull(16) != null } + +internal fun isSamWrapper(internalName: String) = + (internalName.endsWith(SAM_WRAPPER_SUFFIX) && internalName.contains("\$sam\$i\$")) || isOldSamWrapper(internalName) + internal fun isSamWrapperConstructorCall(internalName: String, methodName: String) = - isConstructor(methodName) && isOldSamWrapper(internalName) + isConstructor(methodName) && isSamWrapper(internalName) internal fun isAnonymousClass(internalName: String) = - !isOldSamWrapper(internalName) && + !isSamWrapper(internalName) && internalName.substringAfterLast('/').substringAfterLast("$", "").isInteger() fun wrapWithMaxLocalCalc(methodNode: MethodNode) = diff --git a/compiler/testData/cli/jvm/kt22304.args b/compiler/testData/cli/jvm/kt22304.args new file mode 100644 index 00000000000..99834a762a3 --- /dev/null +++ b/compiler/testData/cli/jvm/kt22304.args @@ -0,0 +1,5 @@ +$TESTDATA_DIR$/kt22304_1.kt +$TESTDATA_DIR$/kt22304_2.kt +$TESTDATA_DIR$/kt22304_3.kt +-d +$TEMP_DIR$ diff --git a/compiler/testData/cli/jvm/kt22304.out b/compiler/testData/cli/jvm/kt22304.out new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/compiler/testData/cli/jvm/kt22304.out @@ -0,0 +1 @@ +OK diff --git a/compiler/testData/cli/jvm/kt22304_1.kt b/compiler/testData/cli/jvm/kt22304_1.kt new file mode 100644 index 00000000000..507475c2d6c --- /dev/null +++ b/compiler/testData/cli/jvm/kt22304_1.kt @@ -0,0 +1,11 @@ +package test + +import java.util.concurrent.Callable + +fun test(): String = "" + +inline fun String.switchMapOnce(crossinline mapper: (String) -> String): String { + Callable(::test) + return { mapper(this) }() +} + diff --git a/compiler/testData/cli/jvm/kt22304_2.kt b/compiler/testData/cli/jvm/kt22304_2.kt new file mode 100644 index 00000000000..85256b281fd --- /dev/null +++ b/compiler/testData/cli/jvm/kt22304_2.kt @@ -0,0 +1,12 @@ +package beforeTest //package name should be less than imported one + +import test.* + +fun main(args: Array) { + "O".switchMapOnce { + + "K".switchMapOnce { + "OK" + } + } +} \ No newline at end of file diff --git a/compiler/testData/cli/jvm/kt22304_3.kt b/compiler/testData/cli/jvm/kt22304_3.kt new file mode 100644 index 00000000000..64739cd7d05 --- /dev/null +++ b/compiler/testData/cli/jvm/kt22304_3.kt @@ -0,0 +1,12 @@ +package ttest //package name should be more than imported one + +import test.* + +fun foo(): String { + return "O".switchMapOnce { + + "K".switchMapOnce { + "OK" + } + } +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/anonymousObject/sam/kt22304.kt b/compiler/testData/codegen/boxInline/anonymousObject/sam/kt22304.kt new file mode 100644 index 00000000000..a2b64453497 --- /dev/null +++ b/compiler/testData/codegen/boxInline/anonymousObject/sam/kt22304.kt @@ -0,0 +1,26 @@ +// FILE: 1.kt +// FULL_JDK + +package test + +import java.util.concurrent.Callable + +fun test(): String = "" + +inline fun String.switchMapOnce(crossinline mapper: (String) -> String): String { + Callable(::test) + return { mapper(this) }() +} +// FILE: 2.kt + +import test.* + +fun box() : String { + return "O".switchMapOnce { + + "K".switchMapOnce { + "OK" + } + } +} + diff --git a/compiler/testData/compileKotlinAgainstKotlin/copySamOnInline2.kt b/compiler/testData/compileKotlinAgainstKotlin/copySamOnInline2.kt index 31fc9ee5eaf..504949834ce 100644 --- a/compiler/testData/compileKotlinAgainstKotlin/copySamOnInline2.kt +++ b/compiler/testData/compileKotlinAgainstKotlin/copySamOnInline2.kt @@ -16,7 +16,7 @@ import test.* fun box(): String { val anotherModule = doWork { "K" } - if (anotherModule.javaClass.name != "BKt\$box$\$inlined\$doWork$1") return "class should be regenerated, but ${anotherModule.javaClass.name}" + if (anotherModule.javaClass.name != "BKt\$inlined\$sam\$i\$java_util_concurrent_Callable\$0") return "class should be regenerated, but ${anotherModule.javaClass.name}" return "OK" } diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index fa25b02bf9d..d7990820a8d 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -482,6 +482,12 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli doTest(fileName); } + @TestMetadata("kt22304.kt") + public void testKt22304() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/sam/kt22304.kt"); + doTest(fileName); + } + @TestMetadata("samOnCallSite.kt") public void testSamOnCallSite() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/sam/samOnCallSite.kt"); diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index fefb2869940..1b95347fd48 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -482,6 +482,12 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC doTest(fileName); } + @TestMetadata("kt22304.kt") + public void testKt22304() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/sam/kt22304.kt"); + doTest(fileName); + } + @TestMetadata("samOnCallSite.kt") public void testSamOnCallSite() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/sam/samOnCallSite.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index 0e8186f14f5..62b1ade3fbe 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -387,6 +387,12 @@ public class CliTestGenerated extends AbstractCliTest { doJvmTest(fileName); } + @TestMetadata("kt22304.args") + public void testKt22304() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/kt22304.args"); + doJvmTest(fileName); + } + @TestMetadata("languageVersion.args") public void testLanguageVersion() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/languageVersion.args"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 3eb727e2f64..9b0e6e9f9a2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -482,6 +482,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTest(fileName); } + @TestMetadata("kt22304.kt") + public void testKt22304() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/sam/kt22304.kt"); + doTest(fileName); + } + @TestMetadata("samOnCallSite.kt") public void testSamOnCallSite() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/sam/samOnCallSite.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index dafd30265a7..c6a2dec351f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -482,6 +482,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doTest(fileName); } + @TestMetadata("kt22304.kt") + public void testKt22304() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/sam/kt22304.kt"); + doTest(fileName); + } + @TestMetadata("samOnCallSite.kt") public void testSamOnCallSite() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/sam/samOnCallSite.kt");