[Analysis API] KtPsiTypeProvider: drop asPsiTypeElement method

This was an implementation detail of SLC.
In the case, if we want to have proper parents for type annotations
in light classes, we have to properly implement `PsiTypeElement`
for required declarations.

^KT-66603
^KT-56870
This commit is contained in:
Dmitrii Gridin
2024-03-15 20:26:13 +01:00
committed by Space Team
parent 2fbde7a85c
commit e1bf85f790
7 changed files with 33 additions and 132 deletions
@@ -40,14 +40,15 @@ internal class KtFe10PsiTypeProvider(
private val typeMapper by lazy { KtFe10JvmTypeMapperContext(analysisContext.resolveSession) }
override fun asPsiTypeElement(
override fun asPsiType(
type: KtType,
useSitePosition: PsiElement,
allowErrorTypes: Boolean,
mode: KtTypeMappingMode,
isAnnotationMethod: Boolean,
suppressWildcards: Boolean?,
allowErrorTypes: Boolean,
): PsiTypeElement? {
preserveAnnotations: Boolean
): PsiType? {
val kotlinType = (type as KtFe10Type).fe10Type
with(typeMapper.typeContext) {
@@ -58,29 +59,14 @@ internal class KtFe10PsiTypeProvider(
if (!analysisSession.useSiteModule.platform.has<JvmPlatform>()) return null
return asPsiTypeElement(
val typeElement = asPsiTypeElement(
simplifyType(kotlinType),
useSitePosition,
mode.toTypeMappingMode(type, isAnnotationMethod, suppressWildcards),
)
}
override fun asPsiType(
type: KtType,
useSitePosition: PsiElement,
allowErrorTypes: Boolean,
mode: KtTypeMappingMode,
isAnnotationMethod: Boolean,
suppressWildcards: Boolean?,
preserveAnnotations: Boolean
): PsiType? = asPsiTypeElement(
type = type,
useSitePosition = useSitePosition,
mode = mode,
isAnnotationMethod = isAnnotationMethod,
suppressWildcards = suppressWildcards,
allowErrorTypes = allowErrorTypes,
)?.type
return typeElement?.type
}
private fun KtTypeMappingMode.toTypeMappingMode(
type: KtType,
@@ -70,15 +70,15 @@ internal class KtFirPsiTypeProvider(
override val analysisSession: KtFirAnalysisSession,
override val token: KtLifetimeToken,
) : KtPsiTypeProvider(), KtFirAnalysisSessionComponent {
override fun asPsiTypeElement(
override fun asPsiType(
type: KtType,
useSitePosition: PsiElement,
allowErrorTypes: Boolean,
mode: KtTypeMappingMode,
isAnnotationMethod: Boolean,
suppressWildcards: Boolean?,
allowErrorTypes: Boolean,
): PsiTypeElement? {
preserveAnnotations: Boolean,
): PsiType? {
val coneType = type.coneType
with(rootModuleSession.typeContext) {
@@ -89,30 +89,10 @@ internal class KtFirPsiTypeProvider(
if (!rootModuleSession.moduleData.platform.has<JvmPlatform>()) return null
return coneType.simplifyType(rootModuleSession, useSitePosition)
.asPsiTypeElement(
rootModuleSession,
mode.toTypeMappingMode(type, isAnnotationMethod, suppressWildcards),
useSitePosition,
allowErrorTypes,
)
}
override fun asPsiType(
type: KtType,
useSitePosition: PsiElement,
allowErrorTypes: Boolean,
mode: KtTypeMappingMode,
isAnnotationMethod: Boolean,
suppressWildcards: Boolean?,
preserveAnnotations: Boolean,
): PsiType? {
val typeElement = asPsiTypeElement(
type = type,
val typeElement = coneType.simplifyType(rootModuleSession, useSitePosition).asPsiTypeElement(
session = rootModuleSession,
mode = mode.toTypeMappingMode(type, isAnnotationMethod, suppressWildcards),
useSitePosition = useSitePosition,
mode = mode,
isAnnotationMethod = isAnnotationMethod,
suppressWildcards = suppressWildcards,
allowErrorTypes = allowErrorTypes,
) ?: return null
@@ -125,7 +105,6 @@ internal class KtFirPsiTypeProvider(
psiType = psiType,
ktType = type,
psiContext = typeElement,
modifierListAsParent = null,
)
}
}
@@ -7,21 +7,11 @@ package org.jetbrains.kotlin.analysis.api.components
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiType
import com.intellij.psi.PsiTypeElement
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.api.types.KtTypeMappingMode
public abstract class KtPsiTypeProvider : KtAnalysisSessionComponent() {
public abstract fun asPsiTypeElement(
type: KtType,
useSitePosition: PsiElement,
mode: KtTypeMappingMode,
isAnnotationMethod: Boolean,
suppressWildcards: Boolean?,
allowErrorTypes: Boolean,
): PsiTypeElement?
public abstract fun asPsiType(
type: KtType,
useSitePosition: PsiElement,
@@ -34,58 +24,11 @@ public abstract class KtPsiTypeProvider : KtAnalysisSessionComponent() {
public abstract fun asKtType(
psiType: PsiType,
useSitePosition: PsiElement
useSitePosition: PsiElement,
): KtType?
}
public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn {
/**
* Converts the given [KtType] to [PsiTypeElement] under [useSitePosition] context.
*
* [useSitePosition] is used as the parent of the resulting [PsiTypeElement],
* which is in turn used to resolve [PsiTypeElement].
*
* [useSitePosition] is also used to determine if the given [KtType] needs to be approximated.
* For example, if the given type is local yet available in the same scope of use site, we can
* still use such local type. Otherwise, e.g., exposed to public as a return type, the resulting
* type will be approximated accordingly.
*
* If [allowErrorTypes] set to false then method returns `null` if the conversion encounters any
* erroneous cases, e.g., errors in type arguments.
* A client can handle such case in its own way. For instance,
* * UAST will return `UastErrorType` as a default error type.
*
* If [allowErrorTypes] set to true then erroneous types will be replaced with `error.NonExistentClass` type.
*
* [suppressWildcards] indicates whether wild cards in type arguments need to be suppressed or not,
* e.g., according to the annotation on the containing declarations.
* `true` means they should be suppressed;
* `false` means they should appear;
* `null` is no-op by default, i.e., their suppression/appearance is determined by type annotations.
*
* Note: [PsiTypeElement] is JVM conception, so this method will return `null` for non-JVM platforms.
*
* @return [PsiTypeElement] without type annotations if mapping is successful
*
* @see asPsiType
*/
public fun KtType.asPsiTypeElement(
useSitePosition: PsiElement,
allowErrorTypes: Boolean,
mode: KtTypeMappingMode = KtTypeMappingMode.DEFAULT,
isAnnotationMethod: Boolean = false,
suppressWildcards: Boolean? = null,
): PsiTypeElement? = withValidityAssertion {
analysisSession.psiTypeProvider.asPsiTypeElement(
type = this,
useSitePosition = useSitePosition,
mode = mode,
isAnnotationMethod = isAnnotationMethod,
suppressWildcards = suppressWildcards,
allowErrorTypes = allowErrorTypes,
)
}
/**
* Converts the given [KtType] to [PsiType] under [useSitePosition] context.
*
@@ -98,7 +41,14 @@ public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn {
* we can still use such a local type.
* Otherwise, e.g., exposed to public as a return type, the resulting type will be approximated accordingly.
*
* @param allowErrorTypes if **false** the result will be null in the case of an error type inside the [type][this]
* @param allowErrorTypes if **false** the result will be null in the case of an error type inside the [type][this].
* Erroneous types will be replaced with `error.NonExistentClass` type.
*
* @param suppressWildcards indicates whether wild cards in type arguments need to be suppressed or not,
* e.g., according to the annotation on the containing declarations.
* - `true` means they should be suppressed.
* - `false` means they should appear.
* - `null` is no-op by default, i.e., their suppression/appearance is determined by type annotations.
*
* @param preserveAnnotations if **true** the result [PsiType] will have converted annotations from the original [type][this]
*/
@@ -167,14 +167,13 @@ fun annotateByKtType(
psiType: PsiType,
ktType: KtType,
psiContext: PsiTypeElement,
modifierListAsParent: PsiModifierList?,
): PsiType {
fun KtType.getAnnotationsSequence(modifierList: PsiModifierList?): Sequence<List<PsiAnnotation>> = sequence {
fun KtType.getAnnotationsSequence(): Sequence<List<PsiAnnotation>> = sequence {
yield(
annotations.map { annoApp ->
SymbolLightSimpleAnnotation(
annoApp.classId?.asFqNameString(),
modifierList ?: psiContext,
psiContext,
annoApp.arguments,
annoApp.psi,
)
@@ -183,10 +182,10 @@ fun annotateByKtType(
(this@getAnnotationsSequence as? KtNonErrorClassType)?.ownTypeArguments?.forEach { typeProjection ->
typeProjection.type?.let {
yieldAll(it.getAnnotationsSequence(modifierList = null))
yieldAll(it.getAnnotationsSequence())
}
}
}
return psiType.annotateByTypeAnnotationProvider(ktType.getAnnotationsSequence(modifierListAsParent))
return psiType.annotateByTypeAnnotationProvider(ktType.getAnnotationsSequence())
}
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.light.classes.symbol.methods
import com.intellij.psi.*
import kotlinx.collections.immutable.PersistentMap
import kotlinx.collections.immutable.mutate
import org.jetbrains.kotlin.analysis.api.KtAnalysisNonPublicApi
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.annotations.hasAnnotation
import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionSymbol
@@ -230,16 +229,13 @@ internal class SymbolLightSimpleMethod(
else
KtTypeMappingMode.RETURN_TYPE
ktType.asPsiTypeElement(
ktType.asPsiType(
this@SymbolLightSimpleMethod,
allowErrorTypes = true,
typeMappingMode,
this@SymbolLightSimpleMethod.containingClass.isAnnotationType,
suppressWildcards = suppressWildcards(),
)?.let {
@OptIn(KtAnalysisNonPublicApi::class)
annotateByKtType(it.type, ktType, it, modifierList)
}
)
} ?: nonExistentType()
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.light.classes.symbol.parameters
import com.intellij.psi.*
import org.jetbrains.kotlin.analysis.api.KtAnalysisNonPublicApi
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.symbols.KtValueParameterSymbol
import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtSymbolPointer
@@ -15,7 +14,6 @@ import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.asJava.elements.KtLightIdentifier
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.light.classes.symbol.*
import org.jetbrains.kotlin.light.classes.symbol.annotations.annotateByKtType
import org.jetbrains.kotlin.light.classes.symbol.annotations.suppressWildcardMode
import org.jetbrains.kotlin.light.classes.symbol.methods.SymbolLightMethodBase
import org.jetbrains.kotlin.psi.KtParameter
@@ -79,15 +77,12 @@ internal abstract class SymbolLightParameterCommon(
val convertedType = run {
val ktType = parameterSymbol.returnType
ktType.asPsiTypeElement(
ktType.asPsiType(
this@SymbolLightParameterCommon,
allowErrorTypes = true,
ktType.typeMappingMode(),
suppressWildcards = parameterSymbol.suppressWildcardMode(),
)?.let {
@OptIn(KtAnalysisNonPublicApi::class)
annotateByKtType(it.type, ktType, it, modifierList)
}
)
} ?: nonExistentType()
if (isDeclaredAsVararg()) {
@@ -9,7 +9,6 @@ import com.intellij.psi.PsiIdentifier
import com.intellij.psi.PsiModifierList
import com.intellij.psi.PsiType
import com.intellij.psi.util.TypeConversionUtil
import org.jetbrains.kotlin.analysis.api.KtAnalysisNonPublicApi
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtReceiverParameterSymbol
@@ -88,15 +87,12 @@ internal class SymbolLightParameterForReceiver private constructor(
private val _type: PsiType by lazyPub {
withReceiverSymbol { receiver ->
val ktType = receiver.type
val psiType = ktType.asPsiTypeElement(
val psiType = ktType.asPsiType(
this,
allowErrorTypes = true,
ktType.typeMappingMode(),
suppressWildcards = receiver.suppressWildcard() ?: method.suppressWildcards(),
)?.let {
@OptIn(KtAnalysisNonPublicApi::class)
annotateByKtType(it.type, ktType, it, modifierList)
}
)
if (method is SymbolLightAnnotationsMethod) {
val erased = TypeConversionUtil.erasure(psiType)