[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.toAnnotationClassId
import org.jetbrains.kotlin.fir.declarations.toAnnotationClassIdSafe
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
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.expressions.*
import org.jetbrains.kotlin.fir.references.toResolvedCallableSymbol
import org.jetbrains.kotlin.fir.resolve.transformers.plugin.CompilerRequiredAnnotationsHelper
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" }) {
withFirEntry("annotation", annotation)
withClassEntry("annotationTypeRef", annotation.annotationTypeRef)
@OptIn(UnresolvedExpressionTypeAccess::class)
withClassEntry("coneTypeOrNull", annotation.coneTypeOrNull)
annotation.calleeReference?.let { withClassEntry("calleeReference", it) }
}
@@ -415,7 +415,7 @@ internal class KtFirCallResolver(
}
// Specially handle @ExtensionFunctionType
if (dispatchReceiver?.coneTypeSafe<ConeKotlinType>()?.isExtensionFunctionType == true) {
if (dispatchReceiver?.resolvedType?.isExtensionFunctionType == true) {
firstArgIsExtensionReceiver = true
}
@@ -923,7 +923,7 @@ internal class KtFirCallResolver(
private fun FirArrayLiteral.toTypeArgumentsMapping(
partiallyAppliedSymbol: KtPartiallyAppliedSymbol<*, *>
): 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()
return mapOf(typeParameter to elementType)
}
@@ -1157,6 +1157,8 @@ internal class KtFirCallResolver(
private fun FirArrayLiteral.toKtCallInfo(): KtCallInfo? {
val arrayOfSymbol = with(analysisSession) {
@OptIn(UnresolvedExpressionTypeAccess::class)
val type = coneTypeSafe<ConeClassLikeType>()
?: return run {
val defaultArrayOfSymbol = arrayOfSymbol(arrayOf) ?: return null
@@ -1202,6 +1204,8 @@ internal class KtFirCallResolver(
val firSymbol = arrayOfSymbol.firSymbol
// 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)
@OptIn(UnresolvedExpressionTypeAccess::class)
val elementType = coneTypeSafe<ConeClassLikeType>()?.arrayElementType() ?: return KtSubstitutor.Empty(token)
val coneSubstitutor = substitutorByMap(mapOf(typeParameter.symbol to elementType), rootModuleSession)
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.
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
when (superTypes.size) {
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.coneTypeSafe
import org.jetbrains.kotlin.fir.types.isStableSmartcast
import org.jetbrains.kotlin.fir.types.resolvedType
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
@@ -105,7 +106,7 @@ internal class KtFirSmartcastProvider(
if (receiver == null || receiver == firExpression.explicitReceiver) 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)
}
}
@@ -45,6 +45,8 @@ internal object FirAnnotationValueConverter {
private fun <T> FirConstExpression<T>.convertConstantExpression(): KtConstantAnnotationValue? {
val expression = psi as? KtElement
@OptIn(UnresolvedExpressionTypeAccess::class)
val type = coneTypeOrNull
val constantValue = when {
value == null -> KtConstantValue.KtNullConstantValue(expression)
@@ -178,19 +178,19 @@ internal object FirCompileTimeConstantEvaluator {
val opr1 = evaluate(functionCall.explicitReceiver, mode) ?: return null
opr1.evaluate(function)?.let {
return it.adjustType(functionCall.coneTypeOrNull)
return it.adjustType(functionCall.resolvedType)
}
val argument = functionCall.arguments.firstOrNull() ?: return null
val opr2 = evaluate(argument, mode) ?: return null
opr1.evaluate(function, opr2)?.let {
return it.adjustType(functionCall.coneTypeOrNull)
return it.adjustType(functionCall.resolvedType)
}
return null
}
private fun FirConstExpression<*>.adjustType(expectedType: ConeKotlinType?): FirConstExpression<*> {
val expectedKind = expectedType?.toConstantValueKind()
private fun FirConstExpression<*>.adjustType(expectedType: ConeKotlinType): FirConstExpression<*> {
val expectedKind = expectedType.toConstantValueKind()
// Note that the resolved type for the const expression is not always matched with the const kind. For example,
// fun foo(x: Int) {
// 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
// from the dispatch receiver `this`.
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) {
fir.explicitReceiver?.unwrapSmartcastExpression()
@@ -733,7 +733,7 @@ internal object FirReferenceResolveHelper {
session: FirSession,
symbolBuilder: KtSymbolByFirBuilder
): Collection<KtSymbol> {
val type = fir.coneTypeOrNull ?: return emptyList()
val type = fir.resolvedType
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.low.level.api.fir.api.getOrBuildFirSafe
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.coneTypeSafe
@@ -24,6 +25,8 @@ class KtFirCollectionLiteralReference(
override fun KtAnalysisSession.resolveToSymbols(): Collection<KtSymbol> {
check(this is KtFirAnalysisSession)
val fir = element.getOrBuildFirSafe<FirArrayLiteral>(firResolveSession) ?: return emptyList()
@OptIn(UnresolvedExpressionTypeAccess::class)
val type = fir.coneTypeSafe<ConeClassLikeType>() ?: return listOfNotNull(arrayOfSymbol(arrayOf))
val call = arrayTypeToArrayOfCall[type.lookupTag.classId] ?: arrayOf
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.FirTypeRef
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.visitors.FirDefaultVisitorVoid
import org.jetbrains.kotlin.name.StandardClassIds
@@ -111,7 +112,7 @@ private class CodeFragmentCapturedValueVisitor(
private fun processElement(element: FirElement) {
if (element is FirExpression) {
val symbol = element.coneTypeOrNull?.toSymbol(session)
val symbol = element.resolvedType.toSymbol(session)
if (symbol != null) {
registerFile(symbol)
}
@@ -290,4 +291,4 @@ private class CodeFragmentCapturedValueVisitor(
private val FirBasedSymbol<*>.isMutated: Boolean
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.psi
import org.jetbrains.kotlin.fir.references.*
import org.jetbrains.kotlin.fir.types.FirErrorTypeRef
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.*
import org.jetbrains.kotlin.fir.types.impl.FirResolvedTypeRefImpl
import org.jetbrains.kotlin.fir.visitors.FirVisitor
import org.jetbrains.kotlin.lexer.KtTokens
@@ -218,7 +215,7 @@ internal open class FirElementsRecorder : FirVisitor<Unit, MutableMap<KtElement,
convertedValue as T,
setType = false
).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.isUsedInControlFlowGraphBuilderForFile
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.KtFile
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
return body !is FirLazyBlock && body.coneTypeOrNull != null
return body !is FirLazyBlock && body.isResolved
}
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.
*/
@file:OptIn(UnresolvedExpressionTypeAccess::class)
package org.jetbrains.kotlin.analysis.low.level.api.fir.util
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.FirExpression
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.references.*
import org.jetbrains.kotlin.fir.types.ConeKotlinType