https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123422
Bug ID: 123422
Summary: d: RVO/NRVO not done when returning a copy constructor
by __rvalue
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: d
Assignee: ibuclaw at gdcproject dot org
Reporter: ibuclaw at gcc dot gnu.org
Target Milestone: ---
RVO already done when returning a constructor call via a temporary, which in
the front-end AST looks like:
return S.this((__tmp = {}, &__tmp), args...);
Is rewritten into:
S.this((*<retval> = {}, <retval>), args...);
return <retval>;
However in the following test:
```
struct S
{
S* ptr;
this(int) { ptr = &this; }
this(ref inout S) { ptr = &this; }
this(S) { ptr = &this; }
}
struct V
{
S s;
this(int) { s = S(1); }
}
S foo()
{
return __rvalue(V(1).s); // here
}
void main()
{
S s = foo();
assert(&s == s.ptr);
}
```
At the marked location, we instead have:
return __copytmp = {}, S.this(&__copytmp, V(1).s), __copytmp;
This pattern should also be matched and rewritten in the same way.