[FIR] Unregister java element finders from previous sessions
The problem here is that for common session we register `FirJavaElementFinder` which provides light classes based on expect classes. And then at the start of analysis of jvm module we register one another `FirJavaElementFinder`, which sees actual classes and uses them to build light classes But, because class ids of expect and actual class pair are the same and element finders are ordered by creation order, when java resolve tries to resolve some class, it founds light class based on expect class, even if we are already in platform session To fix this problem, it was decided to unregister all previous element finders on creation of each new session, so old finders won't interfere with analysis ^KT-63612 Fixed ^KT-64296
This commit is contained in:
committed by
Space Team
parent
e58b5e7d22
commit
f4e3203cd8
+6
@@ -35247,6 +35247,12 @@ public class LLFirBlackBoxCodegenBasedTestGenerated extends AbstractLLFirBlackBo
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/internalOverride2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaClassReferencedActualType.kt")
|
||||
public void testJavaClassReferencedActualType() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/javaClassReferencedActualType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithTypeParameter.kt")
|
||||
public void testJavaMethodWithTypeParameter() throws Exception {
|
||||
|
||||
+6
@@ -35247,6 +35247,12 @@ public class LLFirReversedBlackBoxCodegenBasedTestGenerated extends AbstractLLFi
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/internalOverride2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaClassReferencedActualType.kt")
|
||||
public void testJavaClassReferencedActualType() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/javaClassReferencedActualType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithTypeParameter.kt")
|
||||
public void testJavaMethodWithTypeParameter() throws Exception {
|
||||
|
||||
+1
-1
@@ -799,7 +799,7 @@ class KotlinCoreEnvironment private constructor(
|
||||
// exception from `ExtensionPointImpl.doRegisterExtension`, because the registered extension can no longer be found
|
||||
// when the project is being disposed.
|
||||
// For example, see the `unregisterExtension` call in `GenerationUtils.compileFilesUsingFrontendIR`.
|
||||
// TODO: refactor this to avoid registering unneeded extensions in the first place, and avoid using deprecated API.
|
||||
// TODO: refactor this to avoid registering unneeded extensions in the first place, and avoid using deprecated API. (KT-64296)
|
||||
@Suppress("DEPRECATION")
|
||||
PsiElementFinder.EP.getPoint(project).registerExtension(JavaElementFinder(this))
|
||||
@Suppress("DEPRECATION")
|
||||
|
||||
+12
-4
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.cli.jvm.compiler
|
||||
|
||||
import com.intellij.openapi.extensions.ExtensionPoint
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.StandardFileSystems
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
@@ -66,13 +67,14 @@ open class VfsBasedProjectEnvironment(
|
||||
@OptIn(SessionConfiguration::class)
|
||||
override fun registerAsJavaElementFinder(firSession: FirSession) {
|
||||
val psiFinderExtensionPoint = PsiElementFinder.EP.getPoint(project)
|
||||
if (psiFinderExtensionPoint.extensionList.any { it is JavaElementFinder }) {
|
||||
psiFinderExtensionPoint.unregisterExtension(JavaElementFinder::class.java)
|
||||
}
|
||||
psiFinderExtensionPoint.unregisterFinders<JavaElementFinder>()
|
||||
psiFinderExtensionPoint.unregisterFinders<FirJavaElementFinder>()
|
||||
|
||||
val firJavaElementFinder = FirJavaElementFinder(firSession, project)
|
||||
firSession.register(FirJavaElementFinder::class, firJavaElementFinder)
|
||||
psiFinderExtensionPoint.registerExtension(firJavaElementFinder, project)
|
||||
// see comment and TODO in KotlinCoreEnvironment.registerKotlinLightClassSupport (KT-64296)
|
||||
@Suppress("DEPRECATION")
|
||||
PsiElementFinder.EP.getPoint(project).registerExtension(firJavaElementFinder)
|
||||
}
|
||||
|
||||
private fun List<VirtualFile>.toSearchScope(allowOutOfProjectRoots: Boolean) =
|
||||
@@ -152,3 +154,9 @@ fun KotlinCoreEnvironment.toAbstractProjectEnvironment(): AbstractProjectEnviron
|
||||
|
||||
fun GlobalSearchScope.toAbstractProjectFileSearchScope(): AbstractProjectFileSearchScope =
|
||||
PsiBasedProjectFileSearchScope(this)
|
||||
|
||||
inline fun <reified T : PsiElementFinder> ExtensionPoint<PsiElementFinder>.unregisterFinders() {
|
||||
if (extensionList.any { it is T }) {
|
||||
unregisterExtension(T::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -34828,6 +34828,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/internalOverride2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaClassReferencedActualType.kt")
|
||||
public void testJavaClassReferencedActualType() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/javaClassReferencedActualType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithTypeParameter.kt")
|
||||
public void testJavaMethodWithTypeParameter() throws Exception {
|
||||
|
||||
+6
@@ -34828,6 +34828,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/internalOverride2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaClassReferencedActualType.kt")
|
||||
public void testJavaClassReferencedActualType() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/javaClassReferencedActualType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithTypeParameter.kt")
|
||||
public void testJavaMethodWithTypeParameter() throws Exception {
|
||||
|
||||
+6
@@ -34828,6 +34828,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/internalOverride2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaClassReferencedActualType.kt")
|
||||
public void testJavaClassReferencedActualType() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/multiplatform/k2/javaClassReferencedActualType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithTypeParameter.kt")
|
||||
public void testJavaMethodWithTypeParameter() throws Exception {
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
// LANGUAGE: +MultiPlatformProjects
|
||||
// IGNORE_BACKEND_K1: ANY
|
||||
// TARGET_BACKEND: JVM_IR
|
||||
|
||||
// MODULE: common
|
||||
// FILE: Base.kt
|
||||
expect interface Base
|
||||
|
||||
// MODULE: jvm()()(common)
|
||||
// FILE: Property.kt
|
||||
public actual interface Base {
|
||||
fun getValue(): PropType // /Base.PropType
|
||||
|
||||
interface PropType {
|
||||
val name: String
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: Derived.java
|
||||
public abstract class Derived implements Base {
|
||||
@Override
|
||||
public Base.PropType getValue() { // /Base/PropType
|
||||
return new Base.PropType() {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "OK";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: main.kt
|
||||
class Impl : Derived()
|
||||
|
||||
fun box(): String {
|
||||
return Impl().value.name
|
||||
}
|
||||
+2
-1
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.asJava.finder.JavaElementFinder
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.PsiBasedProjectFileSearchScope
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.VfsBasedProjectEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.unregisterFinders
|
||||
import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
|
||||
import org.jetbrains.kotlin.cli.jvm.config.jvmModularRoots
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
@@ -278,7 +279,7 @@ open class FirFrontendFacade(
|
||||
|
||||
val project = compilerConfigurationProvider.getProject(module)
|
||||
|
||||
PsiElementFinder.EP.getPoint(project).unregisterExtension(JavaElementFinder::class.java)
|
||||
PsiElementFinder.EP.getPoint(project).unregisterFinders<JavaElementFinder>()
|
||||
|
||||
val parser = module.directives.singleValue(FirDiagnosticsDirectives.FIR_PARSER)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user