[Issue 5207] Immutability is broken in constructors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 23 06:42:31 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=5207

--- Comment #8 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to hsteoh from comment #7)
> Here's an example of a problematic case:

In the code that you have posted there seems to be an error with regards to
module constructors, because if you put the same code in a normal constructor:

import std;

struct S {
        int x;
        this(int k)
        {
            fun(&s);

            writeln(ptr.x); // prints 0

            s.x = 1;
            writeln(ptr.x); // prints 1

            s.x = 2;
            writeln(ptr.x); // prints 2: immutability broken

        }

}

immutable S s;
immutable(S)* ptr;

void fun(immutable(S)* p) {
        ptr = p;
}

you will get errors when trying to modify `s`. The bug in the example that you
posted is that `s` is not typechecked correctly in the module constructor, not
the fact that you should not be able to read an immutable variable before
initialization.

--


More information about the Digitalmars-d-bugs mailing list