Bug 20365 – Copy constructor not invoked on static arrays of structs but the postblit works

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-11-06T22:23:52Z
Last change time
2022-08-17T13:25:37Z
Keywords
betterC, pull
Assigned to
No Owner
Creator
Suleyman Sahmi (سليمان السهمي)

Comments

Comment #0 by sahmi.soulaimane — 2019-11-06T22:23:52Z
Copy constructor case: https://run.dlang.io/is/xzVWOi ``` extern(C) void puts(const char*); struct S { long[3] a; this(ref typeof(this)) { puts("CC"); } } void main() { S[4] a; auto b = a; } ``` Prints nothing. Expected to print "CC" four times. Postblit case: https://run.dlang.io/is/OX4O8I ``` extern(C) void puts(const char*); struct S { long[3] a; this(this) { puts("PB"); } } void main() { S[4] a; auto b = a; } ``` prints "PB" four times as expected.
Comment #1 by kinke — 2019-11-07T00:10:25Z
Let's include CatAssign as well for completeness: S[] c; c ~= a; // no copy ctor calls, but 4 postblits
Comment #2 by sahmi.soulaimane — 2019-11-07T00:23:02Z
There is a whole jungle of pustblit calls in druntime that do not call the copy constructor yet. Just grep the word "postblit" in druntime source to get an idea. I contacted Razvan about it.
Comment #3 by razvan.nitu1305 — 2019-11-07T07:53:21Z
This can be nicely solved with the new hasCopyConstructor/hasPostblit traits [1]. I will look into it. [1] https://github.com/dlang/dmd/pull/10528
Comment #4 by razvan.nitu1305 — 2022-02-17T13:39:59Z
*** Issue 21368 has been marked as a duplicate of this issue. ***
Comment #5 by razvan.nitu1305 — 2022-07-05T10:11:57Z
(In reply to kinke from comment #1) > Let's include CatAssign as well for completeness: > > S[] c; > c ~= a; // no copy ctor calls, but 4 postblits The CatAssign example seems to have been fixed, but the initial assign example still fails.
Comment #6 by dlang-bot — 2022-08-16T12:08:47Z
@RazvanN7 created dlang/dmd pull request #14372 "Fix Issue 20365 - Copy constructor not invoked on static arrays of structs but the postblit works" fixing this issue: - Fix Issue 20365 - Copy constructor not invoked on static arrays of structs but the postblit works https://github.com/dlang/dmd/pull/14372