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");
}
}
@@ -5,31 +5,33 @@
package org.jetbrains.kotlin.analysis.api.fir.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.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFirSafe
import org.jetbrains.kotlin.fir.expressions.FirExpressionWithSmartcast
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.coneTypeSafe
import org.jetbrains.kotlin.fir.types.isStableSmartcast
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFirSafe
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.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.psi.KtExpression
internal class KtFirSmartcastProvider(
override val analysisSession: KtFirAnalysisSession,
override val token: ValidityToken,
) : KtSmartCastProvider(), KtFirAnalysisSessionComponent {
override fun getSmartCastedToType(expression: KtExpression): KtType? = withValidityAssertion {
expression.getOrBuildFirSafe<FirExpressionWithSmartcast>(analysisSession.firResolveState)
?.takeIf { it.isStable }
?.typeRef
?.coneTypeSafe<ConeKotlinType>()
?.asKtType()
override fun getSmartCastedInfo(expression: KtExpression): SmartCastInfo? = withValidityAssertion {
val smartCastExpression =
expression.getOrBuildFirSafe<FirExpressionWithSmartcast>(analysisSession.firResolveState) ?: return@withValidityAssertion null
SmartCastInfo(
smartCastExpression.smartcastType.coneTypeSafe<ConeKotlinType>()?.asKtType() ?: return@withValidityAssertion null,
smartCastExpression.isStable
)
}
@OptIn(ExperimentalStdlibApi::class)
@@ -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.fir.components
import org.jetbrains.kotlin.analysis.api.fir.FirFrontendApiTestConfiguratorService
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.AbstractHLSmartCastInfoTest
abstract class AbstractFirHLSmartCastInfoTest : AbstractHLSmartCastInfoTest(FirFrontendApiTestConfiguratorService)
@@ -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.fir.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 FirHLSmartCastInfoTestGenerated extends AbstractFirHLSmartCastInfoTest {
@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");
}
}
@@ -0,0 +1,34 @@
/*
* 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.impl.base.test.components
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.expressionMarkerProvider
import org.jetbrains.kotlin.analysis.api.impl.base.test.test.framework.AbstractHLApiSingleFileTest
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
abstract class AbstractHLSmartCastInfoTest(configurator: FrontendApiTestConfiguratorService) : AbstractHLApiSingleFileTest(configurator) {
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
super.doTestByFileStructure(ktFile, module, testServices)
val expression = testServices.expressionMarkerProvider.getSelectedElement(ktFile) as KtExpression
val actual = executeOnPooledThreadInReadAction {
analyseForTest(expression) {
val smartCastInfo = expression.getSmartCastInfo()
buildString {
appendLine("expression: ${expression.text}")
appendLine("isStable: ${smartCastInfo?.isStable}")
appendLine("smartCastType: ${smartCastInfo?.smartCastType?.render()}")
}
}
}
testServices.assertions.assertEqualsToFile(testDataFileSibling(".txt"), actual)
}
}
@@ -10,13 +10,18 @@ import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.psi.KtExpression
public abstract class KtSmartCastProvider : KtAnalysisSessionComponent() {
public abstract fun getSmartCastedToType(expression: KtExpression): KtType?
public abstract fun getSmartCastedInfo(expression: KtExpression): SmartCastInfo?
public abstract fun getImplicitReceiverSmartCast(expression: KtExpression): Collection<ImplicitReceiverSmartCast>
}
public class SmartCastInfo(public val smartCastType: KtType, public val isStable: Boolean)
public interface KtSmartCastProviderMixIn : KtAnalysisSessionMixIn {
public fun KtExpression.getSmartCast(): KtType? =
analysisSession.smartCastProvider.getSmartCastedToType(this)
/**
* Gets the smart-cast information of the given expression or null if the expression is not smart casted.
*/
public fun KtExpression.getSmartCastInfo(): SmartCastInfo? =
analysisSession.smartCastProvider.getSmartCastedInfo(this)
public fun KtExpression.getImplicitReceiverSmartCast(): Collection<ImplicitReceiverSmartCast> =
analysisSession.smartCastProvider.getImplicitReceiverSmartCast(this)
@@ -0,0 +1,3 @@
expression: a
isStable: null
smartCastType: null
@@ -0,0 +1,10 @@
interface A
interface B
fun <T> T.foo() where T: A, T: B {}
fun test(a: Any) {
if (a is A && a is B) {
<expr>a</expr>.foo()
}
}
@@ -0,0 +1,3 @@
expression: a
isStable: true
smartCastType: (A&B)
@@ -0,0 +1,3 @@
expression: a
isStable: null
smartCastType: null
@@ -0,0 +1,11 @@
interface A
interface B
fun <T> T.foo() where T: A, T: B {}
var a: Any = 1
fun test() {
if (a is A && a is B) {
<expr>a</expr>.foo()
}
}
@@ -0,0 +1,3 @@
expression: a
isStable: false
smartCastType: (A&B)
@@ -0,0 +1,3 @@
expression: a
isStable: null
smartCastType: null
@@ -0,0 +1,8 @@
interface A
interface B
fun test(a: Any) {
if (a is A && a is B) {
<expr>a</expr>
}
}
@@ -0,0 +1,3 @@
expression: a
isStable: true
smartCastType: (A&B)
@@ -0,0 +1,3 @@
expression: a
isStable: null
smartCastType: null
@@ -0,0 +1,9 @@
interface A
interface B
var a: Any = 1
fun test() {
if (a is A && a is B) {
<expr>a</expr>
}
}
@@ -0,0 +1,3 @@
expression: a
isStable: false
smartCastType: (A&B)
@@ -0,0 +1,5 @@
fun test(a: Any) {
if (a is String) {
<expr>a</expr>.length
}
}
@@ -0,0 +1,3 @@
expression: a
isStable: true
smartCastType: kotlin.String
@@ -0,0 +1,6 @@
var a: Any = 1
fun test() {
if (a is String) {
<expr>a</expr>.length
}
}
@@ -0,0 +1,3 @@
expression: a
isStable: null
smartCastType: null
@@ -0,0 +1,3 @@
expression: a
isStable: null
smartCastType: null
@@ -0,0 +1,5 @@
fun test(a: Any) {
if (a is String) {
<expr>a</expr>
}
}
@@ -0,0 +1,3 @@
expression: a
isStable: true
smartCastType: kotlin.String
@@ -0,0 +1,3 @@
expression: a
isStable: null
smartCastType: null
@@ -0,0 +1,6 @@
var a: Any = 1
fun test() {
if (a is String) {
<expr>a</expr>
}
}
@@ -0,0 +1,3 @@
expression: a
isStable: false
smartCastType: kotlin.String
@@ -108,6 +108,10 @@ fun main(args: Array<String>) {
testClass<AbstractPsiTypeProviderTest> {
model("components/psiTypeProvider")
}
testClass<AbstractFirHLSmartCastInfoTest> {
model("components/smartCastInfo")
}
}
testGroup("analysis/analysis-api-fe10/tests", "analysis/analysis-api/testData") {
@@ -162,6 +166,10 @@ fun main(args: Array<String>) {
testClass<AbstractKtFe10HasCommonSubtypeTest> {
model("components/hasCommonSubtype")
}
testClass<AbstractKtFe10HLSmartCastInfoTest> {
model("components/smartCastInfo")
}
}
testGroup("analysis/low-level-api-fir/tests", "compiler/fir/raw-fir/psi2fir/testData") {