Simplify data class function generation and signature lookup code
- change prerequisites for generating equals/hashCode/toString in a data class: previously they were generated if the corresponding method is trivial (i.e. it comes from kotlin.Any), now we're generating it always unless it'll cause a JVM signature clash error (see KT-6206) - use static KotlinBuiltIns.isXxx methods to compare types instead of checking against descriptors loaded from certain built-ins instance, this is quicker and more correct in environments where several built-ins are possible - don't use isOrOverridesSynthesized, it's not relevant for equals/hashCode/toString because functions with these names are never synthesized #KT-6206 Fixed
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
// TODO: remove the suppression once data classes can have supertypes
|
||||
@file:Suppress("DATA_CLASS_CANNOT_HAVE_CLASS_SUPERTYPES")
|
||||
|
||||
abstract class Base {
|
||||
final override fun toString() = "OK"
|
||||
final override fun hashCode() = 42
|
||||
final override fun equals(other: Any?) = false
|
||||
}
|
||||
|
||||
data class DataClass(val field: String) : Base()
|
||||
|
||||
fun box(): String {
|
||||
val d = DataClass("x")
|
||||
|
||||
if (d.toString() != "OK") return "Fail toString"
|
||||
if (d.hashCode() != 42) return "Fail hashCode"
|
||||
if (d.equals(d) != false) return "Fail equals"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// See KT-6206 Always generate hashCode() and equals() for data classes even if base classes have non-trivial analogs
|
||||
// TODO: remove the suppression once data classes can have supertypes
|
||||
@file:Suppress("DATA_CLASS_CANNOT_HAVE_CLASS_SUPERTYPES")
|
||||
|
||||
abstract class Base {
|
||||
override fun toString() = "Fail"
|
||||
override fun hashCode() = -42
|
||||
override fun equals(other: Any?) = false
|
||||
}
|
||||
|
||||
data class DataClass(val field: String) : Base()
|
||||
|
||||
fun box(): String {
|
||||
val d = DataClass("x")
|
||||
|
||||
if (d.toString() != "DataClass(field=x)") return "Fail toString"
|
||||
if (d.hashCode() != "x".hashCode()) return "Fail hashCode"
|
||||
if (d.equals(d) == false) return "Fail equals"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user