www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24099] New: Immutable module variable can be initialized by

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 puremagic.com
          Reporter: snarwin+bugzilla 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.

--
Aug 22 2023