Dealing with the interior pointers bug

Cym13 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 22 12:11:19 PDT 2017


On Thursday, 22 June 2017 at 18:38:59 UTC, Boris-Barboris wrote:
> On Thursday, 22 June 2017 at 13:56:29 UTC, ag0aep6g wrote:
>> For example, the type system guarantees that immutable data 
>> never changes. But the compiler allows you to cast from 
>> immutable to mutable and change the data. It's an invalid 
>> operation, but the compiler is not expected to catch that for 
>> you.
> Casts are part of the type system. Yes, D type system allows 
> invalid operations. It's not the compiler's fault, it's type 
> system's fault.
>
> unittest
> {
>     immutable int a = 4;
>     int* b = cast(int*) &a;
>     *b = 5;
>     assert(*(&a) == 5);
>     assert(a == 4);
> }

Here it's the programmer's fault really. You should never use 
casts in normal code, cast is the ultimate switch to say "Look, I 
know what I'm doing, so disable all safety, don't try to make 
sense of it, and let me do my thing. If I'm telling you it's a 
cat, then it is dammit.". You can't blame the type system not to 
do something coherent here, you explicitely went out of your way 
to lie to that very same type system in the most unsafe way 
possible.


More information about the Digitalmars-d-learn mailing list