[FIR] Forbid complex boolean expressions

Disabling the language feature is supported,
otherwise there are too many tests that
would need to be updated to account for
K2's inability to do it.

The `ifConstVal.kt` test is deleted,
because now it must also be ignored in K2,
and since it is ignored for both the
frontends, and the ignored backends
include all the target backends, this test
is basically unused.

Note that now both the frontends report
`CONST_VAL_WITH_NON_CONST_INITIALIZER`.
for `condition`.

^KT-55196 Fixed
This commit is contained in:
Nikolay Lunyak
2023-08-16 11:00:26 +03:00
committed by Space Team
parent 79fe48ab5b
commit 73b4a81663
26 changed files with 69 additions and 200 deletions
@@ -795,6 +795,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("nonConstNotCall.kt")
public void testNonConstNotCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/nonConstNotCall.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -795,6 +795,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("nonConstNotCall.kt")
public void testNonConstNotCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/nonConstNotCall.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -795,6 +795,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("nonConstNotCall.kt")
public void testNonConstNotCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/nonConstNotCall.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -795,6 +795,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("nonConstNotCall.kt")
public void testNonConstNotCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/nonConstNotCall.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.fir.analysis.checkers
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.hasAnnotation
import org.jetbrains.kotlin.fir.declarations.utils.isConst
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.fir.declarations.utils.isStatic
import org.jetbrains.kotlin.fir.declarations.utils.modality
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.impl.FirNoReceiverExpression
import org.jetbrains.kotlin.fir.languageVersionSettings
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.references.toResolvedCallableSymbol
@@ -24,6 +25,7 @@ import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.unwrapFakeOverrides
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.util.OperatorNameConventions
@@ -76,7 +78,7 @@ internal fun checkConstantArguments(
}
expression is FirStringConcatenationCall || expression is FirEqualityOperatorCall -> {
for (exp in (expression as FirCall).arguments) {
if (exp is FirResolvedQualifier) {
if (exp is FirResolvedQualifier || expression.isForbiddenComplexConstant(session)) {
return ConstantArgumentKind.NOT_CONST
}
checkConstantArguments(exp, session)?.let { return it }
@@ -132,7 +134,7 @@ internal fun checkConstantArguments(
if (calleeReference !is FirResolvedNamedReference) return ConstantArgumentKind.NOT_CONST
val symbol = calleeReference.resolvedSymbol as? FirNamedFunctionSymbol ?: return ConstantArgumentKind.NOT_CONST
if (!symbol.canBeEvaluated() && !expression.isCompileTimeBuiltinCall()) {
if (!symbol.canBeEvaluated() && !expression.isCompileTimeBuiltinCall() || expression.isForbiddenComplexConstant(session)) {
return ConstantArgumentKind.NOT_CONST
}
@@ -184,6 +186,30 @@ internal fun checkConstantArguments(
return null
}
private fun FirExpression.isForbiddenComplexConstant(session: FirSession): Boolean {
val forbidComplexBooleanExpressions = session.languageVersionSettings.supportsFeature(
LanguageFeature.ProhibitSimplificationOfNonTrivialConstBooleanExpressions
)
return isComplexBooleanConstant && forbidComplexBooleanExpressions
}
private val FirExpression.isComplexBooleanConstant
get(): Boolean = when {
!typeRef.coneType.isBoolean -> false
this is FirConstExpression<*> -> false
usesVariableAsConstant -> false
else -> true
}
/**
* See: org.jetbranis.kotlin.resolve.constants.CompileTimeConstant.Parameters.usesVariableAsConstant
*/
@Suppress("RecursivePropertyAccessor")
private val FirExpression.usesVariableAsConstant: Boolean
get() = this is FirPropertyAccessExpression && toResolvedCallableSymbol()?.isConst == true
|| this is FirQualifiedAccessExpression && explicitReceiver?.usesVariableAsConstant != false
|| this is FirCall && this.arguments.any { it.usesVariableAsConstant }
private val compileTimeFunctions = setOf(
*OperatorNameConventions.BINARY_OPERATION_NAMES.toTypedArray(), *OperatorNameConventions.UNARY_OPERATION_NAMES.toTypedArray(),
OperatorNameConventions.SHL, OperatorNameConventions.SHR, OperatorNameConventions.USHR,
@@ -28904,12 +28904,6 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -28904,12 +28904,6 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -1,24 +0,0 @@
// !LANGUAGE: +IntrinsicConstEvaluation
// TARGET_BACKEND: JVM_IR
// TARGET_BACKEND: JS_IR
// TARGET_BACKEND: NATIVE
// IGNORE_BACKEND_K1: JVM_IR, JS_IR, JS_IR_ES6, NATIVE
fun <T> T.id() = this
const val flag = <!EVALUATED("true")!>true<!>
const val value = <!EVALUATED("10")!>10<!>
const val condition = <!EVALUATED("True")!>if (flag) "True" else "Error"<!>
const val withWhen = <!EVALUATED("True")!>when (flag) { true -> "True"; else -> "Error" }<!>
const val withWhen2 = <!EVALUATED("True")!>when { flag == true -> "True"; else -> "Error" }<!>
const val withWhen3 = <!EVALUATED("1")!>when(value) { 10 -> "1"; 100 -> "2"; else -> "3" }<!>
const val multibranchIf = <!EVALUATED("3")!>if (value == 100) 1 else if (value == 1000) 2 else 3<!>
// STOP_EVALUATION_CHECKS
fun box(): String {
if (condition.id() != "True") return "Fail 1"
if (withWhen.id() != "True") return "Fail 2"
if (withWhen2.id() != "True") return "Fail 3"
if (withWhen3.id() != "1") return "Fail 4"
if (multibranchIf.id() != 3) return "Fail 5"
return "OK"
}
@@ -1,46 +0,0 @@
// !LANGUAGE: +IntrinsicConstEvaluation
const val equalsBoolean1 = true.equals(true)
const val equalsBoolean2 = false != true
const val equalsBoolean3 = false.equals(1)
const val equalsBoolean4 = <!EQUALITY_NOT_APPLICABLE!>false == 1<!>
const val equalsChar1 = '1'.equals('2')
const val equalsChar2 = '2' == '2'
const val equalsChar3 = '1'.equals(1)
const val equalsChar4 = <!EQUALITY_NOT_APPLICABLE!>'1' == 1<!>
const val equalsByte1 = 1.toByte().equals(2.toByte())
const val equalsByte2 = 2.toByte() == 2.toByte()
const val equalsByte3 = 1.toByte().equals("1")
const val equalsByte4 = <!EQUALITY_NOT_APPLICABLE!>1.toByte() == "1"<!>
const val equalsShort1 = 1.toShort().equals(2.toShort())
const val equalsShort2 = 2.toShort() == 2.toShort()
const val equalsShort3 = 1.toShort().equals("1")
const val equalsShort4 = <!EQUALITY_NOT_APPLICABLE!>1.toShort() == "1"<!>
const val equalsInt1 = 1.equals(2)
const val equalsInt2 = 2 == 2
const val equalsInt3 = 1.equals("1")
const val equalsInt4 = <!EQUALITY_NOT_APPLICABLE!>1 == "1"<!>
const val equalsLong1 = 1L.equals(2L)
const val equalsLong2 = 2L == 2L
const val equalsLong3 = 1L.equals("1")
const val equalsLong4 = <!EQUALITY_NOT_APPLICABLE!>1L == "1"<!>
const val equalsFloat1 = 1.0f.equals(2.0f)
const val equalsFloat2 = 2.0f == 2.0f
const val equalsFloat3 = 1.0f.equals("1")
const val equalsFloat4 = <!EQUALITY_NOT_APPLICABLE!>1.0f == "1"<!>
const val equalsDoable1 = 1.0.equals(2.0)
const val equalsDoable2 = 2.0 == 2.0
const val equalsDoable3 = 1.0.equals("1")
const val equalsDoable4 = <!EQUALITY_NOT_APPLICABLE!>1.0 == "1"<!>
const val equalsString1 = "someStr".equals("123")
const val equalsString2 = "someStr" == "otherStr"
const val equalsString3 = "someStr".equals(1)
const val equalsString4 = <!EQUALITY_NOT_APPLICABLE!>"someStr" == 1<!>
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +IntrinsicConstEvaluation
const val equalsBoolean1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>true.equals(true)<!>
@@ -1,46 +0,0 @@
// !LANGUAGE: -IntrinsicConstEvaluation
const val equalsBoolean1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>true.equals(true)<!>
const val equalsBoolean2 = false != true
const val equalsBoolean3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>false.equals(1)<!>
const val equalsBoolean4 = <!EQUALITY_NOT_APPLICABLE!>false == 1<!>
const val equalsChar1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>'1'.equals('2')<!>
const val equalsChar2 = '2' == '2'
const val equalsChar3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>'1'.equals(1)<!>
const val equalsChar4 = <!EQUALITY_NOT_APPLICABLE!>'1' == 1<!>
const val equalsByte1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.toByte().equals(2.toByte())<!>
const val equalsByte2 = 2.toByte() == 2.toByte()
const val equalsByte3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.toByte().equals("1")<!>
const val equalsByte4 = <!EQUALITY_NOT_APPLICABLE!>1.toByte() == "1"<!>
const val equalsShort1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.toShort().equals(2.toShort())<!>
const val equalsShort2 = 2.toShort() == 2.toShort()
const val equalsShort3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.toShort().equals("1")<!>
const val equalsShort4 = <!EQUALITY_NOT_APPLICABLE!>1.toShort() == "1"<!>
const val equalsInt1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.equals(2)<!>
const val equalsInt2 = 2 == 2
const val equalsInt3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.equals("1")<!>
const val equalsInt4 = <!EQUALITY_NOT_APPLICABLE!>1 == "1"<!>
const val equalsLong1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1L.equals(2L)<!>
const val equalsLong2 = 2L == 2L
const val equalsLong3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1L.equals("1")<!>
const val equalsLong4 = <!EQUALITY_NOT_APPLICABLE!>1L == "1"<!>
const val equalsFloat1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.0f.equals(2.0f)<!>
const val equalsFloat2 = 2.0f == 2.0f
const val equalsFloat3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.0f.equals("1")<!>
const val equalsFloat4 = <!EQUALITY_NOT_APPLICABLE!>1.0f == "1"<!>
const val equalsDoable1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.0.equals(2.0)<!>
const val equalsDoable2 = 2.0 == 2.0
const val equalsDoable3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>1.0.equals("1")<!>
const val equalsDoable4 = <!EQUALITY_NOT_APPLICABLE!>1.0 == "1"<!>
const val equalsString1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>"someStr".equals("123")<!>
const val equalsString2 = "someStr" == "otherStr"
const val equalsString3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>"someStr".equals(1)<!>
const val equalsString4 = <!EQUALITY_NOT_APPLICABLE!>"someStr" == 1<!>
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -IntrinsicConstEvaluation
const val equalsBoolean1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>true.equals(true)<!>
@@ -4,19 +4,19 @@ const val flag = true
const val value = 10
const val condition = if (flag) "True" else "Error"
const val withWhen = when (flag) {
const val withWhen = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>when (flag) {
true -> "True"
else -> "Error"
}
}<!>
const val withWhen2 = when {
flag == true -> "True"
else -> "Error"
}
const val withWhen3 = when(value) {
const val withWhen3 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>when(value) {
10 -> "1"
100 -> "2"
else -> "3"
}
}<!>
const val multibranchIf = if (value == 100) 1 else if (value == 1000) 2 else 3
val nonConstFlag = true
@@ -0,0 +1,4 @@
// FIR_IDENTICAL
// ISSUE: KT-55196
const val bb: Boolean = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>!false<!> // Red in K1, green in K2
@@ -795,6 +795,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("nonConstNotCall.kt")
public void testNonConstNotCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/nonConstNotCall.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -28904,12 +28904,6 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -28904,12 +28904,6 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -24383,11 +24383,6 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/enumName.kt");
}
@TestMetadata("ifConstVal.kt")
public void ignoreIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@TestMetadata("kCallableName.kt")
public void ignoreKCallableName() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/kCallableName.kt");
@@ -21410,12 +21410,6 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -21410,12 +21410,6 @@ public class FirJsES6CodegenBoxTestGenerated extends AbstractFirJsES6CodegenBoxT
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -21410,12 +21410,6 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -21410,12 +21410,6 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -24235,12 +24235,6 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -24711,12 +24711,6 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -23998,12 +23998,6 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {
@@ -24236,12 +24236,6 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/equals_after.kt");
}
@Test
@TestMetadata("ifConstVal.kt")
public void testIfConstVal() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intrinsicConst/ifConstVal.kt");
}
@Test
@TestMetadata("kCallableName.kt")
public void testKCallableName() throws Exception {