[Native] Always cast expression to the expected type after inline
Right now, during the process of inlining, the compiler erases types. Because of that, we can end up with some random type (for example, `Any`) where the concrete type was expected (for example, `Int`). Compiler must insert a cast in the required places. #KT-66017 Fixed
This commit is contained in:
+18
@@ -24667,12 +24667,30 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("innerInlineFunCapturesOuter.kt")
|
||||
public void testInnerInlineFunCapturesOuter() {
|
||||
runTest("compiler/testData/codegen/box/inline/innerInlineFunCapturesOuter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
|
||||
+18
@@ -24667,12 +24667,30 @@ public class FirLightTreeBlackBoxCodegenWithFir2IrFakeOverrideGeneratorTestGener
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("innerInlineFunCapturesOuter.kt")
|
||||
public void testInnerInlineFunCapturesOuter() {
|
||||
runTest("compiler/testData/codegen/box/inline/innerInlineFunCapturesOuter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
|
||||
+18
@@ -24667,12 +24667,30 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("innerInlineFunCapturesOuter.kt")
|
||||
public void testInnerInlineFunCapturesOuter() {
|
||||
runTest("compiler/testData/codegen/box/inline/innerInlineFunCapturesOuter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
|
||||
+15
-2
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.ir.types.isUnit
|
||||
import org.jetbrains.kotlin.ir.util.isSuspend
|
||||
import org.jetbrains.kotlin.ir.util.render
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
|
||||
data class TailSuspendCalls(val callSites: Set<IrCall>, val hasNotTailSuspendCalls: Boolean)
|
||||
|
||||
@@ -36,6 +37,14 @@ fun collectTailSuspendCalls(context: CommonBackendContext, irFunction: IrSimpleF
|
||||
element.acceptChildren(this, VisitorState(data.insideTryBlock, isTailExpression = false))
|
||||
}
|
||||
|
||||
override fun visitTypeOperator(expression: IrTypeOperatorCall, data: VisitorState) {
|
||||
if (expression.operator == IrTypeOperator.IMPLICIT_CAST) {
|
||||
expression.acceptChildren(this, data)
|
||||
} else {
|
||||
super.visitTypeOperator(expression, data)
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitTry(aTry: IrTry, data: VisitorState) {
|
||||
aTry.tryResult.accept(this, VisitorState(insideTryBlock = true, isTailExpression = false))
|
||||
aTry.catches.forEach { it.result.accept(this, data) }
|
||||
@@ -98,8 +107,12 @@ fun collectTailSuspendCalls(context: CommonBackendContext, irFunction: IrSimpleF
|
||||
expression.acceptChildren(this, VisitorState(data.insideTryBlock, isTailExpression))
|
||||
}
|
||||
|
||||
private fun IrExpression.isUnitRead(): Boolean =
|
||||
this is IrGetObjectValue && symbol == context.irBuiltIns.unitClass
|
||||
private fun IrExpression.isUnitRead(): Boolean {
|
||||
if (this is IrTypeOperatorCall) {
|
||||
return this.argument.isUnitRead()
|
||||
}
|
||||
return this is IrGetObjectValue && symbol == context.irBuiltIns.unitClass
|
||||
}
|
||||
|
||||
private fun IrCall.isReturnIfSuspendedCall() =
|
||||
symbol == context.ir.symbols.returnIfSuspended
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
class A<T>(val prop: T)
|
||||
|
||||
inline fun <T> A<T>.process(action: (T) -> Unit) {
|
||||
action(prop)
|
||||
}
|
||||
|
||||
inline fun acceptInt(p: Int, action: (Int) -> Unit) {
|
||||
action(p)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var x = 0
|
||||
A(1).process { acceptInt(it) { p -> x += p } }
|
||||
return ('N' + x).toString() + "K"
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
fun box(): String {
|
||||
listOf(1).forEach { size ->
|
||||
repeat(size) {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
return "Fail"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// WITH_STDLIB
|
||||
inline fun <T> Iterable<T>.myForEach(action: (T) -> Unit): Unit {
|
||||
for (element in this) action(element)
|
||||
}
|
||||
|
||||
inline fun myRepeat(times: Int, action: (Int) -> Unit) {
|
||||
for (index in 0 until times) {
|
||||
action(index)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
listOf(1).myForEach { size ->
|
||||
myRepeat(size) {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
return "Fail"
|
||||
}
|
||||
+18
@@ -24667,12 +24667,30 @@ public class JvmAbiConsistencyTestBoxGenerated extends AbstractJvmAbiConsistency
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("innerInlineFunCapturesOuter.kt")
|
||||
public void testInnerInlineFunCapturesOuter() {
|
||||
runTest("compiler/testData/codegen/box/inline/innerInlineFunCapturesOuter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
|
||||
+18
@@ -23431,6 +23431,24 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
|
||||
+18
@@ -24667,12 +24667,30 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("innerInlineFunCapturesOuter.kt")
|
||||
public void testInnerInlineFunCapturesOuter() {
|
||||
runTest("compiler/testData/codegen/box/inline/innerInlineFunCapturesOuter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
|
||||
+18
@@ -24667,12 +24667,30 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("innerInlineFunCapturesOuter.kt")
|
||||
public void testInnerInlineFunCapturesOuter() {
|
||||
runTest("compiler/testData/codegen/box/inline/innerInlineFunCapturesOuter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
|
||||
+18
@@ -24667,12 +24667,30 @@ public class FirBlackBoxCodegenTestWithInlineScopesGenerated extends AbstractFir
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("innerInlineFunCapturesOuter.kt")
|
||||
public void testInnerInlineFunCapturesOuter() {
|
||||
runTest("compiler/testData/codegen/box/inline/innerInlineFunCapturesOuter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
|
||||
+15
@@ -20683,11 +20683,26 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
runTest("compiler/testData/codegen/box/inline/inlineCtor.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inlinedLambdaWithErasedParamType.kt")
|
||||
public void testInlinedLambdaWithErasedParamType() {
|
||||
runTest("compiler/testData/codegen/box/inline/inlinedLambdaWithErasedParamType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("innerInlineFunCapturesOuter.kt")
|
||||
public void testInnerInlineFunCapturesOuter() {
|
||||
runTest("compiler/testData/codegen/box/inline/innerInlineFunCapturesOuter.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt66017.kt")
|
||||
public void testKt66017() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt66017_inlineFromTheSameModule.kt")
|
||||
public void testKt66017_inlineFromTheSameModule() {
|
||||
runTest("compiler/testData/codegen/box/inline/kt66017_inlineFromTheSameModule.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("lambdaAsAny.kt")
|
||||
public void testLambdaAsAny() {
|
||||
runTest("compiler/testData/codegen/box/inline/lambdaAsAny.kt");
|
||||
|
||||
Reference in New Issue
Block a user