Bug 13629 – Field postblit can't be generated for const field
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-10-17T13:56:28Z
Last change time
2021-06-10T18:05:43Z
Assigned to
No Owner
Creator
Igor Stepanov
Comments
Comment #0 by wazar.leollone — 2014-10-17T13:56:28Z
module test1;
struct A
{
this(this)
{
}
}
struct B
{
A a;
}
struct C
{
const B b;
}
void main()
{
C c;
}
This code raises an error:
Error: mutable method test1.B.__fieldPostBlit is not callable using a const object
Comment #1 by ag0aep6g — 2014-10-19T21:02:29Z
Same with destructor:
struct A
{
~this() {} /* Error: mutable method test.B.~this is not callable using a const object */
}
struct B {A a;}
struct C {const B b;}
Comment #2 by ag0aep6g — 2014-10-19T21:05:15Z
A workaround:
struct C
{
const B[1] b_;
@property ref const(B) b() {return b_[0];}
}
Comment #3 by issues.dlang — 2014-10-20T22:24:45Z
As much as this sucks, it really isn't a bug. const and postblits are incompatible by their very nature, because the postblit copies and then mutates, which violates const. So, arguably, going with postblit constructors over copy constructors was a mistake. There's a DIP that tries to fix the problem ( http://wiki.dlang.org/DIP49 ), but it was deemed to be way too complicated. This is a design problem in the language that needs to be fixed (possibly by adding copy constructors) and not a bug in the compiler. It is expected behavior at this point.
Comment #4 by moonlightsentinel — 2021-06-10T18:05:43Z
The first example works since 2.067.1, the latter with 2.068.2 (according to run.dlang.io)