I believe I found a kotlin-native bug. I want to ...
# kotlin-native
c
I believe I found a kotlin-native bug. I want to bounce it off of people here before I report it, to see if I'm just doing something wrong or perhaps expecting too much. When I build this code sample (let me know if anyone wants the entire reproducing project) with kotlin native, at least on linux_x64, and then run the resulting binary, it will print out bb_b and bb_c just fine, but bb_a (which comes before bb_b alphabetically!) will then cause a segfault, because bb_a.number is uninitialized. I'm using a trivial wrapper class because the issue doesn't seem to manifest with types that can become C types. Of course, the issue also doesn't manifest when running on jvm.
Copy code
fun main(args: Array<String>) {
    NumbersByBit.values().forEach {
        println("The number ${it.name} is: ${it.number.num}")
    }
}

enum class NumbersByBit(val number: NumWrapper) {
    bb_b(NumWrapper(6)),
    bb_c(bb_b.number),
    bb_a(bb_b.number),
}

class NumWrapper(val num: Int) {
    operator fun plus(other: NumWrapper) = NumWrapper(num + other.num)
}
So is this a bug? Or are self-referential enums out-of-scope for kotlin native? Update: Based on above messages, I tried kotlin 1.4.31 and the same error occurs. When I test the binaries with valgrind, I see an invalid read:
Copy code
The number bb_c is: 6 
==895440== Invalid read of size 4 
==895440==   at 0x443ED1: Init_and_run_start (in /home/alex/IdeaProjects/NativeError/nativeError.kexe_1.5.10~) 
==895440==   by 0x4A300B2: (below main) (libc-start.c:308) 
==895440== Address 0x8 is not stack'd, malloc'd or (recently) free'd
The invalid read error looks the same when running the kotlin 1.4.31 version.
r
I've heard of other weirdness with self-referencing enums, eg https://youtrack.jetbrains.com/issue/KT-38540. I wonder if the fix for that will help here too.
c
Good intuition. That does seem related (in particular the alphabetical connection). I tried the fix version, 1.5.20-M1, and the problem disappears, so I'll just roll with that version until there's a newer stable. Thank you!