Actual immutability enforcement by placing immutable data into read-only sections

Siarhei Siamashka siarhei.siamashka at gmail.com
Mon Dec 19 12:19:40 UTC 2022


On Monday, 19 December 2022 at 12:13:08 UTC, Siarhei Siamashka 
wrote:
> What do you think about it? Does this require a new DIP?

BTW, I tried to experiment with DMD code and can place some array 
literals into a read-only section: 
https://github.com/ssvb/dmd/commit/44c3a7c312b042fa7fafd357775aedf904ba0700

But much more seems to be needed to get it right. Nested array 
literals, such as the `[1,2]` part of `[[1,2],[3,4]]`, don't seem 
to have the immutable flag set when checked from 
https://github.com/dlang/dmd/blob/v2.101.1/compiler/src/dmd/todt.d#L456-L490

Additionally, the immutable flag seems to be stripped at 
https://github.com/dlang/dmd/blob/v2.101.1/compiler/src/dmd/tocsym.d#L189-L219 from immutable class and struct instances if they have constructors. But does this really matter for the data generated by CTFE?

Detecting whether the data was generated by CTFE also doesn't 
seem to be very obvious. I tried to check the `.ownedByCtfe` 
field, but I'm getting strange results.

Can anyone give me some hints?


More information about the Digitalmars-d mailing list