[Issue 12885] const union wrongly converts implicitly to mutable

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun May 15 01:51:27 UTC 2022


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

Steven Schveighoffer <schveiguy at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at gmail.com

--- Comment #6 from Steven Schveighoffer <schveiguy at gmail.com> ---
I just ran into this, with vibe.d Json type. Well, not a problem exactly, I
just noticed I happened to be stripping const by accident, and it alarmed me
that the compiler lets you get away with it.

I will note that this fails:

```d
union U
{
   int *p;
   int i;
}
void main()
{
   const U c;
   U m = c;
}
```

I see no reason why this should fail but the other should not, the
inconsistency itself is a problem.

But beyond that, I will note that a struct that contains such an overlapping is
convertible from const to mutable in @safe code. This in itself isn't a problem
since you can't access the pointer value in @safe code. But surely if you
wanted to provide access for @safe callers (like, for a tagged union, like
Json), there would be trusted escapes, and those would surely not expect an
implicit conversion of a const pointer to a mutable pointer.

--


More information about the Digitalmars-d-bugs mailing list