Bug 23239 – Type with destructor returned from helper function moved without postblit or copyctor

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-07-11T09:31:38Z
Last change time
2024-12-13T19:23:47Z
Keywords
industry
Assigned to
No Owner
Creator
FeepingCreature
Moved to GitHub: dmd#20129 →

Comments

Comment #0 by default_357-line — 2022-07-11T09:31:38Z
Consider this code: import std.stdio; @safe: void main() { auto value = make; writefln!"field is at %s"(&value.field); value.field = 5; assert(value.dg() == 5); } Test make() { return Test(0); } struct Test { int field; int delegate() dg; @disable this(this); @disable this(ref Test); this(int) scope { this.dg = { writefln!"get field at %s"(&this.field); return this.field; }; } ~this() { writefln!"Now destroyed."; } } Struct Test has forbidden any form of copying or moving via `@disable this`. We'd assume that either NRVO and inlining would let the constructor be called with `this` at the final location in `with (make)` in main, or some part of this would error. However, even with -dip1000 in master (v2.100.0-8763-gcdfadf8a18), the compiler calls `this(int)` with a different address than `value` in main, and never tries to call postblit or the copy constructor. As a result, the assert fails.
Comment #1 by robert.schadek — 2024-12-13T19:23:47Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20129 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB