June 13, 2018
https://issues.dlang.org/show_bug.cgi?id=18977

          Issue ID: 18977
           Summary: struct and class declarations are inconsistent about
                    what happens when they're marked with immutable
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: issues.dlang@jmdavisProg.com

This code

----
immutable struct S {}
immutable class C {}

void main()
{
    S s;
    pragma(msg, typeof(s));

    C c;
    pragma(msg, typeof(c));
}
----

prints

immutable(S)
C

I find this very surprising. It is my understanding that marking a struct or class as immutable only affects its members, not the type itself. The type doesn't magically become immutable everywhere it's used - and having it be magically immutable everywhere is going to be really confusing. I don't see how anyone can expect to look at

S s;

and think anything other than s is mutable, so it strikes me as a terrible idea to have it be immutable based on the fact that the struct declaration was marked with immutable

So, I would strongly argue that this should be fixed so that s is not implicitly immutable and acts more like what happens with the class case. However, regardless of whether the struct variable should be implicitly considered immutable or not, the fact that it is while the class variable is not is inconsistent, which makes what's going on harder to understand and more confusing.

So, either, we should fix it so that marking a struct declaration as immutable has no effect on the mutability of any variables of that type (just its members), or we should fix it so that marking a class declaration as immutable makes all variables of that type immutable just like it currently does with structs. I vote for making it so that variables are never implicitly immutable based on the declaration of the types, but either way, it should be consistent.

--