diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt index 4469e551cc2..2cccc5a1143 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt @@ -207,6 +207,7 @@ class LocalDeclarationsLowering( val localFunctions: MutableMap = LinkedHashMap() val localClasses: MutableMap = LinkedHashMap() val localClassConstructors: MutableMap = LinkedHashMap() + val usedLocalFunctionNames: MutableSet = mutableSetOf() val transformedDeclarations = mutableMapOf() @@ -530,8 +531,9 @@ class LocalDeclarationsLowering( private fun suggestLocalName(declaration: IrDeclarationWithName): String { localFunctions[declaration]?.let { + val baseName = if (declaration.name.isSpecial) "lambda" else declaration.name if (it.index >= 0) - return "lambda-${it.index}" + return "$baseName-${it.index}" } return localNameProvider.localName(declaration) @@ -827,9 +829,12 @@ class LocalDeclarationsLowering( localFunctions[declaration] = LocalFunctionContext( declaration, - if (declaration.name.isSpecial) (scopeWithIr as ScopeWithCounter).counter++ else -1, + if (declaration.name.isSpecial || declaration.name in usedLocalFunctionNames) + (scopeWithIr as ScopeWithCounter).counter++ + else -1, scopeWithIr.irElement as IrDeclarationContainer ) + usedLocalFunctionNames.add(declaration.name) } } diff --git a/compiler/testData/codegen/box/functions/localFunctions/nameClash.kt b/compiler/testData/codegen/box/functions/localFunctions/nameClash.kt new file mode 100644 index 00000000000..2374fe33694 --- /dev/null +++ b/compiler/testData/codegen/box/functions/localFunctions/nameClash.kt @@ -0,0 +1,12 @@ +// IGNORE_BACKEND_FIR: JVM_IR + +fun test(b: Boolean): String { + if (b) { + fun result() = "OK" + return result() + } else { + fun result() = "Fail" + return result() + } +} +fun box(): String = test(true) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 67564b99932..7c5650be0fe 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -12206,6 +12206,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/functions/localFunctions/localFunctionVsLocalVariable.kt"); } + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/box/functions/localFunctions/nameClash.kt"); + } + @TestMetadata("overloadedLocalFunWithoutClosure.kt") public void testOverloadedLocalFunWithoutClosure() throws Exception { runTest("compiler/testData/codegen/box/functions/localFunctions/overloadedLocalFunWithoutClosure.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 9fc94a96ef3..36a591536bf 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -12206,6 +12206,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/functions/localFunctions/localFunctionVsLocalVariable.kt"); } + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/box/functions/localFunctions/nameClash.kt"); + } + @TestMetadata("overloadedLocalFunWithoutClosure.kt") public void testOverloadedLocalFunWithoutClosure() throws Exception { runTest("compiler/testData/codegen/box/functions/localFunctions/overloadedLocalFunWithoutClosure.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 7fa08947b49..45fbbd5616f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -11051,6 +11051,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/functions/localFunctions/localFunctionVsLocalVariable.kt"); } + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/box/functions/localFunctions/nameClash.kt"); + } + @TestMetadata("overloadedLocalFunWithoutClosure.kt") public void testOverloadedLocalFunWithoutClosure() throws Exception { runTest("compiler/testData/codegen/box/functions/localFunctions/overloadedLocalFunWithoutClosure.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 6e07f3cf147..7042980af16 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -11051,6 +11051,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/functions/localFunctions/localFunctionVsLocalVariable.kt"); } + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/box/functions/localFunctions/nameClash.kt"); + } + @TestMetadata("overloadedLocalFunWithoutClosure.kt") public void testOverloadedLocalFunWithoutClosure() throws Exception { runTest("compiler/testData/codegen/box/functions/localFunctions/overloadedLocalFunWithoutClosure.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 84d559eb6b5..d79440211b4 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -9511,6 +9511,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/functions/localFunctions/localFunctionVsLocalVariable.kt"); } + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/box/functions/localFunctions/nameClash.kt"); + } + @TestMetadata("overloadedLocalFunWithoutClosure.kt") public void testOverloadedLocalFunWithoutClosure() throws Exception { runTest("compiler/testData/codegen/box/functions/localFunctions/overloadedLocalFunWithoutClosure.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 8aa1715b94f..3e52472bf6e 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -10591,6 +10591,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/functions/localFunctions/localFunctionVsLocalVariable.kt"); } + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/box/functions/localFunctions/nameClash.kt"); + } + @TestMetadata("overloadedLocalFunWithoutClosure.kt") public void testOverloadedLocalFunWithoutClosure() throws Exception { runTest("compiler/testData/codegen/box/functions/localFunctions/overloadedLocalFunWithoutClosure.kt");