On Friday, 14 April 2023 at 09:42:14 UTC, Bastiaan Veelo wrote:
[...]
Up to dmd v2.100.2 I am warned/get an error during compilation:
```
$ dmd returnref2.d
returnref2.d(3): Deprecation: returning `i` escapes a
reference to parameter `i`
returnref2.d(1): perhaps annotate the parameter with
`return`
$ dmd -dip1000 returnref2.d
returnref2.d(3): Error: returning `i` escapes a reference to
parameter `i`
returnref2.d(1): perhaps annotate the parameter with
`return`
```
With later dmd versions (up to including v2.102.2) the code
compiles without complaints. Is this intended?
I think this is intended. Adding `@safe:` on top makes the
complaint come back (in dmd 2.102 it is deprecated, in 2.103
it is an error).
What irritates me is that only with `return ref int`
```
ref int foo (return ref int i)
{
return i;
}
[...]
```
dmd complains:
```
returnref2.d(9): Error: returning `foo(i)` escapes a reference to
local variable `i`
```
The documentation [1] says that a `ref` parameter may not be
returned unless it is `return ref`. But in fact it is returned
unless it is `return ref`. Probably i get/got the meaning of the
English modal verb "may" wrong.
The documentation also says that `return ref` parameters are used
to ensure that the returned reference will not outlive the
matching argument's lifetime. I don't get it! Is there any
legitimate use of returning a ref such that it outlives the
matching argument's lifetime? If not: Isn't this `return ref`
completely redundant?
[1] https://dlang.org/spec/function.html