Related: 9666
Given:
import core.stdc.stdio;
struct S {
this(ref S) { printf("this(ref S)\n"); }
void opAssign(ref S) { printf("opAssign(ref S)\n"); }
void opAssign(S) { printf("opAssign(S)\n"); }
void opMove(ref S) { printf("opMove(ref S)\n"); }
}
void main() {
S s;
S t;
S test() { S* p = &s; return *p; }
t = test();
}
The code generated for main() is:
_Dmain:
push RBP
mov RBP,RSP
sub RSP,010h
xor EAX,EAX
mov -8[RBP],AL // S s;
mov -7[RBP],AL // S t;
lea RSI,-6[RBP] // address of struct return value
mov RDI,RBP // static link
call test
mov RSI,RAX // address of struct return value as source
lea RDI,-5[RBP] // address of temporary
movsb // copy return value to temporary
lea RSI,-5[RBP] // address of temporary
lea RDI,-7[RBP] // address of t
call opAssign // opAssign(S)
xor EAX,EAX
leave
ret
The copy is not necessary, and is a bug.
Comment #1 by dlang-bot — 2024-10-27T03:32:24Z
@WalterBright created dlang/dmd pull request #17033 "fix bugzilla Issue 24836 - struct return by hidden struct arg does no…" fixing this issue:
- fix bugzilla Issue 24836 - struct return by hidden struct arg does not need copy to become an rvalue
https://github.com/dlang/dmd/pull/17033
Comment #2 by dlang-bot — 2024-10-29T18:20:12Z
dlang/dmd pull request #17033 "fix bugzilla Issue 24836 - struct return by hidden struct arg does no…" was merged into master:
- 252291627196b0bcc9bc836bf94ad4adb2c46e5a by Walter Bright:
fix bugzilla Issue 24836 - struct return by hidden struct arg does not need copy to become an rvalue
https://github.com/dlang/dmd/pull/17033