[FE] Relax rules of matching @Target annotation on expect and actual

Allow `expect` targets to be subset of `actual`.

^KT-58551
This commit is contained in:
Roman Efremov
2023-07-17 14:55:03 +02:00
committed by Space Team
parent ad84c83ee9
commit 6611a55a60
15 changed files with 202 additions and 21 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.mpp.*
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualCollectionArgumentsCompatibilityCheckStrategy
import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualMatchingContext
import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualMatchingContext.AnnotationCallInfo
import org.jetbrains.kotlin.resolve.checkers.OptInNames
@@ -453,10 +454,17 @@ internal abstract class IrExpectActualMatchingContext(
override val DeclarationSymbolMarker.annotations: List<AnnotationCallInfo>
get() = asIr().annotations.map(::AnnotationCallInfoImpl)
override fun areAnnotationArgumentsEqual(annotation1: AnnotationCallInfo, annotation2: AnnotationCallInfo): Boolean {
override fun areAnnotationArgumentsEqual(
annotation1: AnnotationCallInfo, annotation2: AnnotationCallInfo,
collectionArgumentsCompatibilityCheckStrategy: ExpectActualCollectionArgumentsCompatibilityCheckStrategy,
): Boolean {
fun AnnotationCallInfo.getIrElement(): IrConstructorCall = (this as AnnotationCallInfoImpl).irElement
return areIrExpressionConstValuesEqual(annotation1.getIrElement(), annotation2.getIrElement())
return areIrExpressionConstValuesEqual(
annotation1.getIrElement(),
annotation2.getIrElement(),
collectionArgumentsCompatibilityCheckStrategy,
)
}
internal fun getClassIdAfterActualization(classId: ClassId): ClassId {
@@ -8,8 +8,13 @@ package org.jetbrains.kotlin.backend.common.actualizer
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.classOrFail
import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualCollectionArgumentsCompatibilityCheckStrategy
internal fun IrExpectActualMatchingContext.areIrExpressionConstValuesEqual(a: IrElement?, b: IrElement?): Boolean {
internal fun IrExpectActualMatchingContext.areIrExpressionConstValuesEqual(
a: IrElement?,
b: IrElement?,
collectionArgumentsCompatibilityCheckStrategy: ExpectActualCollectionArgumentsCompatibilityCheckStrategy,
): Boolean {
return when {
a == null || b == null -> (a == null) == (b == null)
@@ -25,15 +30,20 @@ internal fun IrExpectActualMatchingContext.areIrExpressionConstValuesEqual(a: Ir
a is IrGetEnumValue && b is IrGetEnumValue -> equalBy(a, b) { it.symbol.signature?.toString() }
a is IrVararg && b is IrVararg -> {
equalBy(a, b) { it.elements.size } &&
a.elements.zip(b.elements).all { (f, s) -> areIrExpressionConstValuesEqual(f, s) }
collectionArgumentsCompatibilityCheckStrategy.areCompatible(a.elements, b.elements) { f, s ->
areIrExpressionConstValuesEqual(f, s, collectionArgumentsCompatibilityCheckStrategy)
}
}
a is IrConstructorCall && b is IrConstructorCall -> {
equalBy(a, b) { it.valueArgumentsCount } &&
areCompatibleExpectActualTypes(a.type, b.type) &&
(0..<a.valueArgumentsCount).all { i ->
areIrExpressionConstValuesEqual(a.getValueArgument(i), b.getValueArgument(i))
areIrExpressionConstValuesEqual(
a.getValueArgument(i),
b.getValueArgument(i),
collectionArgumentsCompatibilityCheckStrategy
)
}
}