dip1000 and preview in combine to cause extra safety errors

Timon Gehr timon.gehr at gmx.ch
Wed Jun 8 18:32:41 UTC 2022


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 
guess you can complain about the language specification, but what else 
are you expecting `scope` to do? There could be some more diagnostics I 
guess, like for the case where a stack variable is escaped directly.


>> Some of the changes for DIP1000 made it to releases even without the 
>> switch, that's one example.
> 
> No, promoting the array on stack is not sufficient to explain the 
> behavior - thought it is certainly part of it.
> 
> The compiler is going out of his way in some other way to break the code.

It's reusing the same location on the stack for all instances of `[c]`. 
I think that's a pretty complete and straightforward explanation of the 
behavior. What is missing?

Anyway, this kind of issue is why one should never rely on undefined 
behavior giving a specific result; the compiler may get smart about it 
later.


More information about the Digitalmars-d mailing list