D not considered memory safe

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Thu Jul 4 02:00:52 UTC 2024


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`
```


More information about the Digitalmars-d mailing list