www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20712] New: Struct construction/assignment in static

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

          Issue ID: 20712
           Summary: Struct construction/assignment in static constructors
                    is broken
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: maxsamukha gmail.com

import std.stdio;

struct S {
    this(int x) {
        writeln("ctor");
    }

    ref S opAssign(ref const S s) {
        writeln("assign");
        return this;
    }

    ~this() {
        writeln("dtor");
    }
}

S s;
static this() {
    s = S(1); // (a)
    s = S(1); // (b)
    auto s2 = S(1);
    s = s2; // (c)
}

void main()
{
}

(a) Constructor is correctly called on the "branded" struct.

(b) Bug: s is "branded" and constructed again. The original value doesn't get
destructed. A correct behavior would be to try opAssign and fail because of the
rvalue-to-ref mismatch.

(c) Bug: s2 is simply blitted to s. A correct behavior would be to call
opAssign.

--
Mar 31