KT-52743 IR: Fix null checks in Elvis operators

This commit is contained in:
Pavel Mikhailovskii
2022-06-20 18:51:46 +02:00
committed by teamcity
parent ed97e73129
commit 3766698081
11 changed files with 60 additions and 2 deletions
@@ -44747,6 +44747,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/safeCall/kt52580.kt");
}
@Test
@TestMetadata("kt52743.kt")
public void testKt52743() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/kt52743.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.ir.util.isTrivial
import org.jetbrains.kotlin.ir.util.isConstantLike
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
@@ -121,7 +121,7 @@ class JvmSafeCallChainFoldingLowering(val context: JvmBackendContext) : FileLowe
IrConstImpl.boolean(startOffset, endOffset, context.irBuiltIns.booleanType, false)
private fun irValNotNull(startOffset: Int, endOffset: Int, irVariable: IrVariable): IrExpression =
if (irVariable.type.isJvmNullable() || irVariable.initializer?.isTrivial() != true)
if (irVariable.type.isJvmNullable() || irVariable.initializer?.isConstantLike != true)
IrGetValueImpl(startOffset, endOffset, irVariable.symbol).irEqEqNull().irNot()
else
irTrue(startOffset, endOffset)
@@ -596,6 +596,10 @@ fun IrExpression.isTrivial() =
this is IrGetObjectValue ||
this is IrErrorExpressionImpl
val IrExpression.isConstantLike: Boolean
get() = this is IrConst<*> || this is IrGetSingletonValue
|| this is IrGetValue && this.symbol.owner.origin == IrDeclarationOrigin.INSTANCE_RECEIVER
fun IrExpression.shallowCopy(): IrExpression =
shallowCopyOrNull()
?: error("Not a copyable expression: ${render()}")
+8
View File
@@ -0,0 +1,8 @@
fun <T: Any?> nullableFun(): T {
return null as T
}
fun box(): String {
val t = nullableFun<String>()
return if (t?.length == null) "OK" else "Fail"
}
@@ -44177,6 +44177,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/safeCall/kt52580.kt");
}
@Test
@TestMetadata("kt52743.kt")
public void testKt52743() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/kt52743.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -44747,6 +44747,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/safeCall/kt52580.kt");
}
@Test
@TestMetadata("kt52743.kt")
public void testKt52743() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/kt52743.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -35687,6 +35687,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/safeCall/kt52580.kt");
}
@TestMetadata("kt52743.kt")
public void testKt52743() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/kt52743.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/primitive.kt");
@@ -32491,6 +32491,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/safeCall/kt52580.kt");
}
@Test
@TestMetadata("kt52743.kt")
public void testKt52743() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/kt52743.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -32593,6 +32593,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/safeCall/kt52580.kt");
}
@Test
@TestMetadata("kt52743.kt")
public void testKt52743() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/kt52743.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -29161,6 +29161,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/safeCall/kt52580.kt");
}
@TestMetadata("kt52743.kt")
public void testKt52743() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/kt52743.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/primitive.kt");
@@ -35586,6 +35586,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/safeCall/kt52580.kt");
}
@Test
@TestMetadata("kt52743.kt")
public void testKt52743() throws Exception {
runTest("compiler/testData/codegen/box/safeCall/kt52743.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {