[Issue 24324] A default-initialized variable is not identical to its init value when it contains a default-initialized member variable that is a dynamic array
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jan 8 11:19:17 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=24324
RazvanN <razvan.nitu1305 at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |razvan.nitu1305 at gmail.com
--- Comment #1 from RazvanN <razvan.nitu1305 at gmail.com> ---
My first impression on this was that the compiler probably stores the struct
initializer in the data/rodata segment and when an instance is created, it
allocates heap memory with the GC and copies over the dynamic array elements.
However, looking at the compiler code, things get weirder. It seems that in
this case, the compiler lowers `S.init` to a Struct Literal Expression "S([1,
2, 3])" (i.e. it does not store a literal representation in the data/rodata
segment), whereas when it encounters `S s1;` is uses a global symbol to
initialize s1. That is why s1 and s2 have the same bit representation but
`S.init` does not.
A consequence of this is that `assert(S.init is S.init)` fails in this case.
Which is kind of absurd. However, I would argue that when dynamic arrays are
involved the backend is free to do whatever it sees fit with regards to the
addresses of dynamic arrays, so using `is` in this case (as opposed to
opEquals) is brittle.
--
More information about the Digitalmars-d-bugs
mailing list