Avoid object's interpretation with nullable type

#KT-56215 Fixed
This commit is contained in:
Ivan Kylchik
2023-02-12 17:30:21 +01:00
committed by Space Team
parent 99448b77f3
commit 477d092bb8
15 changed files with 92 additions and 6 deletions
@@ -28259,6 +28259,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Test @Test
@TestMetadata("referenceNameFromStaticInDifferentModule.kt") @TestMetadata("referenceNameFromStaticInDifferentModule.kt")
public void testReferenceNameFromStaticInDifferentModule() throws Exception { public void testReferenceNameFromStaticInDifferentModule() throws Exception {
@@ -256,11 +256,11 @@ private fun unfoldSetField(expression: IrSetField, callStack: CallStack) {
} }
private fun unfoldGetValue(expression: IrGetValue, environment: IrInterpreterEnvironment) { private fun unfoldGetValue(expression: IrGetValue, environment: IrInterpreterEnvironment) {
if (expression.isAccessToObject()) { if (expression.isAccessToNotNullableObject()) {
// used to evaluate constants inside object // used to evaluate constants inside object
// TODO is this correct behaviour? // TODO is this correct behaviour?
val irGetObject = expression.type.classOrNull?.owner!!.createGetObject() val irGetObject = expression.type.classOrNull?.owner!!.createGetObject()
return unfoldGetObjectValue(irGetObject, environment) return unfoldGetObjectValue(irGetObject, environment) // if object already exists, it will be taken from `mapOfObjects`
} }
environment.callStack.pushState(environment.callStack.loadState(expression.symbol)) environment.callStack.pushState(environment.callStack.loadState(expression.symbol))
} }
@@ -294,10 +294,10 @@ internal fun IrClass.getSingleAbstractMethod(): IrFunction {
return declarations.filterIsInstance<IrSimpleFunction>().single { it.modality == Modality.ABSTRACT } return declarations.filterIsInstance<IrSimpleFunction>().single { it.modality == Modality.ABSTRACT }
} }
internal fun IrGetValue.isAccessToObject(): Boolean { internal fun IrGetValue.isAccessToNotNullableObject(): Boolean {
val owner = this.symbol.owner val owner = this.symbol.owner
val expectedClass = this.type.classOrNull?.owner val expectedClass = this.type.classOrNull?.owner
if (expectedClass == null || !expectedClass.isObject) return false if (expectedClass == null || !expectedClass.isObject || this.type.isNullable()) return false
return owner.origin == IrDeclarationOrigin.INSTANCE_RECEIVER || owner.name.asString() == "<this>" return owner.origin == IrDeclarationOrigin.INSTANCE_RECEIVER || owner.name.asString() == "<this>"
} }
@@ -11,7 +11,7 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.interpreter.accessesTopLevelOrObjectField import org.jetbrains.kotlin.ir.interpreter.accessesTopLevelOrObjectField
import org.jetbrains.kotlin.ir.interpreter.fqName import org.jetbrains.kotlin.ir.interpreter.fqName
import org.jetbrains.kotlin.ir.interpreter.isAccessToObject import org.jetbrains.kotlin.ir.interpreter.isAccessToNotNullableObject
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.constructors import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.statements import org.jetbrains.kotlin.ir.util.statements
@@ -160,7 +160,7 @@ class IrCompileTimeChecker(
} }
override fun visitGetValue(expression: IrGetValue, data: Nothing?): Boolean { override fun visitGetValue(expression: IrGetValue, data: Nothing?): Boolean {
return visitedStack.contains(expression.symbol.owner.parent) || expression.isAccessToObject() return visitedStack.contains(expression.symbol.owner.parent) || expression.isAccessToNotNullableObject()
} }
override fun visitSetValue(expression: IrSetValue, data: Nothing?): Boolean { override fun visitSetValue(expression: IrSetValue, data: Nothing?): Boolean {
@@ -0,0 +1,22 @@
object ObjectWithExtension
fun ObjectWithExtension?.nullableExtensionFun(): String =
if(this == null)
"Null"
else
"Not null"
fun ObjectWithExtension.extensionFun(): String =
if(this == null)
"Null" // unreachable branch, will be optimized by interpreter
else
"Not null"
fun box(): String {
val nullObjectWithExtension: ObjectWithExtension? = null
if ("${nullObjectWithExtension.nullableExtensionFun()}" != "Null") return "Fail 1"
if ("${ObjectWithExtension.nullableExtensionFun()}" != "Not null") return "Fail 2"
if ("${ObjectWithExtension.extensionFun()}" != "Not null") return "Fail 3"
return "OK"
}
@@ -27149,6 +27149,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -28259,6 +28259,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Test @Test
@TestMetadata("referenceNameFromStaticInDifferentModule.kt") @TestMetadata("referenceNameFromStaticInDifferentModule.kt")
public void testReferenceNameFromStaticInDifferentModule() throws Exception { public void testReferenceNameFromStaticInDifferentModule() throws Exception {
@@ -22887,6 +22887,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)
@@ -20977,6 +20977,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -20995,6 +20995,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -20995,6 +20995,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -20995,6 +20995,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -23954,6 +23954,12 @@ public class K2NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -23720,6 +23720,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@Test
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -18635,6 +18635,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt"); runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt55912.kt");
} }
@TestMetadata("kt56215.kt")
public void testKt56215() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/kt56215.kt");
}
@TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck") @TestMetadata("compiler/testData/codegen/box/involvesIrInterpreter/dumpIrAndCheck")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)