diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index f8bbe66e1d1..4905c5594ec 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -23951,6 +23951,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/modifiers/const/arrayInAnnotationArgumentType.kt"); } + @Test + @TestMetadata("compare.kt") + public void testCompare() throws Exception { + runTest("compiler/testData/diagnostics/tests/modifiers/const/compare.kt"); + } + @Test @TestMetadata("constInteraction.kt") public void testConstInteraction() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index 22374ead0f4..a1569d8afa6 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -23951,6 +23951,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/modifiers/const/arrayInAnnotationArgumentType.kt"); } + @Test + @TestMetadata("compare.kt") + public void testCompare() throws Exception { + runTest("compiler/testData/diagnostics/tests/modifiers/const/compare.kt"); + } + @Test @TestMetadata("constInteraction.kt") public void testConstInteraction() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index e613dbd9b36..e4a44d5dba3 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -23945,6 +23945,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/modifiers/const/arrayInAnnotationArgumentType.kt"); } + @Test + @TestMetadata("compare.kt") + public void testCompare() throws Exception { + runTest("compiler/testData/diagnostics/tests/modifiers/const/compare.kt"); + } + @Test @TestMetadata("constInteraction.kt") public void testConstInteraction() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index e3685852d5c..7ab64957205 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -23951,6 +23951,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/modifiers/const/arrayInAnnotationArgumentType.kt"); } + @Test + @TestMetadata("compare.kt") + public void testCompare() throws Exception { + runTest("compiler/testData/diagnostics/tests/modifiers/const/compare.kt"); + } + @Test @TestMetadata("constInteraction.kt") public void testConstInteraction() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirConstChecks.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirConstChecks.kt index b6cd2b8a541..a424e79b07a 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirConstChecks.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirConstChecks.kt @@ -96,10 +96,6 @@ internal fun checkConstantArguments( return ConstantArgumentKind.NOT_CONST } - if (expression.isForbiddenComplexConstant(session)) { - return ConstantArgumentKind.NOT_CONST - } - for (exp in (expression as FirCall).arguments) { if (exp is FirResolvedQualifier || exp is FirGetClassCall || exp.getExpandedType().isUnsignedType) { return ConstantArgumentKind.NOT_CONST @@ -162,7 +158,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(session) || expression.isForbiddenComplexConstant(session)) { + if (!symbol.canBeEvaluated() && !expression.isCompileTimeBuiltinCall(session)) { return ConstantArgumentKind.NOT_CONST } @@ -214,34 +210,6 @@ internal fun checkConstantArguments( return null } -private fun FirExpression.isForbiddenComplexConstant(session: FirSession): Boolean { - val forbidComplexBooleanExpressions = session.languageVersionSettings.supportsFeature( - LanguageFeature.ProhibitSimplificationOfNonTrivialConstBooleanExpressions - ) - val intrinsicConstEvaluation = session.languageVersionSettings.supportsFeature( - LanguageFeature.IntrinsicConstEvaluation - ) - return !intrinsicConstEvaluation && forbidComplexBooleanExpressions && isComplexBooleanConstant(session) -} - -private fun FirExpression.isComplexBooleanConstant(session: FirSession): Boolean { - return when { - !resolvedType.fullyExpandedType(session).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, diff --git a/compiler/testData/diagnostics/tests/modifiers/const/compare.fir.kt b/compiler/testData/diagnostics/tests/modifiers/const/compare.fir.kt new file mode 100644 index 00000000000..9c6ae4acc71 --- /dev/null +++ b/compiler/testData/diagnostics/tests/modifiers/const/compare.fir.kt @@ -0,0 +1,22 @@ +const val INT = 0 +const val LONG = 0L + +const val compareInt1 = 1 < 2 +const val compareInt2 = 1 <= 3 +const val compareInt3 = 1 > 4 +const val compareInt4 = 1 >= 5 + +const val compareInt5 = INT < 2 +const val compareInt6 = INT <= 3 +const val compareInt7 = INT > 4 +const val compareInt8 = INT >= 5 + +const val compareLong1 = 1L < 2L +const val compareLong2 = 1L <= 3L +const val compareLong3 = 1L > 4L +const val compareLong4 = 1L >= 5L + +const val compareLong5 = LONG < 2L +const val compareLong6 = LONG <= 3L +const val compareLong7 = LONG > 4L +const val compareLong8 = LONG >= 5L diff --git a/compiler/testData/diagnostics/tests/modifiers/const/compare.kt b/compiler/testData/diagnostics/tests/modifiers/const/compare.kt new file mode 100644 index 00000000000..18db23b7067 --- /dev/null +++ b/compiler/testData/diagnostics/tests/modifiers/const/compare.kt @@ -0,0 +1,22 @@ +const val INT = 0 +const val LONG = 0L + +const val compareInt1 = 1 < 2 +const val compareInt2 = 1 <= 3 +const val compareInt3 = 1 > 4 +const val compareInt4 = 1 >= 5 + +const val compareInt5 = INT < 2 +const val compareInt6 = INT <= 3 +const val compareInt7 = INT > 4 +const val compareInt8 = INT >= 5 + +const val compareLong1 = 1L < 2L +const val compareLong2 = 1L <= 3L +const val compareLong3 = 1L > 4L +const val compareLong4 = 1L >= 5L + +const val compareLong5 = LONG < 2L +const val compareLong6 = LONG <= 3L +const val compareLong7 = LONG > 4L +const val compareLong8 = LONG >= 5L diff --git a/compiler/testData/diagnostics/tests/modifiers/const/equals_before.fir.kt b/compiler/testData/diagnostics/tests/modifiers/const/equals_before.fir.kt new file mode 100644 index 00000000000..b4da50686ba --- /dev/null +++ b/compiler/testData/diagnostics/tests/modifiers/const/equals_before.fir.kt @@ -0,0 +1,46 @@ +// !LANGUAGE: -IntrinsicConstEvaluation + +const val equalsBoolean1 = true.equals(true) +const val equalsBoolean2 = false != true +const val equalsBoolean3 = false.equals(1) +const val equalsBoolean4 = false == 1 + +const val equalsChar1 = '1'.equals('2') +const val equalsChar2 = '2' == '2' +const val equalsChar3 = '1'.equals(1) +const val equalsChar4 = '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 = 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 = 1.toShort() == "1" + +const val equalsInt1 = 1.equals(2) +const val equalsInt2 = 2 == 2 +const val equalsInt3 = 1.equals("1") +const val equalsInt4 = 1 == "1" + +const val equalsLong1 = 1L.equals(2L) +const val equalsLong2 = 2L == 2L +const val equalsLong3 = 1L.equals("1") +const val equalsLong4 = 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 = 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 = 1.0 == "1" + +const val equalsString1 = "someStr".equals("123") +const val equalsString2 = "someStr" == "otherStr" +const val equalsString3 = "someStr".equals(1) +const val equalsString4 = "someStr" == 1 diff --git a/compiler/testData/diagnostics/tests/modifiers/const/equals_before.kt b/compiler/testData/diagnostics/tests/modifiers/const/equals_before.kt index 15ae47b00aa..00bfdc2f401 100644 --- a/compiler/testData/diagnostics/tests/modifiers/const/equals_before.kt +++ b/compiler/testData/diagnostics/tests/modifiers/const/equals_before.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !LANGUAGE: -IntrinsicConstEvaluation const val equalsBoolean1 = true.equals(true) diff --git a/compiler/testData/diagnostics/tests/nonConstNotCall.fir.kt b/compiler/testData/diagnostics/tests/nonConstNotCall.fir.kt new file mode 100644 index 00000000000..dd85a62e056 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nonConstNotCall.fir.kt @@ -0,0 +1,3 @@ +// ISSUE: KT-55196 + +const val bb: Boolean = !false // Red in K1, green in K2 diff --git a/compiler/testData/diagnostics/tests/nonConstNotCall.kt b/compiler/testData/diagnostics/tests/nonConstNotCall.kt index f3ef5cdffcb..2e8a76bafb0 100644 --- a/compiler/testData/diagnostics/tests/nonConstNotCall.kt +++ b/compiler/testData/diagnostics/tests/nonConstNotCall.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // ISSUE: KT-55196 const val bb: Boolean = !false // Red in K1, green in K2 diff --git a/compiler/testData/diagnostics/testsWithJsStdLibAndBackendCompilation/jsCode/compileTimeStringWithIntrinConstCall.fir.kt b/compiler/testData/diagnostics/testsWithJsStdLibAndBackendCompilation/jsCode/compileTimeStringWithIntrinConstCall.fir.kt index f64df4e42b0..b9c07d8a191 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLibAndBackendCompilation/jsCode/compileTimeStringWithIntrinConstCall.fir.kt +++ b/compiler/testData/diagnostics/testsWithJsStdLibAndBackendCompilation/jsCode/compileTimeStringWithIntrinConstCall.fir.kt @@ -74,10 +74,10 @@ fun testArithmeticOperations() { } fun testLogicOperations() { - js("{ var a = ${!true}; }") - js("{ var a = ${true or false}; }") + js("{ var a = ${!true}; }") + js("{ var a = ${true or false}; }") js("{ var a = ${true || false}; }") - js("{ var a = ${true and false}; }") + js("{ var a = ${true and false}; }") js("{ var a = ${true && false}; }") js("{ var a = ${TRUE && false}; }") @@ -85,14 +85,14 @@ fun testLogicOperations() { } fun testEq() { - js("{ var a = ${1 == 1}; }") + js("{ var a = ${1 == 1}; }") js("{ var a = ${1U == 1U}; }") js("{ var a = ${UONE == 1U}; }") js("{ var a = ${"FOO" == STR}; }") js("{ var a = ${TRUE == null}; }") js("{ var a = ${STR == null}; }") - js("{ var a = ${1 != 1}; }") + js("{ var a = ${1 != 1}; }") js("{ var a = ${1U != 1U}; }") js("{ var a = ${UONE != 1U}; }") js("{ var a = ${"FOO" != STR}; }") diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index b69fd059d55..ed6d5c5b65d 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -23951,6 +23951,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/modifiers/const/arrayInAnnotationArgumentType.kt"); } + @Test + @TestMetadata("compare.kt") + public void testCompare() throws Exception { + runTest("compiler/testData/diagnostics/tests/modifiers/const/compare.kt"); + } + @Test @TestMetadata("constInteraction.kt") public void testConstInteraction() throws Exception {