Bug 23337 – Wrongly elided postblit/copy ctor for array construction (_d_arrayctor lowering)

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-09-15T16:16:49Z
Last change time
2022-10-06T02:34:10Z
Keywords
pull
Assigned to
No Owner
Creator
kinke

Comments

Comment #0 by kinke — 2022-09-15T16:16:49Z
The following code worked for DMD v2.085-2.090, but regressed with v2.091 and still fails with current DMD master, after the templated `_d_arrayctor` introduction: ``` void main() { int copies; struct S { int i; this(this) { ++copies; } //this(inout ref S) inout { ++copies; } } import core.lifetime; S[2] ss1 = void; S[2] ss2; emplace(&ss1, ss2); assert(copies == 2); copies = 0; emplace(&ss1, ss2[]); assert(copies == 2); // fails (is 0) } ``` [It works just fine with LDC though.] This came up when bumping LDC's frontend, as the glue layer would still lower this to old untemplated `_d_arrayctor`. So the frontend misses the new templated `_d_arrayctor` lowering in this case; the according buggy logic is probably here: https://github.com/dlang/dmd/blob/638de4022d1b2d7a8f529d8b81c7916a958bbe9d/compiler/src/dmd/expressionsem.d#L9913-L9923
Comment #1 by kinke — 2022-09-15T18:47:37Z
Another testcase [straight from LDC], not depending on `emplace`: ``` int numPostblit = 0, numDtor = 0; struct S { int v; this(this) { ++numPostblit; } ~this() { ++numDtor; } } void foo() { S[4] sa = [ S(1), S(2), S(3), S(4) ]; // helper to generate a slice rvalue static S[] toSlice(ref S[4] sa) { return sa[1..$]; } S[3] r = toSlice(sa); } void main() { foo(); assert(numPostblit == 3); assert(numDtor == 7); } ```
Comment #2 by kinke — 2022-09-15T19:22:33Z
[The latter worked from 2.069 to 2.098, then regressed with v2.099.]
Comment #3 by dlang-bot — 2022-09-15T23:57:42Z
@kinke created dlang/dmd pull request #14442 "Fix Issue 23337 - Wrongly elided postblit/copy ctor for array construction (_d_arrayctor lowering)" fixing this issue: - Fix Issue 23337 - Wrongly elided postblit/copy ctor for array construction (_d_arrayctor lowering) https://github.com/dlang/dmd/pull/14442
Comment #4 by dlang-bot — 2022-10-06T02:34:10Z
dlang/dmd pull request #14442 "Fix Issue 23337 - Wrongly elided postblit/copy ctor for array construction (_d_arrayctor lowering)" was merged into master: - 6ee695676d5c122125a4b6611e2625c443572d29 by Martin Kinkelin: Fix Issue 23337 - Wrongly elided postblit/copy ctor for array construction (_d_arrayctor lowering) https://github.com/dlang/dmd/pull/14442