Discussion Thread: DIP 1035-- at system Variables--Community Review Round 2
Steven Schveighoffer
schveiguy at gmail.com
Sat Feb 27 19:05:01 UTC 2021
On 2/27/21 10:55 AM, Paul Backus wrote:
> On Saturday, 27 February 2021 at 15:21:41 UTC, Steven Schveighoffer wrote:
>> On 2/27/21 8:32 AM, Paul Backus wrote:
>>> You *can* get the compiler to treat any value as unsafe by
>>> overlapping it with a pointer; e.g.,
>>>
>>> struct Unsafe(T)
>>> {
>>> union Storage { T value; void* dummy; }
>>> Storage storage;
>>> ref T get() { return Storage.value; }
>>> /* ... */
>>> }
>>>
>>> But this introduces memory overhead for any T smaller than a pointer,
>>> which is not ideal.
>>
>> Better mark that get as @system, or it can be used in @safe code.
>
> It's in a template, so it will be inferred as @system.
D considers that @safe. It was a whole section of my Dconf online talk.
I filed a bug report on it (in which you seem to think this is OK):
https://issues.dlang.org/show_bug.cgi?id=21565
Even without templates:
struct DThinksThisIsSafe
{
union Storage { int value; void *dummy; }
Storage storage;
@safe ref int get() return { return storage.value; } // compiles
just fine
}
-Steve
More information about the Digitalmars-d
mailing list