[Issue 19903] postblit called for uninitialised elements of unions
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue May 28 11:17:57 UTC 2019
https://issues.dlang.org/show_bug.cgi?id=19903
RazvanN <razvan.nitu1305 at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |razvan.nitu1305 at gmail.com
--- Comment #3 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to Manu from comment #0)
> This assignment calls the T postblit, which is a `void` initialised member
> of a union.
> This is just a hard crash waiting to happen!
>
> This pattern should be supplanted with copy-ctors.
> I suggest a reasonable resolution is:
> 1. no such implicit non-trivial assignment is attempted inside a union
> 2. if a union contains an element with a non-trivial copy, then a
> copy-ctor must be defined otherwise user receives a compile error informing
> them such.
> 2a. perhaps rather than emitting a compile error, it might be better to
> emit an automatic @disable copy constructor for that object.
>
> I think it's the case that any union with elaborate copy semantics can only
> be correctly composed by the author of the union.
Spec:
"Unions may have fields that have postblits. However, a union itself never has
a postblit. Copying a union does not result in postblit calls for any fields.
If those calls are desired, they must be inserted explicitly by the
programmer."
It seems that it has a problem with anonymous unions. Slightly changing your
program results in corect behavior:
import std.stdio;
struct T
{
this(this) { writeln("dun postblit\n"); }
float f = 0;
}
struct S
{
union U { // the union is named now
int x = 0;
T y = void; // <- void initialised
}
U u; // and we have a field
int which = 0;
}
S a;
S b = a; // <- assignment calls postblit
--
More information about the Digitalmars-d-bugs
mailing list