Bug 20712 – Struct construction/assignment in static constructors is broken

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-03-31T08:49:31Z
Last change time
2024-12-13T19:07:58Z
Assigned to
No Owner
Creator
Max Samukha
See also
https://issues.dlang.org/show_bug.cgi?id=24449
Moved to GitHub: dmd#19687 →

Comments

Comment #0 by maxsamukha — 2020-03-31T08:49:31Z
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.
Comment #1 by nick — 2024-11-29T13:52:44Z
Yes, initializing a global from a static constructor should work like field initialization from a class constructor. That would also solve issue 24449.
Comment #2 by robert.schadek — 2024-12-13T19:07:58Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19687 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB