On Saturday, 24 January 2015 at 13:24:02 UTC, Nordlöw wrote:
This snippet

    static immutable words = [ `zero`, `one`, `two` ];

    static immutable ubyte[string] wordsAA;

    static this()
    {
        foreach (ubyte i, e; words) { wordsAA[e] = i; }
    }

compiles and links on dmd git master but run segfaults.

Removing immutable qualifier from wordsAA avoids the segfeault.

Are static module ctors allowed to modify immutable data or this a DMD bug?

They are definitely allowed to initialize immutable data, i.e. write once. I don't know if you're allowed to change it repeatedly.

You can change your code to write only once:

static this()
{
    import std.exception: assumeUnique;
    ubyte[string] tmp;
    foreach (ubyte i, e; words) { tmp[e] = i; }
wordsAA = assumeUnique(tmp); /* Don't alter tmp from here on. */
}

Reply via email to