D not considered memory safe
Walter Bright
newshound2 at digitalmars.com
Fri Jul 5 16:41:01 UTC 2024
On 7/3/2024 6:52 PM, Steven Schveighoffer wrote:
> ```d
> ref int bar(ref int x) => x;
>
> ref int foo()
> {
> int i = 0;
> return bar(i);
> }
> ```
>
> Compiles, even with dip1000.
```
@safe: // <=== I added this line
ref int bar(ref int x) => x;
ref int foo()
{
int i = 0;
return bar(i);
}
```
fails with:
```
test.d(3): Error: returning `x` escapes a reference to parameter `x`
test.d(3): perhaps annotate the parameter with `return`
```
when compiled with -preview=dip1000
More information about the Digitalmars-d
mailing list