dip1000 and preview in combine to cause extra safety errors

Timon Gehr timon.gehr at gmx.ch
Wed Jun 8 19:01:04 UTC 2022


On 6/8/22 20:44, 12345swordy wrote:
> On Wednesday, 8 June 2022 at 18:32:41 UTC, Timon Gehr wrote:
>> On 6/8/22 19:22, deadalnix wrote:
>>> On Wednesday, 8 June 2022 at 17:09:49 UTC, Mathias LANG wrote:
>>>> And you'll see the bug, even without `-preview=dip1000`.
>>>>
>>>> Why is this happening ? You correctly guessed, because the frontend 
>>>> wrongfully lets the `string` go on the stack instead of allocating 
>>>> with it.
>>>> ...
>>
>> Your code is literally calling this function:
>>
>> ```d
>> string foo(scope string s){ return s; }
>> ```
>>
>> This causes UB, therefore you can't blame the compiler frontend here.
> 
> I got to say here, you shouldn't be able to compile that code at all if 
> it is going to shoot you in the foot unintentionally.
> 
> - Alex

Well, I agree that in simple cases like this one, the compiler should 
just complain. In general though, it won't understand what's going on. 
If you want to catch everything, you'll have to use @safe, but that will 
also reject some things that are actually fine.


More information about the Digitalmars-d mailing list