[Analysis API] fix "KotlinIllegalStateExceptionWithAttachments: expected as maximum one expect for the actual"
^KT-61597 fixed
This commit is contained in:
committed by
Space Team
parent
f39153b6e6
commit
56910b70a3
+6
-15
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.bas
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtDeclarationSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.psiSafe
|
||||
import org.jetbrains.kotlin.utils.exceptions.checkWithAttachment
|
||||
import org.jetbrains.kotlin.descriptors.MemberDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
|
||||
@@ -20,25 +19,17 @@ import org.jetbrains.kotlin.resolve.multiplatform.ExpectActualCompatibility
|
||||
import org.jetbrains.kotlin.resolve.multiplatform.ExpectedActualResolver
|
||||
|
||||
internal class KtFe10MultiplatformInfoProvider(
|
||||
override val analysisSession: KtFe10AnalysisSession
|
||||
override val analysisSession: KtFe10AnalysisSession,
|
||||
) : KtMultiplatformInfoProvider(), Fe10KtAnalysisSessionComponent {
|
||||
override fun getExpectForActual(actual: KtDeclarationSymbol): KtDeclarationSymbol? {
|
||||
if (actual.psiSafe<KtDeclaration>()?.hasActualModifier() != true) return null
|
||||
val memberDescriptor = (getSymbolDescriptor(actual) as? MemberDescriptor)?.takeIf { it.isActual } ?: return null
|
||||
override fun getExpectForActual(actual: KtDeclarationSymbol): List<KtDeclarationSymbol> {
|
||||
if (actual.psiSafe<KtDeclaration>()?.hasActualModifier() != true) return emptyList()
|
||||
val memberDescriptor = (getSymbolDescriptor(actual) as? MemberDescriptor)?.takeIf { it.isActual } ?: return emptyList()
|
||||
|
||||
val expectedCompatibilityMap =
|
||||
ExpectedActualResolver.findExpectedForActual(memberDescriptor) ?: return null
|
||||
ExpectedActualResolver.findExpectedForActual(memberDescriptor) ?: return emptyList()
|
||||
|
||||
val expectsForActual = (expectedCompatibilityMap[ExpectActualCompatibility.Compatible]
|
||||
?: expectedCompatibilityMap.values.flatten())
|
||||
check(expectsForActual.size <= 1) { "expected as maximum one `expect` for the actual" }
|
||||
checkWithAttachment(expectsForActual.size <= 1, message = { "expected as maximum one `expect` for the actual" }) {
|
||||
withEntry("actual", memberDescriptor.toString())
|
||||
withEntry("expectsForActualSize", expectsForActual.size.toString())
|
||||
for ((index, expectForActual) in expectsForActual.withIndex()) {
|
||||
withEntry("expectsForActual$index", expectForActual.toString())
|
||||
}
|
||||
}
|
||||
return expectsForActual.singleOrNull()?.toKtSymbol(analysisContext) as? KtDeclarationSymbol
|
||||
return expectsForActual.map { it.toKtSymbol(analysisContext) as KtDeclarationSymbol }
|
||||
}
|
||||
}
|
||||
+6
@@ -51,4 +51,10 @@ public class Fe10IdeNormalAnalysisSourceModuleExpectForActualTestGenerated exten
|
||||
public void testAllFilesPresentInExpectForActual() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/multiplatformInfoProvider/expectForActual"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleExpects.kt")
|
||||
public void testMultipleExpects() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/multiplatformInfoProvider/expectForActual/multipleExpects.kt");
|
||||
}
|
||||
}
|
||||
|
||||
+4
-13
@@ -10,8 +10,6 @@ import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.fir.symbols.KtFirSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtDeclarationSymbol
|
||||
import org.jetbrains.kotlin.fir.utils.exceptions.withFirSymbolEntry
|
||||
import org.jetbrains.kotlin.utils.exceptions.checkWithAttachment
|
||||
import org.jetbrains.kotlin.fir.declarations.expectForActual
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
|
||||
@@ -22,7 +20,7 @@ internal class KtFirMultiplatformInfoProvider(
|
||||
override val analysisSession: KtFirAnalysisSession,
|
||||
override val token: KtLifetimeToken,
|
||||
) : KtMultiplatformInfoProvider(), KtFirAnalysisSessionComponent {
|
||||
override fun getExpectForActual(actual: KtDeclarationSymbol): KtDeclarationSymbol? {
|
||||
override fun getExpectForActual(actual: KtDeclarationSymbol): List<KtDeclarationSymbol> {
|
||||
require(actual is KtFirSymbol<*>)
|
||||
val firSymbol = actual.firSymbol
|
||||
val status = when (firSymbol) {
|
||||
@@ -31,16 +29,9 @@ internal class KtFirMultiplatformInfoProvider(
|
||||
is FirTypeAliasSymbol -> firSymbol.rawStatus
|
||||
else -> null
|
||||
}
|
||||
if (status?.isActual != true) return null
|
||||
if (status?.isActual != true) return emptyList()
|
||||
|
||||
val expectsForActual = firSymbol.expectForActual?.get(ExpectActualCompatibility.Compatible) ?: return null
|
||||
checkWithAttachment(expectsForActual.size <= 1, message = { "expected as maximum one `expect` for the actual" }) {
|
||||
withFirSymbolEntry("actual", firSymbol)
|
||||
withEntry("expectsForActualSize", expectsForActual.size.toString())
|
||||
for ((index, expectForActual) in expectsForActual.withIndex()) {
|
||||
withFirSymbolEntry("expectsForActual$index", expectForActual)
|
||||
}
|
||||
}
|
||||
return expectsForActual.singleOrNull()?.let { analysisSession.firSymbolBuilder.buildSymbol(it) as? KtDeclarationSymbol }
|
||||
val expectsForActual = firSymbol.expectForActual?.get(ExpectActualCompatibility.Compatible) ?: return emptyList()
|
||||
return expectsForActual.map { analysisSession.firSymbolBuilder.buildSymbol(it) as KtDeclarationSymbol }
|
||||
}
|
||||
}
|
||||
+6
@@ -51,4 +51,10 @@ public class FirIdeDependentAnalysisSourceModuleExpectForActualTestGenerated ext
|
||||
public void testAllFilesPresentInExpectForActual() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/multiplatformInfoProvider/expectForActual"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleExpects.kt")
|
||||
public void testMultipleExpects() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/multiplatformInfoProvider/expectForActual/multipleExpects.kt");
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -51,4 +51,10 @@ public class FirIdeNormalAnalysisSourceModuleExpectForActualTestGenerated extend
|
||||
public void testAllFilesPresentInExpectForActual() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/multiplatformInfoProvider/expectForActual"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleExpects.kt")
|
||||
public void testMultipleExpects() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/multiplatformInfoProvider/expectForActual/multipleExpects.kt");
|
||||
}
|
||||
}
|
||||
|
||||
+6
-3
@@ -24,13 +24,16 @@ abstract class AbstractExpectForActualTest : AbstractAnalysisApiBasedTest() {
|
||||
|
||||
val expectedSymbolText: String? = executeOnPooledThreadInReadAction {
|
||||
analyseForTest(declaration) {
|
||||
val expectedSymbol = declaration.getSymbol().getExpectForActual() ?: return@analyseForTest null
|
||||
expectedSymbol.psi?.containingFile?.name + " : " + expectedSymbol.render()
|
||||
val expectedSymbols = declaration.getSymbol().getExpectsForActual()
|
||||
expectedSymbols.joinToString(separator = "\n") { expectedSymbol ->
|
||||
expectedSymbol.psi?.containingFile?.name + " : " + expectedSymbol.render()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val actual = buildString {
|
||||
appendLine("expected symbol: $expectedSymbolText")
|
||||
appendLine("expected symbols:")
|
||||
appendLine(expectedSymbolText)
|
||||
}
|
||||
|
||||
testServices.assertions.assertEqualsToTestDataFileSibling(actual)
|
||||
|
||||
+6
@@ -51,4 +51,10 @@ public class FirStandaloneNormalAnalysisSourceModuleExpectForActualTestGenerated
|
||||
public void testAllFilesPresentInExpectForActual() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/multiplatformInfoProvider/expectForActual"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleExpects.kt")
|
||||
public void testMultipleExpects() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/multiplatformInfoProvider/expectForActual/multipleExpects.kt");
|
||||
}
|
||||
}
|
||||
|
||||
+4
-3
@@ -9,15 +9,16 @@ import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtDeclarationSymbol
|
||||
|
||||
public abstract class KtMultiplatformInfoProvider : KtAnalysisSessionComponent() {
|
||||
public abstract fun getExpectForActual(actual: KtDeclarationSymbol): KtDeclarationSymbol?
|
||||
public abstract fun getExpectForActual(actual: KtDeclarationSymbol): List<KtDeclarationSymbol>
|
||||
}
|
||||
|
||||
public interface KtMultiplatformInfoProviderMixin : KtAnalysisSessionMixIn {
|
||||
|
||||
/**
|
||||
* Gives expect symbol for the actual one if it is available.
|
||||
*
|
||||
* @return a single expect declaration corresponds to the [KtDeclarationSymbol] on valid code or multiple expects in a case of erroneous code with multiple expects.
|
||||
**/
|
||||
public fun KtDeclarationSymbol.getExpectForActual(): KtDeclarationSymbol? =
|
||||
public fun KtDeclarationSymbol.getExpectsForActual(): List<KtDeclarationSymbol> =
|
||||
withValidityAssertion { analysisSession.multiplatformInfoProvider.getExpectForActual(this) }
|
||||
|
||||
}
|
||||
+2
-1
@@ -1 +1,2 @@
|
||||
expected symbol: null
|
||||
expected symbols:
|
||||
|
||||
|
||||
+2
-1
@@ -1 +1,2 @@
|
||||
expected symbol: Common.kt : object Platform
|
||||
expected symbols:
|
||||
Common.kt : object Platform
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
expected symbols:
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// !LANGUAGE: +MultiPlatformProjects
|
||||
|
||||
// MODULE: commonMain1
|
||||
// FILE: Common1.kt
|
||||
|
||||
package sample
|
||||
expect fun foo()
|
||||
|
||||
// MODULE: commonMain2
|
||||
// FILE: Common2.kt
|
||||
|
||||
package sample
|
||||
expect fun foo()
|
||||
|
||||
// MODULE: androidMain(commonMain1, commonMain2)
|
||||
// FILE: JvmAndroid.kt
|
||||
|
||||
package sample
|
||||
actual fun f<caret>oo() {}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
expected symbols:
|
||||
Common1.kt : fun foo()
|
||||
Common2.kt : fun foo()
|
||||
Reference in New Issue
Block a user