[IR BE] Fix SharedVariableLowering
* make it able to detect shared vars under any declaration kind, not only IrFunction * Add & update tests
This commit is contained in:
+12
-22
@@ -18,12 +18,9 @@ package org.jetbrains.kotlin.backend.common.lower
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.BackendContext
|
||||
import org.jetbrains.kotlin.backend.common.FunctionLoweringPass
|
||||
import org.jetbrains.kotlin.backend.common.makePhase
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.declarations.IrVariable
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrGetValue
|
||||
import org.jetbrains.kotlin.ir.expressions.IrSetVariable
|
||||
@@ -50,40 +47,33 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
|
||||
}
|
||||
|
||||
private fun collectSharedVariables() {
|
||||
irFunction.acceptVoid(object : IrElementVisitorVoid {
|
||||
val declarationsStack = ArrayDeque<IrDeclaration>()
|
||||
val currentDeclaration: IrDeclaration
|
||||
get() = declarationsStack.peek()
|
||||
|
||||
irFunction.accept(object : IrElementVisitor<Unit, IrDeclarationParent> {
|
||||
val relevantVars = HashSet<IrVariable>()
|
||||
|
||||
override fun visitElement(element: IrElement) {
|
||||
element.acceptChildrenVoid(this)
|
||||
override fun visitElement(element: IrElement, data: IrDeclarationParent) {
|
||||
element.acceptChildren(this, data)
|
||||
}
|
||||
|
||||
override fun visitFunction(declaration: IrFunction) {
|
||||
declarationsStack.push(declaration)
|
||||
declaration.acceptChildrenVoid(this)
|
||||
declarationsStack.pop()
|
||||
}
|
||||
override fun visitDeclaration(declaration: IrDeclaration, data: IrDeclarationParent) =
|
||||
super.visitDeclaration(declaration, declaration as? IrDeclarationParent ?: data)
|
||||
|
||||
override fun visitVariable(declaration: IrVariable) {
|
||||
declaration.acceptChildrenVoid(this)
|
||||
override fun visitVariable(declaration: IrVariable, data: IrDeclarationParent) {
|
||||
declaration.acceptChildren(this, data)
|
||||
|
||||
if (declaration.isVar) {
|
||||
relevantVars.add(declaration)
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitVariableAccess(expression: IrValueAccessExpression) {
|
||||
expression.acceptChildrenVoid(this)
|
||||
override fun visitValueAccess(expression: IrValueAccessExpression, data: IrDeclarationParent) {
|
||||
expression.acceptChildren(this, data)
|
||||
|
||||
val value = expression.symbol.owner
|
||||
if (value in relevantVars && (value as IrVariable).parent != currentDeclaration) {
|
||||
if (value in relevantVars && (value as IrVariable).parent != data) {
|
||||
sharedVariables.add(value)
|
||||
}
|
||||
}
|
||||
})
|
||||
}, irFunction)
|
||||
}
|
||||
|
||||
private fun rewriteSharedVariables() {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
fun box() : String {
|
||||
var a = 1
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
fun box() : String {
|
||||
var a = 1
|
||||
|
||||
object {
|
||||
val t = run { a++ }
|
||||
}
|
||||
return if (a == 2) "OK" else "fail"
|
||||
}
|
||||
+5
@@ -15995,6 +15995,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2663_3.kt")
|
||||
public void testKt2663_3() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2675.kt")
|
||||
public void testKt2675() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2675.kt");
|
||||
|
||||
+5
@@ -15995,6 +15995,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2663_3.kt")
|
||||
public void testKt2663_3() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2675.kt")
|
||||
public void testKt2675() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2675.kt");
|
||||
|
||||
+5
@@ -16000,6 +16000,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2663_3.kt")
|
||||
public void testKt2663_3() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2675.kt")
|
||||
public void testKt2675() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2675.kt");
|
||||
|
||||
+5
@@ -13585,6 +13585,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2663_3.kt")
|
||||
public void testKt2663_3() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2675.kt")
|
||||
public void testKt2675() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2675.kt");
|
||||
|
||||
+5
@@ -14645,6 +14645,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2663_3.kt")
|
||||
public void testKt2663_3() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2663_3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2675.kt")
|
||||
public void testKt2675() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/objects/kt2675.kt");
|
||||
|
||||
Reference in New Issue
Block a user