FE1.0 Analysis API: get smartcast if available with getKtExpressionType

The current implementation still does not work with multicast. In
addition, it appears FE1.0 does not attempt smart cast if it's not used.
This commit is contained in:
Tianyu Geng
2021-12-07 15:19:05 -08:00
committed by teamcity
parent a5844b754e
commit 2f393cdd02
14 changed files with 102 additions and 2 deletions
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.inference.returnTypeOrNothing
import org.jetbrains.kotlin.resolve.calls.smartcasts.MultipleSmartCasts
import org.jetbrains.kotlin.resolve.calls.smartcasts.SingleSmartCast
import org.jetbrains.kotlin.resolve.calls.util.getParameterForArgument
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.util.getType
@@ -29,6 +30,7 @@ import org.jetbrains.kotlin.resolve.sam.SamConstructorDescriptor
import org.jetbrains.kotlin.resolve.sam.getFunctionTypeForAbstractMethod
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.checker.intersectWrappedTypes
import org.jetbrains.kotlin.types.typeUtil.makeNullable
class KtFe10ExpressionTypeProvider(
@@ -54,7 +56,12 @@ class KtFe10ExpressionTypeProvider(
}
val bindingContext = analysisContext.analyze(unwrapped, AnalysisMode.PARTIAL)
val kotlinType = expression.getType(bindingContext) ?: analysisContext.builtIns.unitType
val smartCastType = when (val smartCastType = bindingContext[BindingContext.SMARTCAST, expression]) {
is SingleSmartCast -> smartCastType.type
is MultipleSmartCasts -> intersectWrappedTypes(smartCastType.map.values)
else -> null
}
val kotlinType = smartCastType ?: expression.getType(bindingContext) ?: analysisContext.builtIns.unitType
return kotlinType.toKtType(analysisContext)
}
@@ -156,6 +156,30 @@ public class KtFe10HLExpressionTypeTestGenerated extends AbstractKtFe10HLExpress
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/returnExpression.kt");
}
@Test
@TestMetadata("smartcast_asCallArg.kt")
public void testSmartcast_asCallArg() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/smartcast_asCallArg.kt");
}
@Test
@TestMetadata("smartcast_asReceiver.kt")
public void testSmartcast_asReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/smartcast_asReceiver.kt");
}
@Test
@TestMetadata("smartcast_multi.kt")
public void testSmartcast_multi() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/smartcast_multi.kt");
}
@Test
@TestMetadata("smartcast_unused.kt")
public void testSmartcast_unused() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/smartcast_unused.kt");
}
@Test
@TestMetadata("stringLiteral.kt")
public void testStringLiteral() throws Exception {