@safe accessing of union members

Paul Backus snarwin at gmail.com
Wed Mar 17 19:27:29 UTC 2021


On Wednesday, 17 March 2021 at 19:09:18 UTC, ag0aep6g wrote:
> On Wednesday, 17 March 2021 at 18:01:43 UTC, Paul Backus wrote:
>> On Wednesday, 17 March 2021 at 16:43:25 UTC, Q. Schroll wrote:
> [...]
>>> When should the language (conservatively) assume an aggregate 
>>> type (struct, class, etc.) has invariants?
>>
>> The rules in the language spec [2] are mostly correct in this 
>> regard, though they leave out `bool` (and enum types, though 
>> that's a more debatable issue).
> [...]
>> [2] https://dlang.org/spec/function.html#safe-values
>
> I left out bool deliberately. The impression I get from Walter 
> is that he only considers indirections to be potentially unsafe 
> [1]. In anticipate him addressing issue by defining 0 = false, 
> anything else = true. Then all bit patterns are safe, and it's 
> just a matter of fixing code that expects only 0 or 1.
>
>
> [1] https://github.com/dlang/dlang.org/pull/2260

IIRC "code that expects only 0 or 1" includes things like the GCC 
and LLVM backends, so it may be worth some additional 
consideration, especially if we want D's bool to interface 
correctly with C99 _Bool and/or C++ bool.


More information about the Digitalmars-d mailing list