Bug 24577 – Struct with constructor returned from C++ wrong

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2024-06-01T13:59:21Z
Last change time
2024-06-08T13:47:38Z
Keywords
C++, pull
Assigned to
No Owner
Creator
Tim

Comments

Comment #0 by tim.dlang — 2024-06-01T13:59:21Z
//////////////// testcpp.cpp ////////////// struct S { int i; S(); S(int i); }; S::S() : i(0) { } S::S(int i) : i(i) { } S f() { S r; r.i = 5; return r; } //////////////// test.d //////////////////// import std.stdio; extern(C++) struct S { int i; this(int i); } extern(C++) S f(); void main() { S s = f(); writeln("test ", s); stdout.flush(); assert(s.i == 5); } //////////////////////////////////////////// The above example can be compiled on Windows with: cl /c testcpp.cpp dmd -g -w -m64 test.d testcpp.obj When running it will print a wrong random number: test S(-246417088) It only fails for DMD on Windows with -m64. It works correctly with -m32mscoff or on Linux or OSX.
Comment #1 by kinke — 2024-06-01T16:43:56Z
I'm pretty sure there's an existing issue about this. IIRC, it's backend specific, LDC working fine. It boils down to MSVC++ having separate POD semantics - any struct with an explicit constructor makes it a non-POD. Such types are returned via sret (caller passing a pointer to the pre-allocated result).
Comment #2 by dlang-bot — 2024-06-08T12:52:40Z
@tim-dlang created dlang/dmd pull request #16570 "Fix bugzilla 24577 - Struct with constructor returned from C++ wrong" fixing this issue: - Fix bugzilla 24577 - Struct with constructor returned from C++ wrong The test case is copied from LDC, where it already worked. It also has an additional test for a member struct with constructor. https://github.com/dlang/dmd/pull/16570
Comment #3 by dlang-bot — 2024-06-08T13:47:38Z
dlang/dmd pull request #16570 "Fix bugzilla 24577 - Struct with constructor returned from C++ wrong" was merged into master: - d8a684c435aca1f85fcb6a250beed5628284f777 by Tim Schendekehl: Fix bugzilla 24577 - Struct with constructor returned from C++ wrong The test case is copied from LDC, where it already worked. It also has an additional test for a member struct with constructor. https://github.com/dlang/dmd/pull/16570