[FIR] Let attributes opt-in to participating in ConeClassLikeTypeImpl structural equality

This is required for EnhancedTypeForWarningAttribute because scopes
should not be reused between cone types with different values of
this attribute.

#KT-63208
This commit is contained in:
Kirill Rakhman
2024-01-05 15:10:28 +01:00
committed by Space Team
parent 931cc48def
commit 9189154cae
11 changed files with 58 additions and 92 deletions
@@ -32,6 +32,13 @@ abstract class ConeAttribute<out T : ConeAttribute<T>> : AnnotationMarker {
abstract override fun toString(): String
open fun renderForReadability(): String? = null
/**
* Signals that this attribute properly implements the [equals] and [hashCode] protocol.
*
* If it returns `true`, attributes will be compared using structural equality in [ConeAttributes.definitelyDifferFrom].
*/
open val implementsEquality: Boolean get() = false
abstract val key: KClass<out T>
/**
@@ -152,6 +159,32 @@ class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : A
return create(attributes)
}
/**
* Returns `true` if this instance is definitely not equal to the [other] instance.
* This is `true` when one instance contains an attribute **type** that the other doesn't contain or both instances contain
* an attribute of a type where [ConeAttribute.implementsEquality]` == true` and the attribute's [equals] method returns `false`.
*
* A return value of `false` doesn't guarantee that the instances are equal because [ConeAttribute.implementsEquality] is optional,
* i.e., not all attributes can be compared structurally.
*
* @see org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl.equals
*/
infix fun definitelyDifferFrom(other: ConeAttributes): Boolean {
if (this === other) return false
if (this.isEmpty() && other.isEmpty()) return false
for (index in indices) {
val a = arrayMap[index]
val b = other.arrayMap[index]
if (a == null && b == null) continue
if ((a == null) != (b == null)) return true
if (a!!.implementsEquality && a != b) return true
}
return false
}
/**
* Applies the [transform] to all attributes that are subtypes of [ConeAttributeWithConeType] and returns a [ConeAttributes]
* with the results of transforms that were not-`null` or `null` if no attributes were transformed.
@@ -34,6 +34,7 @@ class ConeClassLikeTypeImpl(
if (lookupTag != other.lookupTag) return false
if (!typeArguments.contentEquals(other.typeArguments)) return false
if (nullability != other.nullability) return false
if (attributes definitelyDifferFrom other.attributes) return false
return true
}