[Issue 22045] Assignment of pointers in union should be @safe, only pointers access are @system

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jul 8 15:31:46 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22045

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305 at gmail.com> ---
(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.

--


More information about the Digitalmars-d-bugs mailing list