@safe - why does this compile?

Steven Schveighoffer schveiguy at yahoo.com
Fri Jul 13 14:22:10 UTC 2018


On 7/13/18 7:22 AM, ketmar wrote:
> Piotr Mitana wrote:
> 
>> This code:
>>
>>      import std.stdio;
>>
>>      class X1 {}
>>      class X2 : X1
>>      {
>>     void run() @safe
>>          {
>>              writeln("DONE");
>>          }
>>      }
>>
>>      void main() @safe
>>      {
>>          X1 x1 = new X1;
>>          X2 x2 = cast(X2) x1;
>>          x2.run();
>>      }
>>
>> is obviously wrong gets killed by OS's signal. Why is it @safe? I 
>> thought @safe should prevent such errors as well.
> 
> there is nothing wrong here. dereferencing null reference is completely 
> safe (in terms of result predictability).

To emphasize the point, this is @safe as well:

X2 x2; // = null
x2.run();

D does not consider a segmentation fault due to null dereferencing to be 
unsafe -- no memory corruption happens.

-Steve


More information about the Digitalmars-d-learn mailing list