FE1.0 Analysis API: fix getReturnTypeForKtDeclaration for setter parameter

This commit is contained in:
Tianyu Geng
2021-11-18 15:06:17 -08:00
committed by Ilya Kirillov
parent f1d0791f15
commit 6e555e1d28
3 changed files with 26 additions and 19 deletions
@@ -99,6 +99,22 @@ class KtFe10ExpressionTypeProvider(
return kotlinType.toKtType(analysisContext)
}
// Manually handle custom setter parameter
if (declaration is KtParameter) {
val parameterList = declaration.parent as? KtParameterList
if (parameterList?.parameters?.singleOrNull() == declaration) {
val propertyAccessor = parameterList.parent as? KtPropertyAccessor
val property = propertyAccessor?.parent as? KtProperty
if (property != null && property.setter == propertyAccessor) {
val bindingContext = analysisContext.analyze(property)
val kotlinType = bindingContext[BindingContext.VARIABLE, property]?.returnType
?: ErrorUtils.createErrorType("Return type for property \"${declaration.name}\" cannot be resolved")
return kotlinType.toKtType(analysisContext)
}
}
}
return analysisContext.builtIns.unitType.toKtType(analysisContext)
}
@@ -19,23 +19,23 @@ abstract class AbstractDeclarationReturnTypeTest(
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
val actual = buildString {
ktFile.accept(object : KtTreeVisitor<Int>() {
override fun visitDeclaration(dcl: KtDeclaration, data: Int): Void? {
if (dcl is KtTypeParameter) return null
append(" ".repeat(data))
if (dcl is KtClassLikeDeclaration) {
appendLine(dcl.getNameWithPositionString())
override fun visitDeclaration(dclaration: KtDeclaration, indent: Int): Void? {
if (dclaration is KtTypeParameter) return null
append(" ".repeat(indent))
if (dclaration is KtClassLikeDeclaration) {
appendLine(dclaration.getNameWithPositionString())
} else {
analyseForTest(dcl) {
val returnType = dcl.getReturnKtType()
append(dcl.getNameWithPositionString())
analyseForTest(dclaration) {
val returnType = dclaration.getReturnKtType()
append(dclaration.getNameWithPositionString())
append(" : ")
appendLine(returnType.render())
}
}
return super.visitDeclaration(dcl, data + 2)
return super.visitDeclaration(dclaration, indent + 2)
}
}, 0)
}
testServices.assertions.assertEqualsToTestDataFileSibling(actual)
}
}
}
@@ -1,9 +0,0 @@
i@(1,1) : kotlin.Int
KtPropertyAccessor@(2,5) : kotlin.Int
KtPropertyAccessor@(3,5) : kotlin.Unit
value@(3,9) : kotlin.Unit
A@(5,1)
j@(6,5) : kotlin.Int
KtPropertyAccessor@(7,9) : kotlin.Int
KtPropertyAccessor@(8,9) : kotlin.Unit
value@(8,13) : kotlin.Unit