https://issues.dlang.org/show_bug.cgi?id=22045
RazvanN <[email protected]> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |[email protected] --- Comment #1 from RazvanN <[email protected]> --- (In reply to Luís Ferreira from comment #0) > This code should successfully compile: > > void main() @safe > { > union Foo { > int a; > int* b; > } > Foo foo; > foo.b = new int; > } > > Nothing unsafe here. > > It worked in DMD 2.065.0 to 2.071.2. > I don't really see the point in special casing union pointer assignment when you cannot access them. I would argue that unions are unsafe by their very nature and should be banned completely in @safe code, rather than allowing more cases. > More examples: > > void main() @safe > { > union Foo { > int a; > int* b; > } > Foo foo; > int* c = (foo.b = new int); > } > > This, however shouldn't compile: > > void main() @safe > { > union Foo { > int a; > int* b; > } > Foo foo; > int* c = foo.b; > } To be able to distinguish between this 2 cases, dataflow analysis is required in the compiler to assure the integrity of the b pointer. Walter is against adding such dataflow analysis in the compiler and frankly I stand by him in this case. My intuition is that this sort of access was initially banned, but bug reports such as this one asked for the feature therefore it was introduced and people saw that it does more harm than good and decided to revert it. I think that this bug report should be closed as invalid/wontfix. --
