[Issue 24099] New: Immutable module variable can be initialized by multiple static constructors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 22 19:03:58 UTC 2023


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

          Issue ID: 24099
           Summary: Immutable module variable can be initialized by
                    multiple static constructors
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: snarwin+bugzilla at gmail.com

As of DMD 2.104.0, the following program compiles and runs to completion:

---
import core.stdc.stdio: printf;

immutable int n;

shared static this()
{
    n = 123;
    printf("n = %d\n", n);
}

shared static this()
{
    n = 456;
    printf("n = %d\n", n);
}

void main() {}
---

When run, the output is:

---
n = 123
n = 456
---

Since it should not be possible to mutate an immutable variable after it has
been initialized, the second static constructor is invalid, and should be
rejected by the compiler.

--


More information about the Digitalmars-d-bugs mailing list