Skip to content

Commit

Permalink
[IR][K/N] Swapped autoboxing & generic calls return type eraser
Browse files Browse the repository at this point in the history
... to make autoboxing a bit simpler

 #KT-71425
  • Loading branch information
homuroll authored and qodana-bot committed Nov 22, 2024
1 parent b471a51 commit 749dcf7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.isNullable
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.util.resolveFakeOverrideMaybeAbstract
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver

Expand Down Expand Up @@ -168,13 +170,15 @@ internal class CheckIrElementVisitor(

// TODO: Why don't we check parameters as well?

val returnType = expression.symbol.owner.returnType
val callee = expression.symbol.owner
// TODO: We don't have the proper type substitution yet, so skip generics for now.
val actualCallee = callee.resolveFakeOverrideMaybeAbstract { it.returnType.classifierOrNull !is IrTypeParameterSymbol } ?: callee
val returnType = actualCallee.returnType
if (returnType is IrSimpleType &&
returnType.classifier is IrClassSymbol &&
returnType.arguments.isEmpty()
) {
expression.ensureTypeIs(returnType)
expression.ensureTypeIs(callee.returnType)
}

expression.superQualifierSymbol?.ensureBound(expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,15 @@ private val bridgesPhase = createFileLoweringPhase(
prerequisite = setOf(coroutinesPhase)
)

private val autoboxPhase = createFileLoweringPhase(
::Autoboxing,
name = "Autobox",
prerequisite = setOf(bridgesPhase, coroutinesPhase)
)

private val eraseGenericCallsReturnTypesPhase = createFileLoweringPhase(
name = "EraseGenericCallsReturnTypesPhase",
lowering = ::GenericCallsReturnTypeEraser,
prerequisite = setOf(autoboxPhase)
)

private val autoboxPhase = createFileLoweringPhase(
::Autoboxing,
name = "Autobox",
prerequisite = setOf(bridgesPhase, coroutinesPhase, eraseGenericCallsReturnTypesPhase)
)

private val constructorsLoweringPhase = createFileLoweringPhase(
Expand Down Expand Up @@ -618,8 +617,8 @@ internal fun KonanConfig.getLoweringsAfterInlining(): LoweringList = listOfNotNu
bridgesPhase,
exportInternalAbiPhase.takeIf { this.produce.isCache },
useInternalAbiPhase,
autoboxPhase,
eraseGenericCallsReturnTypesPhase,
autoboxPhase,
constructorsLoweringPhase,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ private class AutoboxingTransformer(val context: Context) : AbstractValueUsageTr
val skipTypeCheck = forceSkipTypeCheck || !insertSafeCasts || (this as? IrTypeOperatorCall)?.operator == IrTypeOperator.CAST
val actualType = when (this) {
is IrGetField -> this.symbol.owner.type
is IrCall -> when (this.symbol) {
symbols.reinterpret -> this.getTypeArgument(1)!!
else -> this.target.returnType
}
is IrTypeOperatorCall -> when (this.operator) {
IrTypeOperator.CAST -> context.irBuiltIns.anyNType
IrTypeOperator.IMPLICIT_CAST -> if (insertSafeCasts) this.type else context.irBuiltIns.anyNType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
package org.jetbrains.kotlin.backend.konan.lower

import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.lower.at
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.irImplicitCoercionToUnit
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.builders.irImplicitCast
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.types.isNothing
import org.jetbrains.kotlin.ir.types.isUnit
import org.jetbrains.kotlin.ir.util.target
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid

/*
* FE substitutes types for generic calls, but by the end of the lowerings pipeline, the return types should be
Expand All @@ -27,20 +33,27 @@ internal class GenericCallsReturnTypeEraser(val context: Context) : BodyLowering
private val reinterpret = context.ir.symbols.reinterpret.owner

override fun lower(irBody: IrBody, container: IrDeclaration) {
irBody.acceptChildrenVoid(object : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}

override fun visitCall(expression: IrCall) {
expression.acceptChildrenVoid(this)
val irBuilder = context.createIrBuilder(container.symbol)
irBody.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitCall(expression: IrCall): IrExpression {
expression.transformChildrenVoid(this)

val callee = expression.target
if (callee != reinterpret && callee.returnType.classifierOrNull is IrTypeParameterSymbol) {
expression.type = callee.returnType.erasure()
val actualType = callee.returnType.erasure()
val expectedType = expression.type
if (actualType != expectedType) {
expression.type = actualType
return when {
expectedType.isUnit() -> irBuilder.at(expression).irImplicitCoercionToUnit(expression)
expectedType.isNothing() -> expression
else -> irBuilder.at(expression).irImplicitCast(expression, expectedType)
}
}
}

return expression
}
})
}

}

0 comments on commit 749dcf7

Please sign in to comment.