Bug 17920 – Missing postblit for `T lhs = cast(T) <const(T) rhsLValue>`

Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-10-20T21:03:35Z
Last change time
2023-07-19T10:16:56Z
Assigned to
No Owner
Creator
kinke

Comments

Comment #0 by kinke — 2017-10-20T21:03:35Z
Casting away the constness from a right-hand-side lvalue skips the required postblit call: ``` import core.stdc.stdio; int postblit = 0, dtor = 0; struct S { uint val; this(this) { ++postblit; ++val; } ~this() { ++dtor; } } void foo(S s) { const fromMutable = cast(S) s; printf("fromMutable.val: %d\n", fromMutable.val); } void fooConst(const S s) { const fromConst = cast(S) s; printf("fromConst.val: %d\n", fromConst.val); } void main() { const x = S(100); foo(x); printf("postblit: %d, dtor: %d\n", postblit, dtor); fooConst(x); printf("postblit: %d, dtor: %d\n", postblit, dtor); } ``` DMD 2.076.1 yields: ``` fromMutable.val: 102 postblit: 2, dtor: 2 fromConst.val: 101 postblit: 3, dtor: 4 ```
Comment #1 by razvan.nitu1305 — 2023-07-19T10:16:56Z
I cannot reproduce this for neither postblit, nor copy constructor. When I compile the initial test case with latest master, I get: fromMutable.val: 102 postblit: 2, dtor: 2 fromConst.val: 102 postblit: 4, dtor: 4 Which is the correct behavior.