auto ref function parameter causes that non copyable struct is copied?
Sebastiaan Koppe
mail at skoppe.eu
Wed Nov 10 10:38:34 UTC 2021
On Tuesday, 9 November 2021 at 19:30:20 UTC, tchaloupka wrote:
> On Monday, 8 November 2021 at 23:26:39 UTC, tchaloupka wrote:
>> Bug or feature? :)
>
> I've reported it in
> https://issues.dlang.org/show_bug.cgi?id=22498.
It doesn't copy. It just destructs the same object twice... Ouch.
Change the destructor to `~this() { printf("~this(%d)\n", n); n =
12; }` and you'll see.
I have been looking at LDC's IR of this. `unwrap` takes a lvalue,
but the compiler puts the object from `gen` on main's stack and
gives a pointer to unwrap instead. `unwrap` is now the sole owner
of the object, and calls the destructor before returning a ref to
one of its fields.
I haven't figured out the interplay of features and optimizations
here, but it looks like a leak. I would have expected @safe to
catch this.
As you said in the bug, NVRO solves it (not sure why). As well as
not passing a lvalue into `unwrap` in the first place.
More information about the Digitalmars-d-learn
mailing list