D not considered memory safe
Lance Bachmeier
no at spam.net
Thu Jul 4 16:11:56 UTC 2024
On Thursday, 4 July 2024 at 07:11:13 UTC, Sebastian Nibisz wrote:
> On Thursday, 4 July 2024 at 02:00:52 UTC, Richard (Rikki)
> Andrew Cattermole wrote:
>> ``-preview=dip1000``
>>
>> ```d
>> @safe:
>>
>> ref int bar(ref int x) => x;
>>
>> ref int foo()
>> {
>> int i = 0;
>> return bar(i);
>> }
>>
>> void main() {
>> int* v = &foo();
>> }
>> ```
>>
>> ```
>> <source>(3): Error: returning `x` escapes a reference to
>> parameter `x`
>> <source>(3): perhaps annotate the parameter with
>> `return`
>> ```
>>
>> And when annotated:
>>
>> ```
>> <source>(8): Error: returning `bar(i)` escapes a reference to
>> local variable `i`
>> ```
>
> An unsafe programming language is one that allows to write
> unsafe code by default. If D is to be more safe, then @safe
> should be the default and @unsafe optional.
Safety isn't important to anyone that can't be troubled to write
`-preview=dip1000`.
More information about the Digitalmars-d
mailing list