www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23239] New: Type with destructor returned from helper

https://issues.dlang.org/show_bug.cgi?id=23239

          Issue ID: 23239
           Summary: Type with destructor returned from helper function
                    moved without postblit or copyctor
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: default_357-line yahoo.de

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.

--
Jul 11 2022