From 93d1932ccbb107acb146ad23999fc0fe9f79a72f Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Fri, 5 May 2023 13:49:50 +0200 Subject: [PATCH] [IR] Make IrReturnTargetSymbol a sealed interface --- .../lower/AbstractValueUsageTransformer.kt | 1 - .../backend/js/lower/AutoboxingTransformer.kt | 1 - .../IrElementToJsStatementTransformer.kt | 21 +++++++++++-------- .../jetbrains/kotlin/ir/symbols/IrSymbol.kt | 2 +- .../ir/util/DeepCopyIrTreeWithSymbols.kt | 1 - .../common/AbstractValueUsageTransformer.kt | 1 - .../kotlin/backend/konan/lower/Autoboxing.kt | 1 - 7 files changed, 13 insertions(+), 15 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractValueUsageTransformer.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractValueUsageTransformer.kt index 76405a0e32b..e2fa62bf635 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractValueUsageTransformer.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractValueUsageTransformer.kt @@ -72,7 +72,6 @@ abstract class AbstractValueUsageTransformer( is IrSimpleFunctionSymbol -> this.useAs(returnTarget.owner.returnType) is IrConstructorSymbol -> this.useAs(irBuiltIns.unitType) is IrReturnableBlockSymbol -> this.useAs(returnTarget.owner.type) - else -> error(returnTarget) } protected open fun IrExpression.useAsResult(enclosing: IrExpression): IrExpression = diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt index 55de84c67f4..fffbb81b88f 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/AutoboxingTransformer.kt @@ -149,7 +149,6 @@ class AutoboxingTransformer(context: JsCommonBackendContext) : AbstractValueUsag is IrSimpleFunctionSymbol -> useReturnableExpressionAsType(returnTarget.owner.returnType) is IrConstructorSymbol -> useReturnableExpressionAsType(irBuiltIns.unitType) is IrReturnableBlockSymbol -> useReturnableExpressionAsType(returnTarget.owner.type) - else -> error(returnTarget) } override fun IrExpression.useExpressionAsType(actualType: IrType, expectedType: IrType): IrExpression { diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrElementToJsStatementTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrElementToJsStatementTransformer.kt index ac94b239c9f..ec3e3145c3e 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrElementToJsStatementTransformer.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrElementToJsStatementTransformer.kt @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol import org.jetbrains.kotlin.ir.types.isAny import org.jetbrains.kotlin.ir.util.constructedClassType @@ -125,17 +126,19 @@ class IrElementToJsStatementTransformer : BaseIrElementToJsNodeTransformer JsExpression) -> JsStatement = - if (targetSymbol is IrReturnableBlockSymbol) { - // TODO assert that value is Unit? - { - context.getNameForReturnableBlock(targetSymbol.owner) - .takeIf { !expression.isTheLastReturnStatementIn(targetSymbol) } - ?.run { JsBreak(makeRef()) } ?: JsEmpty + when (val targetSymbol = expression.returnTargetSymbol) { + is IrReturnableBlockSymbol -> { + // TODO assert that value is Unit? + { + context.getNameForReturnableBlock(targetSymbol.owner) + .takeIf { !expression.isTheLastReturnStatementIn(targetSymbol) } + ?.run { JsBreak(makeRef()) } ?: JsEmpty + } + } + is IrFunctionSymbol -> { + { JsReturn(it()) } } - } else { - { JsReturn(it()) } } return expression.value.maybeOptimizeIntoSwitch(context, lastStatementTransformer).withSource(expression, context) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/symbols/IrSymbol.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/symbols/IrSymbol.kt index e563e2527bb..b8fd65c4017 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/symbols/IrSymbol.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/symbols/IrSymbol.kt @@ -91,7 +91,7 @@ interface IrValueParameterSymbol : IrValueSymbol, IrBindableSymbol -interface IrReturnTargetSymbol : IrSymbol { +sealed interface IrReturnTargetSymbol : IrSymbol { @ObsoleteDescriptorBasedAPI override val descriptor: FunctionDescriptor diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt index 67978e87aa5..bf9e7863623 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt @@ -806,7 +806,6 @@ open class DeepCopyIrTreeWithSymbols( when (returnTarget) { is IrFunctionSymbol -> getReferencedFunction(returnTarget) is IrReturnableBlockSymbol -> getReferencedReturnableBlock(returnTarget) - else -> throw AssertionError("Unexpected return target: ${returnTarget.javaClass} $returnTarget") } override fun visitThrow(expression: IrThrow): IrThrow = diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt index 3cbebaf52a7..1f42d400fdd 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt @@ -75,7 +75,6 @@ internal abstract class AbstractValueUsageTransformer( is IrSimpleFunctionSymbol -> this.useAs(returnTarget.owner.returnType) is IrConstructorSymbol -> this.useAs(irBuiltIns.unitType) is IrReturnableBlockSymbol -> this.useAs(returnTarget.owner.type) - else -> error(returnTarget) } protected open fun IrExpression.useAsResult(enclosing: IrExpression): IrExpression = diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/Autoboxing.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/Autoboxing.kt index 904b5f1c1e4..a42448daaf0 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/Autoboxing.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/Autoboxing.kt @@ -79,7 +79,6 @@ private class AutoboxingTransformer(val context: Context) : AbstractValueUsageTr is IrSimpleFunctionSymbol -> this.useAs(returnTarget.owner.returnType) is IrConstructorSymbol -> this.useAs(irBuiltIns.unitType) is IrReturnableBlockSymbol -> this.useAs(returnTarget.owner.type) - else -> error(returnTarget) } override fun IrExpression.useAs(type: IrType): IrExpression {