From 7a24d0ee82d92350685e532fa8bba009ef44a748 Mon Sep 17 00:00:00 2001 From: Dmitrii Gridin Date: Fri, 25 Aug 2023 16:20:54 +0200 Subject: [PATCH] [Analysis API] KtPsiTypeProvider: disable for non-jvm platforms Otherwise, it will throw exception like: ``` IllegalStateException: No 'FirJvmTypeMapper' ``` Anyway, PsiType is JVM conception, so it is not supposed to be used outside the JVM platform ^KT-60318 --- .../descriptors/components/KtFe10PsiTypeProvider.kt | 6 +++++- .../api/fir/components/KtFirPsiTypeProvider.kt | 7 ++++++- .../analysis/api/components/KtPsiTypeProvider.kt | 10 +++++++--- 3 files changed, 18 insertions(+), 5 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 ec066d35979..a1b45f6238b 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 @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -24,6 +24,8 @@ import org.jetbrains.kotlin.load.kotlin.TypeMappingMode import org.jetbrains.kotlin.load.kotlin.getOptimalModeForReturnType import org.jetbrains.kotlin.load.kotlin.getOptimalModeForValueParameter import org.jetbrains.kotlin.name.SpecialNames +import org.jetbrains.kotlin.platform.has +import org.jetbrains.kotlin.platform.jvm.JvmPlatform import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.model.SimpleTypeMarker import java.text.StringCharacterIterator @@ -51,6 +53,8 @@ internal class KtFe10PsiTypeProvider( } } + if (!analysisSession.useSiteModule.platform.has()) return null + return asPsiTypeElement(simplifyType(kotlinType), useSitePosition, mode.toTypeMappingMode(type, isAnnotationMethod)) } 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 06f442a9f50..71235de2f5a 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 @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.descriptors.java.JavaVisibilities import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.backend.jvm.jvmTypeMapper import org.jetbrains.kotlin.fir.declarations.FirResolvePhase +import org.jetbrains.kotlin.fir.moduleData import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.substitution.AbstractConeSubstitutor import org.jetbrains.kotlin.fir.resolve.toSymbol @@ -39,6 +40,8 @@ import org.jetbrains.kotlin.load.kotlin.TypeMappingMode import org.jetbrains.kotlin.load.kotlin.getOptimalModeForReturnType import org.jetbrains.kotlin.load.kotlin.getOptimalModeForValueParameter import org.jetbrains.kotlin.name.SpecialNames +import org.jetbrains.kotlin.platform.has +import org.jetbrains.kotlin.platform.jvm.JvmPlatform import org.jetbrains.kotlin.psi import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.psiUtil.parents @@ -64,6 +67,8 @@ internal class KtFirPsiTypeProvider( } } + if (!rootModuleSession.moduleData.platform.has()) return null + return coneType.simplifyType(rootModuleSession, useSitePosition) .asPsiTypeElement(rootModuleSession, mode.toTypeMappingMode(type, isAnnotationMethod), useSitePosition, allowErrorTypes) } 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 8bd3626b05c..4ee81bc5fb0 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 @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -39,7 +39,9 @@ public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn { * 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 + * If [allowErrorTypes] set to true then erroneous types will be replaced with `error.NonExistentClass` type. + * + * Note: [PsiTypeElement] is JVM conception, so this method will return `null` for non-JVM platforms. */ public fun KtType.asPsiTypeElement( useSitePosition: PsiElement, @@ -55,7 +57,9 @@ public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn { * * This simply unwraps [PsiTypeElement] returned from [asPsiTypeElement]. * Use this version if type annotation is not required. Otherwise, use [asPsiTypeElement] to get [PsiTypeElement] as an owner of - * annotations on [PsiType] and annotate the resulting [PsiType] with proper [PsiAnnotation]. + * annotations on [PsiType] and annotate the resulting [PsiType] with proper [PsiAnnotation][com.intellij.psi.PsiAnnotation]. + * + * Note: [PsiType] is JVM conception, so this method will return `null` for non-JVM platforms. */ public fun KtType.asPsiType( useSitePosition: PsiElement,