[K2] Properly evaluate complex boolean constants

Some boolean expressions could be transformed into `IrWhen`
node. To understand that this node is actually
a boolean expression, we need to analyze its origin.

#KT-62683
This commit is contained in:
Ivan Kylchik
2023-11-14 12:49:18 +01:00
committed by Space Team
parent ede77ea29c
commit 3fa82c7bb1
19 changed files with 114 additions and 9 deletions
@@ -29654,6 +29654,12 @@ public class LLFirBlackBoxCodegenBasedTestGenerated extends AbstractLLFirBlackBo
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -29654,6 +29654,12 @@ public class LLFirReversedBlackBoxCodegenBasedTestGenerated extends AbstractLLFi
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -29307,6 +29307,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -29307,6 +29307,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -29307,6 +29307,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -59,6 +59,8 @@ enum class EvaluationMode {
BuiltInOperatorNames.ANDAND, BuiltInOperatorNames.OROR
).map { IrBuiltIns.KOTLIN_INTERNAL_IR_FQN.child(Name.identifier(it)).asString() }.toSet()
private val allowedOriginsForWhen = setOf(IrStatementOrigin.ANDAND, IrStatementOrigin.OROR)
override fun canEvaluateFunction(function: IrFunction): Boolean {
if (function.property.isConst) return true
@@ -80,13 +82,12 @@ enum class EvaluationMode {
override fun canEvaluateBlock(block: IrBlock): Boolean = block.statements.size == 1
override fun canEvaluateExpression(expression: IrExpression): Boolean {
if (expression !is IrCall) return false
if (expression.hasUnsignedArgs()) {
return expression.symbol.owner.fqNameWhenAvailable?.asString() == "kotlin.String.plus"
return when {
expression is IrWhen -> expression.origin in allowedOriginsForWhen
expression !is IrCall -> false
expression.hasUnsignedArgs() -> expression.symbol.owner.fqNameWhenAvailable?.asString() == "kotlin.String.plus"
else -> true
}
return true
}
private fun IrCall.hasUnsignedArgs(): Boolean {
@@ -0,0 +1,9 @@
// TARGET_BACKEND: JVM_IR
// TARGET_BACKEND: JS_IR
// TARGET_BACKEND: NATIVE
const val BOOL = <!EVALUATED("true")!>true<!>
const val BOOL_OR = <!EVALUATED("false")!>false && BOOL<!>
const val BOOL_AND = <!EVALUATED("true")!>true || BOOL<!>
const val BOOL_AND_OR = <!EVALUATED("true")!>true || false && BOOL<!>
fun box() = "OK"
@@ -76,11 +76,11 @@ fun testArithmeticOperations() {
fun testLogicOperations() {
js("{ var a = ${<!JSCODE_ARGUMENT_NON_CONST_EXPRESSION!>!true<!>}; }")
js("{ var a = ${<!JSCODE_ARGUMENT_NON_CONST_EXPRESSION!>true or false<!>}; }")
js(<!JSCODE_CAN_NOT_VERIFY_JAVASCRIPT!>"{ var a = ${true || false}; }"<!>)
js("{ var a = ${true || false}; }")
js("{ var a = ${<!JSCODE_ARGUMENT_NON_CONST_EXPRESSION!>true and false<!>}; }")
js(<!JSCODE_CAN_NOT_VERIFY_JAVASCRIPT!>"{ var a = ${true && false}; }"<!>)
js("{ var a = ${true && false}; }")
js(<!JSCODE_CAN_NOT_VERIFY_JAVASCRIPT!>"{ var a = ${TRUE && false}; }"<!>)
js("{ var a = ${TRUE && false}; }")
js("{ var a = ${TRUE or false}; }")
}
@@ -29307,6 +29307,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -29307,6 +29307,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -24770,6 +24770,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/doubleOperations.kt");
@@ -21699,6 +21699,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -21699,6 +21699,12 @@ public class FirJsES6CodegenBoxTestGenerated extends AbstractFirJsES6CodegenBoxT
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -21699,6 +21699,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -21699,6 +21699,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -24806,6 +24806,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -25294,6 +25294,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -24318,6 +24318,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {
@@ -24807,6 +24807,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
runTest("compiler/testData/codegen/box/involvesIrInterpreter/charOperations.kt");
}
@Test
@TestMetadata("complexBooleanConstant.kt")
public void testComplexBooleanConstant() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/complexBooleanConstant.kt");
}
@Test
@TestMetadata("doubleOperations.kt")
public void testDoubleOperations() throws Exception {