diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/codegenUtil.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/codegenUtil.kt index a85e3ccc116..309a268b4b2 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/codegenUtil.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/codegenUtil.kt @@ -48,11 +48,11 @@ import org.jetbrains.org.objectweb.asm.Label import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter import org.jetbrains.org.objectweb.asm.commons.Method +import org.jetbrains.org.objectweb.asm.tree.MethodNode import org.jetbrains.org.objectweb.asm.util.Textifier import org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor -import org.jetbrains.org.objectweb.asm.tree.MethodNode -import java.io.StringWriter import java.io.PrintWriter +import java.io.StringWriter import java.util.* fun generateIsCheck( @@ -271,6 +271,7 @@ fun Collection.withVariableIndices(): List> = mutableListO } fun FunctionDescriptor.isGenericToArray(): Boolean { + if (name.asString() != "toArray") return false if (valueParameters.size != 1 || typeParameters.size != 1) return false val returnType = returnType ?: throw AssertionError(toString()) @@ -284,6 +285,7 @@ fun FunctionDescriptor.isGenericToArray(): Boolean { } fun FunctionDescriptor.isNonGenericToArray(): Boolean { + if (name.asString() != "toArray") return false if (!valueParameters.isEmpty() || !typeParameters.isEmpty()) return false val returnType = returnType diff --git a/compiler/testData/codegen/box/toArray/incorrectToArrayDetection.kt b/compiler/testData/codegen/box/toArray/incorrectToArrayDetection.kt new file mode 100644 index 00000000000..58ebed15909 --- /dev/null +++ b/compiler/testData/codegen/box/toArray/incorrectToArrayDetection.kt @@ -0,0 +1,14 @@ +// TARGET_BACKEND: JVM +// FULL_JDK +// WITH_RUNTIME + +import java.lang.reflect.Modifier + +abstract class A : Collection { + protected fun foo(x: Array): Array = x +} + +fun box(): String { + val method = A::class.java.declaredMethods.single { it.name == "foo" } + return if (Modifier.isProtected(method.modifiers)) "OK" else "Fail: $method" +} 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 3b3939f51cf..5b07cc4ca65 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 @@ -20585,6 +20585,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/toArray"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("incorrectToArrayDetection.kt") + public void testIncorrectToArrayDetection() throws Exception { + runTest("compiler/testData/codegen/box/toArray/incorrectToArrayDetection.kt"); + } + @TestMetadata("kt3177-toTypedArray.kt") public void testKt3177_toTypedArray() throws Exception { runTest("compiler/testData/codegen/box/toArray/kt3177-toTypedArray.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 35c299de0a4..829205a2b34 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -20585,6 +20585,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/toArray"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("incorrectToArrayDetection.kt") + public void testIncorrectToArrayDetection() throws Exception { + runTest("compiler/testData/codegen/box/toArray/incorrectToArrayDetection.kt"); + } + @TestMetadata("kt3177-toTypedArray.kt") public void testKt3177_toTypedArray() throws Exception { runTest("compiler/testData/codegen/box/toArray/kt3177-toTypedArray.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 32f0f3bb434..a0dd54a65a1 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -20585,6 +20585,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/toArray"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("incorrectToArrayDetection.kt") + public void testIncorrectToArrayDetection() throws Exception { + runTest("compiler/testData/codegen/box/toArray/incorrectToArrayDetection.kt"); + } + @TestMetadata("kt3177-toTypedArray.kt") public void testKt3177_toTypedArray() throws Exception { runTest("compiler/testData/codegen/box/toArray/kt3177-toTypedArray.kt");