-preview=in might break code

ag0aep6g anonymous at example.com
Mon Oct 5 18:35:04 UTC 2020


On 05.10.20 19:11, Walter Bright wrote:
> On 10/5/2020 12:56 AM, Iain Buclaw wrote:
>> Actually, I think there is zero mention of aliasing in the language 
>> spec, so the following can only be interpreted as being valid and 
>> precisely defined to work in D.
>> ---
>> float f = 1.0;
>> bool *bptr = cast(bool*)&f;
>> bptr[2] = false;
>> assert(f == 0.5);
>> ---
> 
> @safe code won't allow such a cast.

@safe allows the cast just fine. It doesn't allow the pointer 
arithmetic, but that can easily be worked around:

----
void main() @safe
{
     float f = 1.0;
     bool[4]* bptr = cast(bool[4]*) &f;
     (*bptr)[2] = false;
     assert(f == 0.5);
}
----

(Compile with `-preview=dip1000`, because `f` is on the stack.)


More information about the Digitalmars-d mailing list