[AA FIR] KT-58503 Use resolvedStatus to access visibility and modality of a class

Compiler plugins (AllOpen, for example) can modify the status of a
declaration, including visibility and modality. If we observe them
without enforcing STATUS resolve, we risk to see incorrect information,
which in turn can lead to false negatives/positives in
inspections/intentions.

Add test for light classes. See KT-58503 description for the explanation
how the initial problem was influencing Spring inspections from intellij
repository.

^KT-58503 Fixed
This commit is contained in:
Roman Golyshev
2023-05-08 15:04:35 +02:00
committed by teamcity
parent c06ec84bb1
commit 105611679e
11 changed files with 163 additions and 5 deletions
@@ -37,21 +37,19 @@ internal class KtFirNamedClassOrObjectSymbol(
override val classIdIfNonLocal: ClassId?
get() = withValidityAssertion { firSymbol.getClassIdIfNonLocal() }
/* FirRegularClass modality is not modified by STATUS, so it can be taken from RAW */
override val modality: Modality
get() = withValidityAssertion {
firSymbol.fir.modality
firSymbol.modality
?: when (classKind) { // default modality
KtClassKind.INTERFACE -> Modality.ABSTRACT
else -> Modality.FINAL
}
}
/* FirRegularClass visibility is not modified by STATUS only for Unknown, so it can be taken from RAW */
override val visibility: Visibility
get() = withValidityAssertion {
when (val possiblyRawVisibility = firSymbol.fir.visibility) {
Visibilities.Unknown -> if (firSymbol.fir.isLocal) Visibilities.Local else Visibilities.Public
when (val possiblyRawVisibility = firSymbol.visibility) {
Visibilities.Unknown -> if (firSymbol.isLocal) Visibilities.Local else Visibilities.Public
else -> possiblyRawVisibility
}
}
@@ -559,4 +559,20 @@ public class SymbolLightClassesByFqNameForLibraryTestGenerated extends AbstractS
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/script"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
}
@Nested
@TestMetadata("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled")
@TestDataPath("$PROJECT_ROOT")
public class WithTestCompilerPluginEnabled {
@Test
public void testAllFilesPresentInWithTestCompilerPluginEnabled() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@Test
@TestMetadata("allOpen.kt")
public void testAllOpen() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled/allOpen.kt");
}
}
}
@@ -559,4 +559,20 @@ public class SymbolLightClassesEqualityByFqNameForLibraryTestGenerated extends A
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/script"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
}
@Nested
@TestMetadata("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled")
@TestDataPath("$PROJECT_ROOT")
public class WithTestCompilerPluginEnabled {
@Test
public void testAllFilesPresentInWithTestCompilerPluginEnabled() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@Test
@TestMetadata("allOpen.kt")
public void testAllOpen() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled/allOpen.kt");
}
}
}
@@ -559,4 +559,20 @@ public class SymbolLightClassesParentingByFqNameForLibraryTestGenerated extends
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/script"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
}
@Nested
@TestMetadata("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled")
@TestDataPath("$PROJECT_ROOT")
public class WithTestCompilerPluginEnabled {
@Test
public void testAllFilesPresentInWithTestCompilerPluginEnabled() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@Test
@TestMetadata("allOpen.kt")
public void testAllOpen() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled/allOpen.kt");
}
}
}
@@ -701,4 +701,20 @@ public class SymbolLightClassesByFqNameForSourceTestGenerated extends AbstractSy
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/script"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
}
@Nested
@TestMetadata("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled")
@TestDataPath("$PROJECT_ROOT")
public class WithTestCompilerPluginEnabled {
@Test
public void testAllFilesPresentInWithTestCompilerPluginEnabled() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@Test
@TestMetadata("allOpen.kt")
public void testAllOpen() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled/allOpen.kt");
}
}
}
@@ -701,4 +701,20 @@ public class SymbolLightClassesEqualityByFqNameForSourceTestGenerated extends Ab
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/script"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
}
@Nested
@TestMetadata("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled")
@TestDataPath("$PROJECT_ROOT")
public class WithTestCompilerPluginEnabled {
@Test
public void testAllFilesPresentInWithTestCompilerPluginEnabled() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@Test
@TestMetadata("allOpen.kt")
public void testAllOpen() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled/allOpen.kt");
}
}
}
@@ -701,4 +701,20 @@ public class SymbolLightClassesParentingByFqNameForSourceTestGenerated extends A
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/script"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
}
@Nested
@TestMetadata("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled")
@TestDataPath("$PROJECT_ROOT")
public class WithTestCompilerPluginEnabled {
@Test
public void testAllFilesPresentInWithTestCompilerPluginEnabled() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@Test
@TestMetadata("allOpen.kt")
public void testAllOpen() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled/allOpen.kt");
}
}
}
@@ -0,0 +1,12 @@
@org.jetbrains.kotlin.fir.plugin.AllOpen()
public class BaseClass /* test.BaseClass*/ {
private int prop = 42 /* initializer type: int */;
public BaseClass();// .ctor()
public int getProp();// getProp()
public void function();// function()
public void setProp(int);// setProp(int)
}
@@ -0,0 +1,12 @@
@org.jetbrains.kotlin.fir.plugin.AllOpen()
public final class BaseClass /* test.BaseClass*/ {
private int prop;
public BaseClass();// .ctor()
public final int getProp();// getProp()
public final void function();// function()
public final void setProp(int);// setProp(int)
}
@@ -0,0 +1,22 @@
// test.BaseClass
// WITH_FIR_TEST_COMPILER_PLUGIN
// IGNORE_LIBRARY_EXCEPTIONS: KT-58535
// FILE: main.kt
package test
import org.jetbrains.kotlin.fir.plugin.AllOpen
@AllOpen
class BaseClass {
fun function() {}
var prop: Int = 42
}
// FILE: AllOpen.kt
package org.jetbrains.kotlin.fir.plugin
/**
* Imitates AllOpen annotation to trigger AllOpenMatcherBasedStatusTransformer
*/
annotation class AllOpen
@@ -578,4 +578,22 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/script/InnerClasses.kts");
}
}
@TestMetadata("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WithTestCompilerPluginEnabled extends AbstractCompilerLightClassTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInWithTestCompilerPluginEnabled() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), null, true);
}
@TestMetadata("allOpen.kt")
public void testAllOpen() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/withTestCompilerPluginEnabled/allOpen.kt");
}
}
}