[FIR AA] Use resolvedType instead of coneTypeOrNull where applicable

#KT-61367 Fixed
This commit is contained in:
Kirill Rakhman
2023-09-11 17:41:10 +02:00
committed by Space Team
parent 6d8091f0ff
commit aebb3bf0ee
12 changed files with 32 additions and 25 deletions
@@ -23,13 +23,7 @@ import org.jetbrains.kotlin.fir.declarations.resolvePhase
import org.jetbrains.kotlin.fir.declarations.resolved import org.jetbrains.kotlin.fir.declarations.resolved
import org.jetbrains.kotlin.fir.declarations.toAnnotationClassId import org.jetbrains.kotlin.fir.declarations.toAnnotationClassId
import org.jetbrains.kotlin.fir.declarations.toAnnotationClassIdSafe import org.jetbrains.kotlin.fir.declarations.toAnnotationClassIdSafe
import org.jetbrains.kotlin.fir.expressions.FirAnnotation import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.expressions.arguments
import org.jetbrains.kotlin.fir.expressions.calleeReference
import org.jetbrains.kotlin.fir.expressions.unwrapAndFlattenArgument
import org.jetbrains.kotlin.fir.references.toResolvedCallableSymbol import org.jetbrains.kotlin.fir.references.toResolvedCallableSymbol
import org.jetbrains.kotlin.fir.resolve.transformers.plugin.CompilerRequiredAnnotationsHelper import org.jetbrains.kotlin.fir.resolve.transformers.plugin.CompilerRequiredAnnotationsHelper
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
@@ -51,6 +45,7 @@ internal fun mapAnnotationParameters(annotation: FirAnnotation): Map<Name, FirEx
checkWithAttachment(annotation.resolved, { "By now the annotations argument mapping should have been resolved" }) { checkWithAttachment(annotation.resolved, { "By now the annotations argument mapping should have been resolved" }) {
withFirEntry("annotation", annotation) withFirEntry("annotation", annotation)
withClassEntry("annotationTypeRef", annotation.annotationTypeRef) withClassEntry("annotationTypeRef", annotation.annotationTypeRef)
@OptIn(UnresolvedExpressionTypeAccess::class)
withClassEntry("coneTypeOrNull", annotation.coneTypeOrNull) withClassEntry("coneTypeOrNull", annotation.coneTypeOrNull)
annotation.calleeReference?.let { withClassEntry("calleeReference", it) } annotation.calleeReference?.let { withClassEntry("calleeReference", it) }
} }
@@ -415,7 +415,7 @@ internal class KtFirCallResolver(
} }
// Specially handle @ExtensionFunctionType // Specially handle @ExtensionFunctionType
if (dispatchReceiver?.coneTypeSafe<ConeKotlinType>()?.isExtensionFunctionType == true) { if (dispatchReceiver?.resolvedType?.isExtensionFunctionType == true) {
firstArgIsExtensionReceiver = true firstArgIsExtensionReceiver = true
} }
@@ -923,7 +923,7 @@ internal class KtFirCallResolver(
private fun FirArrayLiteral.toTypeArgumentsMapping( private fun FirArrayLiteral.toTypeArgumentsMapping(
partiallyAppliedSymbol: KtPartiallyAppliedSymbol<*, *> partiallyAppliedSymbol: KtPartiallyAppliedSymbol<*, *>
): Map<KtTypeParameterSymbol, KtType> { ): Map<KtTypeParameterSymbol, KtType> {
val elementType = coneTypeSafe<ConeClassLikeType>()?.arrayElementType()?.asKtType() ?: return emptyMap() val elementType = resolvedType.arrayElementType()?.asKtType() ?: return emptyMap()
val typeParameter = partiallyAppliedSymbol.symbol.typeParameters.singleOrNull() ?: return emptyMap() val typeParameter = partiallyAppliedSymbol.symbol.typeParameters.singleOrNull() ?: return emptyMap()
return mapOf(typeParameter to elementType) return mapOf(typeParameter to elementType)
} }
@@ -1157,6 +1157,8 @@ internal class KtFirCallResolver(
private fun FirArrayLiteral.toKtCallInfo(): KtCallInfo? { private fun FirArrayLiteral.toKtCallInfo(): KtCallInfo? {
val arrayOfSymbol = with(analysisSession) { val arrayOfSymbol = with(analysisSession) {
@OptIn(UnresolvedExpressionTypeAccess::class)
val type = coneTypeSafe<ConeClassLikeType>() val type = coneTypeSafe<ConeClassLikeType>()
?: return run { ?: return run {
val defaultArrayOfSymbol = arrayOfSymbol(arrayOf) ?: return null val defaultArrayOfSymbol = arrayOfSymbol(arrayOf) ?: return null
@@ -1202,6 +1204,8 @@ internal class KtFirCallResolver(
val firSymbol = arrayOfSymbol.firSymbol val firSymbol = arrayOfSymbol.firSymbol
// No type parameter means this is an arrayOf call of primitives, in which case there is no type arguments // No type parameter means this is an arrayOf call of primitives, in which case there is no type arguments
val typeParameter = firSymbol.fir.typeParameters.singleOrNull() ?: return KtSubstitutor.Empty(token) val typeParameter = firSymbol.fir.typeParameters.singleOrNull() ?: return KtSubstitutor.Empty(token)
@OptIn(UnresolvedExpressionTypeAccess::class)
val elementType = coneTypeSafe<ConeClassLikeType>()?.arrayElementType() ?: return KtSubstitutor.Empty(token) val elementType = coneTypeSafe<ConeClassLikeType>()?.arrayElementType() ?: return KtSubstitutor.Empty(token)
val coneSubstitutor = substitutorByMap(mapOf(typeParameter.symbol to elementType), rootModuleSession) val coneSubstitutor = substitutorByMap(mapOf(typeParameter.symbol to elementType), rootModuleSession)
return firSymbolBuilder.typeBuilder.buildSubstitutor(coneSubstitutor) return firSymbolBuilder.typeBuilder.buildSubstitutor(coneSubstitutor)
@@ -72,7 +72,7 @@ internal class KtFirExpressionTypeProvider(
// For unresolved `super`, we manually create an intersection type so that IDE features like completion can work correctly. // For unresolved `super`, we manually create an intersection type so that IDE features like completion can work correctly.
val containingClass = (fir.dispatchReceiver as? FirThisReceiverExpression)?.calleeReference?.boundSymbol as? FirClassSymbol<*> val containingClass = (fir.dispatchReceiver as? FirThisReceiverExpression)?.calleeReference?.boundSymbol as? FirClassSymbol<*>
if (fir.calleeReference is FirSuperReference && fir.coneTypeOrNull is ConeErrorType && containingClass != null) { if (fir.calleeReference is FirSuperReference && fir.resolvedType is ConeErrorType && containingClass != null) {
val superTypes = containingClass.resolvedSuperTypes val superTypes = containingClass.resolvedSuperTypes
when (superTypes.size) { when (superTypes.size) {
0 -> analysisSession.builtinTypes.ANY 0 -> analysisSession.builtinTypes.ANY
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.coneTypeSafe import org.jetbrains.kotlin.fir.types.coneTypeSafe
import org.jetbrains.kotlin.fir.types.isStableSmartcast import org.jetbrains.kotlin.fir.types.isStableSmartcast
import org.jetbrains.kotlin.fir.types.resolvedType
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
@@ -105,7 +106,7 @@ internal class KtFirSmartcastProvider(
if (receiver == null || receiver == firExpression.explicitReceiver) return null if (receiver == null || receiver == firExpression.explicitReceiver) return null
if (!receiver.isStableSmartcast()) return null if (!receiver.isStableSmartcast()) return null
val type = receiver.coneTypeSafe<ConeKotlinType>()?.asKtType() ?: return null val type = receiver.resolvedType.asKtType()
return KtImplicitReceiverSmartCast(type, kind, token) return KtImplicitReceiverSmartCast(type, kind, token)
} }
} }
@@ -45,6 +45,8 @@ internal object FirAnnotationValueConverter {
private fun <T> FirConstExpression<T>.convertConstantExpression(): KtConstantAnnotationValue? { private fun <T> FirConstExpression<T>.convertConstantExpression(): KtConstantAnnotationValue? {
val expression = psi as? KtElement val expression = psi as? KtElement
@OptIn(UnresolvedExpressionTypeAccess::class)
val type = coneTypeOrNull val type = coneTypeOrNull
val constantValue = when { val constantValue = when {
value == null -> KtConstantValue.KtNullConstantValue(expression) value == null -> KtConstantValue.KtNullConstantValue(expression)
@@ -178,19 +178,19 @@ internal object FirCompileTimeConstantEvaluator {
val opr1 = evaluate(functionCall.explicitReceiver, mode) ?: return null val opr1 = evaluate(functionCall.explicitReceiver, mode) ?: return null
opr1.evaluate(function)?.let { opr1.evaluate(function)?.let {
return it.adjustType(functionCall.coneTypeOrNull) return it.adjustType(functionCall.resolvedType)
} }
val argument = functionCall.arguments.firstOrNull() ?: return null val argument = functionCall.arguments.firstOrNull() ?: return null
val opr2 = evaluate(argument, mode) ?: return null val opr2 = evaluate(argument, mode) ?: return null
opr1.evaluate(function, opr2)?.let { opr1.evaluate(function, opr2)?.let {
return it.adjustType(functionCall.coneTypeOrNull) return it.adjustType(functionCall.resolvedType)
} }
return null return null
} }
private fun FirConstExpression<*>.adjustType(expectedType: ConeKotlinType?): FirConstExpression<*> { private fun FirConstExpression<*>.adjustType(expectedType: ConeKotlinType): FirConstExpression<*> {
val expectedKind = expectedType?.toConstantValueKind() val expectedKind = expectedType.toConstantValueKind()
// Note that the resolved type for the const expression is not always matched with the const kind. For example, // Note that the resolved type for the const expression is not always matched with the const kind. For example,
// fun foo(x: Int) { // fun foo(x: Int) {
// when (x) { // when (x) {
@@ -422,7 +422,7 @@ internal object FirReferenceResolveHelper {
// accessing the `super` property on `this`, hence this weird looking if condition. In addition, the current class type is available // accessing the `super` property on `this`, hence this weird looking if condition. In addition, the current class type is available
// from the dispatch receiver `this`. // from the dispatch receiver `this`.
if (expression is KtLabelReferenceExpression && fir is FirPropertyAccessExpression && fir.calleeReference is FirSuperReference) { if (expression is KtLabelReferenceExpression && fir is FirPropertyAccessExpression && fir.calleeReference is FirSuperReference) {
return listOfNotNull(fir.dispatchReceiver?.coneTypeOrNull?.toTargetSymbol(session, symbolBuilder)) return listOfNotNull(fir.dispatchReceiver?.resolvedType?.toTargetSymbol(session, symbolBuilder))
} }
val receiverOrImplicitInvoke = if (fir is FirImplicitInvokeCall) { val receiverOrImplicitInvoke = if (fir is FirImplicitInvokeCall) {
fir.explicitReceiver?.unwrapSmartcastExpression() fir.explicitReceiver?.unwrapSmartcastExpression()
@@ -733,7 +733,7 @@ internal object FirReferenceResolveHelper {
session: FirSession, session: FirSession,
symbolBuilder: KtSymbolByFirBuilder symbolBuilder: KtSymbolByFirBuilder
): Collection<KtSymbol> { ): Collection<KtSymbol> {
val type = fir.coneTypeOrNull ?: return emptyList() val type = fir.resolvedType
return listOfNotNull(type.toTargetSymbol(session, symbolBuilder)) return listOfNotNull(type.toTargetSymbol(session, symbolBuilder))
} }
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.analysis.api.fir.symbols.KtFirArrayOfSymbolProvider.
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFirSafe import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFirSafe
import org.jetbrains.kotlin.fir.expressions.FirArrayLiteral import org.jetbrains.kotlin.fir.expressions.FirArrayLiteral
import org.jetbrains.kotlin.fir.expressions.UnresolvedExpressionTypeAccess
import org.jetbrains.kotlin.fir.types.ConeClassLikeType import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.coneTypeSafe import org.jetbrains.kotlin.fir.types.coneTypeSafe
@@ -24,6 +25,8 @@ class KtFirCollectionLiteralReference(
override fun KtAnalysisSession.resolveToSymbols(): Collection<KtSymbol> { override fun KtAnalysisSession.resolveToSymbols(): Collection<KtSymbol> {
check(this is KtFirAnalysisSession) check(this is KtFirAnalysisSession)
val fir = element.getOrBuildFirSafe<FirArrayLiteral>(firResolveSession) ?: return emptyList() val fir = element.getOrBuildFirSafe<FirArrayLiteral>(firResolveSession) ?: return emptyList()
@OptIn(UnresolvedExpressionTypeAccess::class)
val type = fir.coneTypeSafe<ConeClassLikeType>() ?: return listOfNotNull(arrayOfSymbol(arrayOf)) val type = fir.coneTypeSafe<ConeClassLikeType>() ?: return listOfNotNull(arrayOfSymbol(arrayOf))
val call = arrayTypeToArrayOfCall[type.lookupTag.classId] ?: arrayOf val call = arrayTypeToArrayOfCall[type.lookupTag.classId] ?: arrayOf
return listOfNotNull(arrayOfSymbol(call)) return listOfNotNull(arrayOfSymbol(call))
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.resolvedType
import org.jetbrains.kotlin.fir.types.toSymbol import org.jetbrains.kotlin.fir.types.toSymbol
import org.jetbrains.kotlin.fir.visitors.FirDefaultVisitorVoid import org.jetbrains.kotlin.fir.visitors.FirDefaultVisitorVoid
import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.name.StandardClassIds
@@ -111,7 +112,7 @@ private class CodeFragmentCapturedValueVisitor(
private fun processElement(element: FirElement) { private fun processElement(element: FirElement) {
if (element is FirExpression) { if (element is FirExpression) {
val symbol = element.coneTypeOrNull?.toSymbol(session) val symbol = element.resolvedType.toSymbol(session)
if (symbol != null) { if (symbol != null) {
registerFile(symbol) registerFile(symbol)
} }
@@ -290,4 +291,4 @@ private class CodeFragmentCapturedValueVisitor(
private val FirBasedSymbol<*>.isMutated: Boolean private val FirBasedSymbol<*>.isMutated: Boolean
get() = assignmentLhs.lastOrNull() == this get() = assignmentLhs.lastOrNull() == this
} }
@@ -15,10 +15,7 @@ import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.builder.buildConstExpression import org.jetbrains.kotlin.fir.expressions.builder.buildConstExpression
import org.jetbrains.kotlin.fir.psi import org.jetbrains.kotlin.fir.psi
import org.jetbrains.kotlin.fir.references.* import org.jetbrains.kotlin.fir.references.*
import org.jetbrains.kotlin.fir.types.FirErrorTypeRef import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.FirUserTypeRef
import org.jetbrains.kotlin.fir.types.impl.FirResolvedTypeRefImpl import org.jetbrains.kotlin.fir.types.impl.FirResolvedTypeRefImpl
import org.jetbrains.kotlin.fir.visitors.FirVisitor import org.jetbrains.kotlin.fir.visitors.FirVisitor
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
@@ -218,7 +215,7 @@ internal open class FirElementsRecorder : FirVisitor<Unit, MutableMap<KtElement,
convertedValue as T, convertedValue as T,
setType = false setType = false
).also { ).also {
it.replaceConeTypeOrNull(original.coneTypeOrNull) it.replaceConeTypeOrNull(original.resolvedType)
} }
} }
@@ -47,6 +47,7 @@ import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
import org.jetbrains.kotlin.fir.resolve.dfa.cfg.isUsedInControlFlowGraphBuilderForClass import org.jetbrains.kotlin.fir.resolve.dfa.cfg.isUsedInControlFlowGraphBuilderForClass
import org.jetbrains.kotlin.fir.resolve.dfa.cfg.isUsedInControlFlowGraphBuilderForFile import org.jetbrains.kotlin.fir.resolve.dfa.cfg.isUsedInControlFlowGraphBuilderForFile
import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.isResolved
import org.jetbrains.kotlin.psi.KtCodeFragment import org.jetbrains.kotlin.psi.KtCodeFragment
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.utils.exceptions.requireWithAttachment import org.jetbrains.kotlin.utils.exceptions.requireWithAttachment
@@ -440,7 +441,7 @@ private val FirFunction.isCertainlyResolved: Boolean
} }
val body = this.body ?: return false // Not completely sure val body = this.body ?: return false // Not completely sure
return body !is FirLazyBlock && body.coneTypeOrNull != null return body !is FirLazyBlock && body.isResolved
} }
private val FirVariable.initializerIfUnresolved: FirExpression? private val FirVariable.initializerIfUnresolved: FirExpression?
@@ -3,6 +3,8 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@file:OptIn(UnresolvedExpressionTypeAccess::class)
package org.jetbrains.kotlin.analysis.low.level.api.fir.util package org.jetbrains.kotlin.analysis.low.level.api.fir.util
import org.jetbrains.kotlin.utils.exceptions.ExceptionAttachmentBuilder import org.jetbrains.kotlin.utils.exceptions.ExceptionAttachmentBuilder
@@ -17,6 +19,7 @@ import org.jetbrains.kotlin.fir.declarations.utils.isActual
import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirResolvable import org.jetbrains.kotlin.fir.expressions.FirResolvable
import org.jetbrains.kotlin.fir.expressions.UnresolvedExpressionTypeAccess
import org.jetbrains.kotlin.fir.expressions.impl.FirResolvedArgumentList import org.jetbrains.kotlin.fir.expressions.impl.FirResolvedArgumentList
import org.jetbrains.kotlin.fir.references.* import org.jetbrains.kotlin.fir.references.*
import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeKotlinType