[K2] Forbid to use "get class" in the context of string concat and equality

#KT-63941 Fixed
This commit is contained in:
Ivan Kylchik
2023-12-01 17:59:30 +01:00
committed by Space Team
parent 8a8eb18efd
commit 46cb108a23
8 changed files with 88 additions and 2 deletions
@@ -23693,6 +23693,18 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/modifiers/const/kCallable_before.kt");
}
@Test
@TestMetadata("kClassInEquals.kt")
public void testKClassInEquals() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInEquals.kt");
}
@Test
@TestMetadata("kClassInStringConcatenation.kt")
public void testKClassInStringConcatenation() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInStringConcatenation.kt");
}
@Test
@TestMetadata("kotlinJavaCycle.kt")
public void testKotlinJavaCycle() throws Exception {
@@ -23693,6 +23693,18 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/modifiers/const/kCallable_before.kt");
}
@Test
@TestMetadata("kClassInEquals.kt")
public void testKClassInEquals() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInEquals.kt");
}
@Test
@TestMetadata("kClassInStringConcatenation.kt")
public void testKClassInStringConcatenation() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInStringConcatenation.kt");
}
@Test
@TestMetadata("kotlinJavaCycle.kt")
public void testKotlinJavaCycle() throws Exception {
@@ -23687,6 +23687,18 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/modifiers/const/kCallable_before.kt");
}
@Test
@TestMetadata("kClassInEquals.kt")
public void testKClassInEquals() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInEquals.kt");
}
@Test
@TestMetadata("kClassInStringConcatenation.kt")
public void testKClassInStringConcatenation() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInStringConcatenation.kt");
}
@Test
@TestMetadata("kotlinJavaCycle.kt")
public void testKotlinJavaCycle() throws Exception {
@@ -23693,6 +23693,18 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/modifiers/const/kCallable_before.kt");
}
@Test
@TestMetadata("kClassInEquals.kt")
public void testKClassInEquals() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInEquals.kt");
}
@Test
@TestMetadata("kClassInStringConcatenation.kt")
public void testKClassInStringConcatenation() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInStringConcatenation.kt");
}
@Test
@TestMetadata("kotlinJavaCycle.kt")
public void testKotlinJavaCycle() throws Exception {
@@ -85,7 +85,7 @@ internal fun checkConstantArguments(
}
expression is FirStringConcatenationCall -> {
for (exp in (expression as FirCall).arguments) {
if (exp is FirResolvedQualifier || expression.isForbiddenComplexConstant(session)) {
if (exp is FirResolvedQualifier || exp is FirGetClassCall) {
return ConstantArgumentKind.NOT_CONST
}
checkConstantArguments(exp, session)?.let { return it }
@@ -95,8 +95,13 @@ internal fun checkConstantArguments(
if (expression.operation == FirOperation.IDENTITY || expression.operation == FirOperation.NOT_IDENTITY) {
return ConstantArgumentKind.NOT_CONST
}
if (expression.isForbiddenComplexConstant(session)) {
return ConstantArgumentKind.NOT_CONST
}
for (exp in (expression as FirCall).arguments) {
if (exp is FirResolvedQualifier || expression.isForbiddenComplexConstant(session) || exp.getExpandedType().isUnsignedType) {
if (exp is FirResolvedQualifier || exp is FirGetClassCall || exp.getExpandedType().isUnsignedType) {
return ConstantArgumentKind.NOT_CONST
}
checkConstantArguments(exp, session)?.let { return it }
@@ -0,0 +1,10 @@
// FIR_IDENTICAL
annotation class Anno(val equal: Boolean)
class A
class B
@Anno(<!ANNOTATION_ARGUMENT_MUST_BE_CONST!>A::class == B::class<!>)
class C
const val equal = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>A::class == B::class<!>
@@ -0,0 +1,11 @@
// FIR_IDENTICAL
annotation class Anno(val str: String)
@Anno(<!ANNOTATION_ARGUMENT_MUST_BE_CONST!>"${A::class}"<!>)
class A
@Anno(<!ANNOTATION_ARGUMENT_MUST_BE_CONST!>B::class.toString()<!>)
class B
const val a = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>"${A::class}"<!>
const val b = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>B::class.toString()<!>
@@ -23693,6 +23693,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kCallable_before.kt");
}
@Test
@TestMetadata("kClassInEquals.kt")
public void testKClassInEquals() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInEquals.kt");
}
@Test
@TestMetadata("kClassInStringConcatenation.kt")
public void testKClassInStringConcatenation() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kClassInStringConcatenation.kt");
}
@Test
@TestMetadata("kotlinJavaCycle.kt")
public void testKotlinJavaCycle() throws Exception {