From e1bf85f790756eea95e8deeed1353de174a8d686 Mon Sep 17 00:00:00 2001 From: Dmitrii Gridin Date: Fri, 15 Mar 2024 20:26:13 +0100 Subject: [PATCH] [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 --- .../components/KtFe10PsiTypeProvider.kt | 28 ++------ .../fir/components/KtFirPsiTypeProvider.kt | 35 ++-------- .../api/components/KtPsiTypeProvider.kt | 68 +++---------------- .../annotations/symbolAnnotationsUtils.kt | 9 ++- .../symbol/methods/SymbolLightSimpleMethod.kt | 8 +-- .../parameters/SymbolLightParameterCommon.kt | 9 +-- .../SymbolLightParameterForReceiver.kt | 8 +-- 7 files changed, 33 insertions(+), 132 deletions(-) diff --git a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10PsiTypeProvider.kt b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10PsiTypeProvider.kt index c0167fee7ba..ff3ea844bd0 100644 --- a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10PsiTypeProvider.kt +++ b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10PsiTypeProvider.kt @@ -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()) 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, diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirPsiTypeProvider.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirPsiTypeProvider.kt index 7fbfc422db7..649bc32067d 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirPsiTypeProvider.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirPsiTypeProvider.kt @@ -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()) 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, ) } } diff --git a/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtPsiTypeProvider.kt b/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtPsiTypeProvider.kt index 7235ac6c01a..a1134708a38 100644 --- a/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtPsiTypeProvider.kt +++ b/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtPsiTypeProvider.kt @@ -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] */ diff --git a/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/annotations/symbolAnnotationsUtils.kt b/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/annotations/symbolAnnotationsUtils.kt index cb85f88db4a..1d7673c1c06 100644 --- a/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/annotations/symbolAnnotationsUtils.kt +++ b/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/annotations/symbolAnnotationsUtils.kt @@ -167,14 +167,13 @@ fun annotateByKtType( psiType: PsiType, ktType: KtType, psiContext: PsiTypeElement, - modifierListAsParent: PsiModifierList?, ): PsiType { - fun KtType.getAnnotationsSequence(modifierList: PsiModifierList?): Sequence> = sequence { + fun KtType.getAnnotationsSequence(): Sequence> = 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()) } diff --git a/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/methods/SymbolLightSimpleMethod.kt b/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/methods/SymbolLightSimpleMethod.kt index e46425f2bb7..c6a1c9c64e7 100644 --- a/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/methods/SymbolLightSimpleMethod.kt +++ b/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/methods/SymbolLightSimpleMethod.kt @@ -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() } diff --git a/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/parameters/SymbolLightParameterCommon.kt b/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/parameters/SymbolLightParameterCommon.kt index aa4e95f96ad..e6800c40b27 100644 --- a/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/parameters/SymbolLightParameterCommon.kt +++ b/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/parameters/SymbolLightParameterCommon.kt @@ -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()) { diff --git a/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/parameters/SymbolLightParameterForReceiver.kt b/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/parameters/SymbolLightParameterForReceiver.kt index 32ce1c8e931..f7a8f450d02 100644 --- a/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/parameters/SymbolLightParameterForReceiver.kt +++ b/analysis/symbol-light-classes/src/org/jetbrains/kotlin/light/classes/symbol/parameters/SymbolLightParameterForReceiver.kt @@ -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)