IR: avoid name clashes between raised local functions.

This commit is contained in:
Georgy Bronnikov
2020-01-16 17:50:57 +03:00
parent 4e0b54e1a6
commit 1c527fc159
8 changed files with 49 additions and 2 deletions
@@ -207,6 +207,7 @@ class LocalDeclarationsLowering(
val localFunctions: MutableMap<IrFunction, LocalFunctionContext> = LinkedHashMap()
val localClasses: MutableMap<IrClass, LocalClassContext> = LinkedHashMap()
val localClassConstructors: MutableMap<IrConstructor, LocalClassConstructorContext> = LinkedHashMap()
val usedLocalFunctionNames: MutableSet<Name> = mutableSetOf()
val transformedDeclarations = mutableMapOf<IrSymbolOwner, IrDeclaration>()
@@ -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)
}
}
@@ -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)
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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");