SLC: use optimal type mapping mode for receiver parameter

...and other kinds of parameters as well
This commit is contained in:
Jinseong Jeon
2023-08-25 12:11:29 -07:00
committed by teamcity
parent 4cdc22c9cc
commit 421cd9929b
5 changed files with 31 additions and 13 deletions
@@ -10,6 +10,9 @@ import com.intellij.psi.*
import com.intellij.psi.search.LocalSearchScope
import com.intellij.psi.search.SearchScope
import com.intellij.util.IncorrectOperationException
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.api.types.KtTypeMappingMode
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.asJava.elements.*
import org.jetbrains.kotlin.light.classes.symbol.basicIsEquivalentTo
@@ -73,4 +76,14 @@ internal abstract class SymbolLightParameterBase(containingDeclaration: SymbolLi
abstract override fun hashCode(): Int
abstract override fun isVarArgs(): Boolean
context(KtAnalysisSession)
protected fun KtType.typeMappingMode(): KtTypeMappingMode {
return when {
this@typeMappingMode.isSuspendFunctionType -> KtTypeMappingMode.DEFAULT
// TODO: extract type mapping mode from annotation?
// TODO: methods with declaration site wildcards?
else -> KtTypeMappingMode.VALUE_PARAMETER
}
}
}
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtValueParameterSymbol
import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.analysis.api.symbols.psiSafe
import org.jetbrains.kotlin.analysis.api.symbols.sourcePsiSafe
import org.jetbrains.kotlin.analysis.api.types.KtTypeMappingMode
import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.asJava.elements.KtLightIdentifier
import org.jetbrains.kotlin.builtins.StandardNames
@@ -80,17 +79,11 @@ internal abstract class SymbolLightParameterCommon(
parameterSymbolPointer.withSymbol(ktModule) { parameterSymbol ->
val convertedType = run {
val ktType = parameterSymbol.returnType
val typeMappingMode = when {
ktType.isSuspendFunctionType -> KtTypeMappingMode.DEFAULT
// TODO: extract type mapping mode from annotation?
// TODO: methods with declaration site wildcards?
else -> KtTypeMappingMode.VALUE_PARAMETER
}
ktType.asPsiTypeElement(
this@SymbolLightParameterCommon,
allowErrorTypes = true,
typeMappingMode
ktType.typeMappingMode()
)?.let {
annotateByKtType(it.type, ktType, it, modifierList)
}
@@ -22,7 +22,12 @@ internal class SymbolLightParameterForDefaultImplsReceiver(containingDeclaration
SymbolLightParameterBase(containingDeclaration) {
private val _type by lazyPub {
(method.containingClass.containingClass as SymbolLightClassForInterface).withClassOrObjectSymbol {
it.buildSelfClassType().asPsiType(containingDeclaration, allowErrorTypes = true) ?: nonExistentType()
val ktType = it.buildSelfClassType()
ktType.asPsiType(
containingDeclaration,
allowErrorTypes = true,
ktType.typeMappingMode()
) ?: nonExistentType()
}
}
@@ -87,7 +87,11 @@ internal class SymbolLightParameterForReceiver private constructor(
private val _type: PsiType by lazyPub {
withReceiverSymbol { receiver ->
val ktType = receiver.type
val psiType = ktType.asPsiTypeElement(this, allowErrorTypes = true)?.let {
val psiType = ktType.asPsiTypeElement(
this,
allowErrorTypes = true,
ktType.typeMappingMode()
)?.let {
annotateByKtType(it.type, ktType, it, modifierList)
}
if (method is SymbolLightAnnotationsMethod) {
@@ -43,9 +43,12 @@ internal class SymbolLightSuspendContinuationParameter(
private val _type by lazyPub {
withFunctionSymbol { functionSymbol ->
buildClassType(StandardClassIds.Continuation) { argument(functionSymbol.returnType) }
.asPsiType(this, allowErrorTypes = true)
?: nonExistentType()
val ktType = buildClassType(StandardClassIds.Continuation) { argument(functionSymbol.returnType) }
ktType.asPsiType(
this,
allowErrorTypes = true,
ktType.typeMappingMode()
) ?: nonExistentType()
}
}