Fix AA/LC behavior accordingly to changes related to KT-54844

This commit is contained in:
Mikhail Glukhikh
2023-05-04 10:51:03 +02:00
committed by Space Team
parent 2a825f8df1
commit 5304afb097
5 changed files with 25 additions and 76 deletions
@@ -20,10 +20,7 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.util.errorWithFirSpecific
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.withFirEntry
import org.jetbrains.kotlin.analysis.utils.errors.buildErrorWithAttachment
import org.jetbrains.kotlin.fir.containingClassForLocalAttr
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticProperty
import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticPropertyAccessor
import org.jetbrains.kotlin.fir.declarations.utils.isLocal
@@ -53,7 +50,7 @@ internal tailrec fun FirDeclaration.ktSymbolOrigin(): KtSymbolOrigin = when (ori
FirDeclarationOrigin.Source -> {
when (source?.kind) {
KtFakeSourceElementKind.ImplicitConstructor,
KtFakeSourceElementKind.DataClassGeneratedMembers,
KtFakeSourceElementKind.DataClassGeneratedMembers, /* Valid for copy() / componentX(), should we change it? */
KtFakeSourceElementKind.EnumGeneratedDeclaration,
KtFakeSourceElementKind.ItLambdaParameter -> KtSymbolOrigin.SOURCE_MEMBER_GENERATED
@@ -69,9 +66,10 @@ internal tailrec fun FirDeclaration.ktSymbolOrigin(): KtSymbolOrigin = when (ori
FirDeclarationOrigin.IntersectionOverride -> KtSymbolOrigin.INTERSECTION_OVERRIDE
FirDeclarationOrigin.Delegated -> KtSymbolOrigin.DELEGATED
FirDeclarationOrigin.Synthetic -> {
when (this) {
is FirSyntheticProperty,
is FirSyntheticPropertyAccessor -> KtSymbolOrigin.JAVA_SYNTHETIC_PROPERTY
when {
source?.kind == KtFakeSourceElementKind.DataClassGeneratedMembers -> KtSymbolOrigin.SOURCE_MEMBER_GENERATED
this is FirValueParameter && this.containingFunctionSymbol.origin == FirDeclarationOrigin.Synthetic -> KtSymbolOrigin.SOURCE_MEMBER_GENERATED
this is FirSyntheticProperty || this is FirSyntheticPropertyAccessor -> KtSymbolOrigin.JAVA_SYNTHETIC_PROPERTY
else -> buildErrorWithAttachment("Invalid FirDeclarationOrigin ${origin::class.simpleName}") {
withFirEntry("firToGetOrigin", this@ktSymbolOrigin)
@@ -1,31 +0,0 @@
KtSuccessCallInfo:
call = KtSimpleFunctionCall:
isImplicitInvoke = false
partiallyAppliedSymbol = KtPartiallyAppliedSymbol:
dispatchReceiver = KtExplicitReceiverValue:
expression = d1
isSafeNavigation = false
type = D
extensionReceiver = null
signature = KtFunctionLikeSignature:
receiverType = null
returnType = kotlin.Boolean
symbol = /D.equals(<dispatch receiver>: D, other: kotlin.Any?): kotlin.Boolean
valueParameters = [
KtVariableLikeSignature:
name = other
receiverType = null
returnType = kotlin.Any?
symbol = other: kotlin.Any?
callableIdIfNonLocal = null
]
callableIdIfNonLocal = /D.equals
typeArgumentsMapping = {}
argumentMapping = {
d2 -> (KtVariableLikeSignature:
name = other
receiverType = null
returnType = kotlin.Any?
symbol = other: kotlin.Any?
callableIdIfNonLocal = null)
}
@@ -10,7 +10,7 @@ KtSuccessCallInfo:
signature = KtFunctionLikeSignature:
receiverType = null
returnType = kotlin.Boolean
symbol = kotlin/Any.equals(<dispatch receiver>: kotlin.Any, other: kotlin.Any?): kotlin.Boolean
symbol = /D.equals(<dispatch receiver>: D, other: kotlin.Any?): kotlin.Boolean
valueParameters = [
KtVariableLikeSignature:
name = other
@@ -19,7 +19,7 @@ KtSuccessCallInfo:
symbol = other: kotlin.Any?
callableIdIfNonLocal = null
]
callableIdIfNonLocal = kotlin/Any.equals
callableIdIfNonLocal = /D.equals
typeArgumentsMapping = {}
argumentMapping = {
d2 -> (KtVariableLikeSignature:
@@ -583,7 +583,7 @@ KtFunctionSymbol:
KtFunctionSymbol:
annotationsList: []
callableIdIfNonLocal: kotlin/Any.equals
callableIdIfNonLocal: /Abc.equals
contextReceivers: []
contractEffects: []
hasStableParameterNames: true
@@ -598,7 +598,7 @@ KtFunctionSymbol:
isSuspend: false
modality: OPEN
name: equals
origin: LIBRARY
origin: SOURCE_MEMBER_GENERATED
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -619,7 +619,7 @@ KtFunctionSymbol:
isNoinline: false
isVararg: false
name: other
origin: LIBRARY
origin: SOURCE_MEMBER_GENERATED
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -627,20 +627,20 @@ KtFunctionSymbol:
type: kotlin/Any?
symbolKind: LOCAL
typeParameters: []
getContainingModule: KtBinaryModule "Builtins for JVM (1.8)"
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
]
visibility: Public
getDispatchReceiver(): KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Any
getContainingModule: KtBinaryModule "Builtins for JVM (1.8)"
type: Abc
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtFunctionSymbol:
annotationsList: []
callableIdIfNonLocal: kotlin/Any.hashCode
callableIdIfNonLocal: /Abc.hashCode
contextReceivers: []
contractEffects: []
hasStableParameterNames: true
@@ -655,7 +655,7 @@ KtFunctionSymbol:
isSuspend: false
modality: OPEN
name: hashCode
origin: LIBRARY
origin: SOURCE_MEMBER_GENERATED
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -668,13 +668,13 @@ KtFunctionSymbol:
getDispatchReceiver(): KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Any
getContainingModule: KtBinaryModule "Builtins for JVM (1.8)"
type: Abc
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtFunctionSymbol:
annotationsList: []
callableIdIfNonLocal: kotlin/Any.toString
callableIdIfNonLocal: /Abc.toString
contextReceivers: []
contractEffects: []
hasStableParameterNames: true
@@ -689,7 +689,7 @@ KtFunctionSymbol:
isSuspend: false
modality: OPEN
name: toString
origin: LIBRARY
origin: SOURCE_MEMBER_GENERATED
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -702,8 +702,8 @@ KtFunctionSymbol:
getDispatchReceiver(): KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Any
getContainingModule: KtBinaryModule "Builtins for JVM (1.8)"
type: Abc
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtConstructorSymbol:
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.StandardNames.HASHCODE_NAME
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.light.classes.symbol.annotations.GranularAnnotationsBox
import org.jetbrains.kotlin.light.classes.symbol.annotations.SymbolAnnotationsProvider
import org.jetbrains.kotlin.light.classes.symbol.cachedValue
@@ -34,8 +33,6 @@ import org.jetbrains.kotlin.light.classes.symbol.methods.SymbolLightSimpleMethod
import org.jetbrains.kotlin.light.classes.symbol.modifierLists.GranularModifiersBox
import org.jetbrains.kotlin.light.classes.symbol.modifierLists.SymbolLightClassModifierList
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.DataClassResolver
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKind
@@ -175,16 +172,6 @@ internal open class SymbolLightClassForClassOrObject : SymbolLightClassForNamedC
)
}
fun actuallyComesFromAny(functionSymbol: KtFunctionSymbol): Boolean {
require(functionSymbol.name.isFromAny) {
"This function's name should one of three Any's function names, but it was ${functionSymbol.name}"
}
if (functionSymbol.callableIdIfNonLocal?.classId == StandardClassIds.Any) return true
return functionSymbol.getAllOverriddenSymbols().any { it.callableIdIfNonLocal?.classId == StandardClassIds.Any }
}
// NB: componentN and copy are added during RAW FIR, but synthetic members from `Any` are not.
// That's why we use declared scope for 'component*' and 'copy', and member scope for 'equals/hashCode/toString'
val componentAndCopyFunctions = classOrObjectSymbol.getDeclaredMemberScope()
@@ -196,14 +183,12 @@ internal open class SymbolLightClassForClassOrObject : SymbolLightClassForNamedC
// Compiler will generate 'equals/hashCode/toString' for data class if they are not final.
// We want to mimic that.
val nonFinalFunctionsFromAny = classOrObjectSymbol.getMemberScope()
val generatedFunctionsFromAny = classOrObjectSymbol.getMemberScope()
.getCallableSymbols(EQUALS, HASHCODE_NAME, TO_STRING)
.filterIsInstance<KtFunctionSymbol>()
.filterNot {
it.modality == Modality.FINAL || (it.getContainingSymbol() as? KtNamedClassOrObjectSymbol)?.modality == Modality.FINAL
}.filter { actuallyComesFromAny(it) }
.filter { it.origin == KtSymbolOrigin.SOURCE_MEMBER_GENERATED }
val functionsFromAnyByName = nonFinalFunctionsFromAny.associateBy { it.name }
val functionsFromAnyByName = generatedFunctionsFromAny.associateBy { it.name }
// NB: functions from `Any` are not in an alphabetic order.
functionsFromAnyByName[TO_STRING]?.let { createMethodFromAny(it) }
@@ -211,9 +196,6 @@ internal open class SymbolLightClassForClassOrObject : SymbolLightClassForNamedC
functionsFromAnyByName[EQUALS]?.let { createMethodFromAny(it) }
}
private val Name.isFromAny: Boolean
get() = this == EQUALS || this == HASHCODE_NAME || this == TO_STRING
context(KtAnalysisSession)
private fun addDelegatesToInterfaceMethods(result: MutableList<KtLightMethod>, classOrObjectSymbol: KtNamedClassOrObjectSymbol) {
fun createDelegateMethod(ktFunctionSymbol: KtFunctionSymbol) {