On Thu, 15 Aug 2024 at 00:31, kinke via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
On Wednesday, 14 August 2024 at 13:56:08 UTC, Manu wrote:
> Well, the condition is that NRVO should elide the copy/move...
> it should be
> constructed at the caller's scope in this case.
> Your example `Thing(null).field` is not the same thing, because
> it's not
> NRVO at all.
>
> I guess this is a bug then?

When actually using NRVO (`auto r = Thing(null); return r;`), one
even gets the error *twice*. So that's definitely a bug, the
destruction is handled by the caller; just for the attributes
check etc., there's no real dtor call in `fun()`.

What you have is a case for RVO; AFAIK, LDC and GDC implement
that (for non-POD types at least), no idea about DMD. Meaning
that the temporary in the return expression isn't destructed by
`foo` either; it's emplaced directly into the caller-allocated
return value, as the NRVO case.

My understanding is that RVO is in the D spec and NOT simply an optimisation. The language shouldn't attempt to call a destructor here in my case under any circumstances...

Walter?