[LL FIR, Java] fix resolve contract violation from java symbol provider from supertypes

To create a smart psi type pointer, IJ Platform uses resolve
We cannot use resolve from JavaSymbolProvider,
as it may lead to resolve contract violation

^KT-59240 Fixed
This commit is contained in:
Dmitrii Gridin
2023-06-12 13:41:58 +02:00
committed by Space Team
parent 67d933185a
commit 6992a707dc
14 changed files with 172 additions and 1 deletions
@@ -60,3 +60,11 @@ internal class JavaElementDelegatingTypeParameterBoundTypeSourceWithSmartPointer
return psi.bounds[boundIndex] as TYPE
}
}
internal class JavaElementDelegatingSuperTypeSourceWithSmartPointer(
override val psiPointer: SmartPsiElementPointer<out PsiClass>,
private val superTypeIndex: Int,
override val factory: JavaElementSourceFactory,
) : JavaElementDelegatingTypeSourceWithSmartPointer<PsiClass, PsiClassType>() {
override fun getType(psi: PsiClass): PsiClassType = psi.superTypes[superTypeIndex]
}
@@ -45,6 +45,18 @@ class JavaElementSourceWithSmartPointerFactory(project: Project) : JavaElementSo
)
}
override fun createSuperTypeSource(
psiTypeParameterSource: JavaElementPsiSource<out PsiClass>,
superTypeIndex: Int
): JavaElementTypeSource<PsiClassType> {
require(psiTypeParameterSource is JavaElementPsiSourceWithSmartPointer)
return JavaElementDelegatingSuperTypeSourceWithSmartPointer(
psiTypeParameterSource.pointer,
superTypeIndex,
psiTypeParameterSource.factory,
)
}
override fun <TYPE : PsiType> createExpressionTypeSource(psiExpressionSource: JavaElementPsiSource<out PsiExpression>): JavaElementTypeSource<TYPE> {
require(psiExpressionSource is JavaElementPsiSourceWithSmartPointer)
return JavaElementDelegatingExpressionTypeSourceWithSmartPointer(psiExpressionSource.pointer, psiExpressionSource.factory)
@@ -27,6 +27,18 @@ public class DiagnosticCompilerTestFirTestdataTestGenerated extends AbstractDiag
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaImport.kt");
}
@Test
@TestMetadata("accessJavaFromKotlinViaStaticImport.kt")
public void testAccessJavaFromKotlinViaStaticImport() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport.kt");
}
@Test
@TestMetadata("accessJavaFromKotlinViaStaticImport2.kt")
public void testAccessJavaFromKotlinViaStaticImport2() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport2.kt");
}
@Test
public void testAllFilesPresentInResolve() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
@@ -27,6 +27,18 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFirTestDataTestGenerated
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaImport.kt");
}
@Test
@TestMetadata("accessJavaFromKotlinViaStaticImport.kt")
public void testAccessJavaFromKotlinViaStaticImport() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport.kt");
}
@Test
@TestMetadata("accessJavaFromKotlinViaStaticImport2.kt")
public void testAccessJavaFromKotlinViaStaticImport2() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport2.kt");
}
@Test
public void testAllFilesPresentInResolve() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
@@ -30,6 +30,16 @@ public class LazyBodyIsNotTouchedTestGenerated extends AbstractLazyBodyIsNotTouc
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaImport.kt");
}
@TestMetadata("accessJavaFromKotlinViaStaticImport.kt")
public void testAccessJavaFromKotlinViaStaticImport() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport.kt");
}
@TestMetadata("accessJavaFromKotlinViaStaticImport2.kt")
public void testAccessJavaFromKotlinViaStaticImport2() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport2.kt");
}
public void testAllFilesPresentInResolve() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@@ -0,0 +1,11 @@
FILE: useSite.kt
public final fun foo(): R|kotlin/Int| {
^foo Int(4)
}
FILE: KotlinInterface.kt
public abstract interface KotlinInterface : R|kotlin/Any| {
public abstract var selectedOptions: R|kotlin/Int|
public get(): R|kotlin/Int|
public set(value: R|kotlin/Int|): R|kotlin/Unit|
}
@@ -0,0 +1,24 @@
// FILE: useSite.kt
import InspectionProfileImpl.INIT_INSPECTIONS
fun foo(): Int = 4
// FILE: InspectionProfileImpl.java
import static Configuration.StaticConfigurationClass
public class InspectionProfileImpl extends StaticConfigurationClass {
public static boolean INIT_INSPECTIONS;
}
// FILE: Configuration.java
public class Configuration implements KotlinInterface {
public static class StaticConfigurationClass {
}
}
// FILE: KotlinInterface.kt
interface KotlinInterface {
var selectedOptions: Int
}
@@ -0,0 +1,11 @@
FILE: useSite.kt
public final fun foo(): R|kotlin/Int| {
^foo Int(4)
}
FILE: KotlinInterface.kt
public abstract interface KotlinInterface : R|kotlin/Any| {
public abstract var selectedOptions: R|kotlin/Int|
public get(): R|kotlin/Int|
public set(value: R|kotlin/Int|): R|kotlin/Unit|
}
@@ -0,0 +1,28 @@
// FILE: useSite.kt
import InspectionProfileImpl.INIT_INSPECTIONS
fun foo(): Int = 4
// FILE: InspectionProfileImpl.java
import static Configuration.StaticConfigurationClass
public class InspectionProfileImpl extends InspectionProfile<StaticConfigurationClass> {
public static boolean INIT_INSPECTIONS;
}
// FILE: InspectionProfile.java
public class InspectionProfile <T> {
}
// FILE: Configuration.java
public class Configuration implements KotlinInterface {
public static class StaticConfigurationClass {
}
}
// FILE: KotlinInterface.kt
interface KotlinInterface {
var selectedOptions: Int
}
@@ -27,6 +27,18 @@ public class FirLightTreeDiagnosticsTestGenerated extends AbstractFirLightTreeDi
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaImport.kt");
}
@Test
@TestMetadata("accessJavaFromKotlinViaStaticImport.kt")
public void testAccessJavaFromKotlinViaStaticImport() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport.kt");
}
@Test
@TestMetadata("accessJavaFromKotlinViaStaticImport2.kt")
public void testAccessJavaFromKotlinViaStaticImport2() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport2.kt");
}
@Test
public void testAllFilesPresentInResolve() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
@@ -27,6 +27,18 @@ public class FirPsiDiagnosticTestGenerated extends AbstractFirPsiDiagnosticTest
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaImport.kt");
}
@Test
@TestMetadata("accessJavaFromKotlinViaStaticImport.kt")
public void testAccessJavaFromKotlinViaStaticImport() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport.kt");
}
@Test
@TestMetadata("accessJavaFromKotlinViaStaticImport2.kt")
public void testAccessJavaFromKotlinViaStaticImport2() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/accessJavaFromKotlinViaStaticImport2.kt");
}
@Test
public void testAllFilesPresentInResolve() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
@@ -84,7 +84,9 @@ class JavaClassImpl(psiClassSource: JavaElementPsiSource<PsiClass>) : JavaClassi
get() = typeParameters(psi.typeParameters, sourceFactory)
override val supertypes: Collection<JavaClassifierType>
get() = classifierTypes(psi.superTypes, sourceFactory)
get() = psi.superTypes.convertIndexed { index, _ ->
JavaClassifierTypeImpl(sourceFactory.createSuperTypeSource(psiElementSource, index))
}
override val methods: Collection<JavaMethod>
get() {
@@ -32,6 +32,12 @@ inline fun <Psi, Java> Array<Psi>.convert(factory: (Psi) -> Java): List<Java> =
else -> map(factory)
}
inline fun <Psi, Java> Array<Psi>.convertIndexed(factory: (Int, Psi) -> Java): List<Java> = when (size) {
0 -> emptyList()
1 -> listOf(factory(0, first()))
else -> mapIndexed(factory)
}
fun <Psi, Java> Collection<Psi>.convert(factory: (Psi) -> Java): List<Java> =
when (size) {
0 -> emptyList()
@@ -19,6 +19,11 @@ abstract class JavaElementSourceFactory {
boundIndex: Int,
): JavaElementTypeSource<TYPE>
abstract fun createSuperTypeSource(
psiTypeParameterSource: JavaElementPsiSource<out PsiClass>,
superTypeIndex: Int,
): JavaElementTypeSource<PsiClassType>
abstract fun <TYPE : PsiType> createExpressionTypeSource(psiExpressionSource: JavaElementPsiSource<out PsiExpression>): JavaElementTypeSource<TYPE>
companion object {
@@ -51,6 +56,12 @@ class JavaFixedElementSourceFactory : JavaElementSourceFactory() {
return createTypeSource(psiTypeParameterSource.psi.bounds[boundIndex] as TYPE)
}
override fun createSuperTypeSource(
psiTypeParameterSource: JavaElementPsiSource<out PsiClass>,
superTypeIndex: Int,
): JavaElementTypeSource<PsiClassType> {
return createTypeSource(psiTypeParameterSource.psi.superTypes[superTypeIndex])
}
override fun <TYPE : PsiType> createExpressionTypeSource(psiExpressionSource: JavaElementPsiSource<out PsiExpression>): JavaElementTypeSource<TYPE> {
@Suppress("UNCHECKED_CAST")