FIR IDE: make smartcast API return unstable smartcasts

This commit is contained in:
Tianyu Geng
2021-11-03 10:34:49 -07:00
committed by Ilya Kirillov
parent 6462642988
commit 197079c85c
31 changed files with 356 additions and 23 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.analysis.api.descriptors.components
import org.jetbrains.kotlin.analysis.api.ImplicitReceiverSmartCast
import org.jetbrains.kotlin.analysis.api.ImplicitReceiverSmartcastKind
import org.jetbrains.kotlin.analysis.api.components.KtSmartCastProvider
import org.jetbrains.kotlin.analysis.api.components.SmartCastInfo
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtType
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
@@ -15,6 +16,7 @@ import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.smartcasts.ExplicitSmartCasts
import org.jetbrains.kotlin.resolve.calls.smartcasts.MultipleSmartCasts
import org.jetbrains.kotlin.types.TypeIntersector
@@ -22,19 +24,29 @@ internal class KtFe10SmartCastProvider(override val analysisSession: KtFe10Analy
override val token: ValidityToken
get() = analysisSession.token
override fun getSmartCastedToType(expression: KtExpression): KtType? {
override fun getSmartCastedInfo(expression: KtExpression): SmartCastInfo? {
withValidityAssertion {
val bindingContext = analysisSession.analyze(expression)
val smartCasts = bindingContext[BindingContext.SMARTCAST, expression] ?: return null
val stableSmartCasts = bindingContext[BindingContext.SMARTCAST, expression]
if (smartCasts is MultipleSmartCasts) {
return TypeIntersector.intersectTypes(smartCasts.map.values)?.toKtType(analysisSession)
return when {
stableSmartCasts != null -> {
val type = stableSmartCasts.getKtType() ?: return null
SmartCastInfo(type, true)
}
// TODO: collect unstable smartcast here.
else -> null
}
return smartCasts.defaultType?.toKtType(analysisSession)
}
}
private fun ExplicitSmartCasts.getKtType(): KtType? {
if (this is MultipleSmartCasts) {
return TypeIntersector.intersectTypes(map.values)?.toKtType(analysisSession)
}
return defaultType?.toKtType(analysisSession)
}
override fun getImplicitReceiverSmartCast(expression: KtExpression): Collection<ImplicitReceiverSmartCast> {
withValidityAssertion {
val bindingContext = analysisSession.analyze(expression)
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.descriptors.test.components
import org.jetbrains.kotlin.analysis.api.descriptors.test.KtFe10FrontendApiTestConfiguratorService
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.AbstractHLSmartCastInfoTest
abstract class AbstractKtFe10HLSmartCastInfoTest : AbstractHLSmartCastInfoTest(KtFe10FrontendApiTestConfiguratorService)
@@ -0,0 +1,74 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.descriptors.test.components;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/analysis-api/testData/components/smartCastInfo")
@TestDataPath("$PROJECT_ROOT")
public class KtFe10HLSmartCastInfoTestGenerated extends AbstractKtFe10HLSmartCastInfoTest {
@Test
public void testAllFilesPresentInSmartCastInfo() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/smartCastInfo"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("multiSmartcastAsReceiver_stable.kt")
public void testMultiSmartcastAsReceiver_stable() throws Exception {
runTest("analysis/analysis-api/testData/components/smartCastInfo/multiSmartcastAsReceiver_stable.kt");
}
@Test
@TestMetadata("multiSmartcastAsReceiver_unstable.kt")
public void testMultiSmartcastAsReceiver_unstable() throws Exception {
runTest("analysis/analysis-api/testData/components/smartCastInfo/multiSmartcastAsReceiver_unstable.kt");
}
@Test
@TestMetadata("multiSmartcast_stable.kt")
public void testMultiSmartcast_stable() throws Exception {
runTest("analysis/analysis-api/testData/components/smartCastInfo/multiSmartcast_stable.kt");
}
@Test
@TestMetadata("multiSmartcast_unstable.kt")
public void testMultiSmartcast_unstable() throws Exception {
runTest("analysis/analysis-api/testData/components/smartCastInfo/multiSmartcast_unstable.kt");
}
@Test
@TestMetadata("smartcastAsReceiver_stable.kt")
public void testSmartcastAsReceiver_stable() throws Exception {
runTest("analysis/analysis-api/testData/components/smartCastInfo/smartcastAsReceiver_stable.kt");
}
@Test
@TestMetadata("smartcastAsReceiver_unstable.kt")
public void testSmartcastAsReceiver_unstable() throws Exception {
runTest("analysis/analysis-api/testData/components/smartCastInfo/smartcastAsReceiver_unstable.kt");
}
@Test
@TestMetadata("smartcast_stable.kt")
public void testSmartcast_stable() throws Exception {
runTest("analysis/analysis-api/testData/components/smartCastInfo/smartcast_stable.kt");
}
@Test
@TestMetadata("smartcast_unstable.kt")
public void testSmartcast_unstable() throws Exception {
runTest("analysis/analysis-api/testData/components/smartCastInfo/smartcast_unstable.kt");
}
}