www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23444] New: Can't append non-copyable struct value to an array

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

          Issue ID: 23444
           Summary: Can't append non-copyable struct value to an array
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

As of DMD 2.100.2, the following program fails to compile:

---
struct NoCopy
{
     disable this(this);
}

void main()
{
    NoCopy[] a;
    a ~= NoCopy(); // error
}
---

The error message is:

---
bug.d(9): Error: struct `bug.NoCopy` is not copyable because it has a disabled
postblit
---

However, this error is spurious. The program below compiles without errors and
produces no output when run, because the postblit is not actually called at
runtime:

---
struct NoCopy
{
    this(this)
    {
        import std.stdio;
        writeln("copied");
    }
}

void main()
{
    NoCopy[] a;
    a ~= NoCopy(); // ok; no output
}
---

--
Oct 29 2022