D not considered memory safe
Steven Schveighoffer
schveiguy at gmail.com
Thu Jul 4 18:05:59 UTC 2024
On Thursday, 4 July 2024 at 02:00:52 UTC, Richard (Rikki) Andrew
Cattermole wrote:
> On 04/07/2024 1:52 PM, Steven Schveighoffer wrote:
>> |ref int bar(ref int x) => x; ref int foo() { int i = 0;
>> return bar(i); } |
>>
>> Compiles, even with dip1000.
>
> ``-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`
> ```
I'm aware. Not writing `@safe:` at the top was intentional.
-Steve
More information about the Digitalmars-d
mailing list