Bug 8192 – inconsistent behavior of initialized immutable instance fields
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-03T10:19:00Z
Last change time
2012-12-26T16:03:42Z
Assigned to
nobody
Creator
timon.gehr
Comments
Comment #0 by timon.gehr — 2012-06-03T10:19:14Z
DMD 2.059:
struct S{ immutable y = 1; }
void main(){
writeln(S.y); // ok
writeln(&S.y); // error
with(S){
writeln(&y); // ok
assert(*&y==y); // fail
}
}
Either the code should run through or all the commented lines should fail to compile.
Comment #1 by timon.gehr — 2012-06-03T10:23:29Z
Better test case:
struct S{ immutable y = 1; }
void main(){
assert(S.y==1); // ok
assert(&S.y!is null); // compile error
with(S){
assert(&y!is null); // ok
assert(*&y==y); // fail
}
}
Comment #2 by art.08.09 — 2012-06-03T12:09:51Z
Worse, the struct layout changes when an immutable field has an initializer.
struct S { immutable int x = 1; int y; }
struct S2 { int x; int y; }
void main(){
S s;
S2 s2;
static assert(s.y.offsetof==s2.y.offsetof); // fails
}
Trying to take the address of s.x fails with 'not an lvalue', etc.
Comment #3 by art.08.09 — 2012-06-03T12:25:49Z
A better example:
struct S { immutable int x = 1; int y; }
struct S2 { immutable int x; int y; }
void main(){
S s;
S2 s2;
static assert(s.y.offsetof==s2.y.offsetof); // fails
}
Comment #4 by andrej.mitrovich — 2012-12-26T16:03:42Z
Another case of Issue 3449 methinks.
*** This issue has been marked as a duplicate of issue 3449 ***