[Issue 18561] postblit should allow writing const/immutable members just like constructors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 6 13:30:43 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18561

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |schveiguy at yahoo.com
         Resolution|DUPLICATE                   |---
            Summary|postblit behaves            |postblit should allow
                   |inconsistently with         |writing const/immutable
                   |constants                   |members just like
                   |                            |constructors

--- Comment #2 from Steven Schveighoffer <schveiguy at yahoo.com> ---
I think the OP has a point here.

A more direct example:

struct S
{
   const char[] t;
   this(this)
   {
      t = t.dup;     // this should be allowed
      // t[0] = 'w'; // this should not
   }
}

S s;

// Should be OK, calls postblit, s2 is new data.
auto s2 = s;

// error cannot overwrite const (does not call postblit). This happens already
s = s2;

essentially, when READING `this`, all normal rules apply. When WRITING members
of `this`, everything should be considered tail-X, where X is const, immutable,
etc. If we have any immutable or const data as members, the compiler should
have already forbade it if you couldn't overwrite it before the postblit.

It's the same as constructors, but for constructors, `this` didn't exist yet.
If we get to a postblit on a type that has const or immutable data, we have the
same guarantee.

This does not address postblits being called on const data types (with `this`
being mutable during the postblit). That is a different bug.

--


More information about the Digitalmars-d-bugs mailing list