D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8192 - inconsistent behavior of initialized immutable instance fields
Summary: inconsistent behavior of initialized immutable instance fields
Status: RESOLVED DUPLICATE of issue 3449
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-03 10:19 UTC by timon.gehr
Modified: 2012-12-26 16:03 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description timon.gehr 2012-06-03 10:19:14 UTC
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 timon.gehr 2012-06-03 10:23:29 UTC
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 art.08.09 2012-06-03 12:09:51 UTC
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 art.08.09 2012-06-03 12:25:49 UTC
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 Andrej Mitrovic 2012-12-26 16:03:42 UTC
Another case of Issue 3449 methinks.

*** This issue has been marked as a duplicate of issue 3449 ***