I noticed that data classes don't declare equals/h...
# compiler
d
I noticed that data classes don't declare equals/hashCode/toString in FIR. Is this an intentional choice, or just a leftover implementation from the pre-FIR compiler?
d
The fact if each of these functions will be generated or not depend on the supertypes, so compiler computes and stores them in the special scope instead of directly in the list of declarations (it's illegal to modify
FirRegularClass.declarations
after it was initially created)
Copy code
abstract class A {
    final override fun toString() = "hello"
}

data class B(val x: Int) : A() {
    // `toString` is not generated
}
Moreover, there could be quite strange scenarios in MPP, when the fact if some of these members is generated or not depend on the module from which you are looking at the class
Copy code
// MODULE: common
expect abstract class A
data class B(val x: Int) : A()

fun testCommon(b: B) {
    b.toString() // resolved to "generated" B.toString
}

// MODULE: platform()()(common)
actual abstract class A {
    final override fun toString() = "hello"
}

fun testPlatform(b: B) {
    b.toString() // resolved to A.toString
}
So it's another point to store these functions in scopes, which are module-dependent, and not in
FirRegularClass
directly (which is the same across all modules)
d
Makes sense, thanks for the details!
It seems like the latter case is currently warned against and will become an error in the future?
Copy code
function 'toString': the modality of this member must be the same in the expect class and the actual class. This error happens because the expect class 'A' is non-final. This warning will become an error in future releases. Also see <https://youtrack.jetbrains.com/issue/KT-22841> for more details
Unless the warning I’m seeing is the mistaken warning that that ticket mentions
d
Yep, it is expected