Bug 18977 – struct and class declarations are inconsistent about what happens when they're marked with immutable

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-06-13T20:45:56Z
Last change time
2024-12-13T18:59:08Z
Assigned to
No Owner
Creator
Jonathan M Davis
Moved to GitHub: dmd#19446 →

Comments

Comment #0 by issues.dlang — 2018-06-13T20:45:56Z
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.
Comment #1 by razvan.nitu1305 — 2018-09-28T09:50:25Z
*** Issue 19273 has been marked as a duplicate of this issue. ***
Comment #2 by default_357-line — 2020-03-13T08:55:38Z
What difference is there for a struct between all fields being immutable and the struct itself being immutable? I can't think of one.
Comment #3 by robert.schadek — 2024-12-13T18:59:08Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19446 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB