Returning a reference to be manipulated

Bastiaan Veelo Bastiaan at Veelo.net
Fri Apr 14 09:42:14 UTC 2023


On Friday, 14 April 2023 at 00:50:31 UTC, kdevel wrote:
> ```
> ref int foo (ref int i)
> {
>    return i;
> }
>
> ref int bar ()
> {
>    int i;
>    return foo (i);
> }
>
> void main ()
> {
>    import std.stdio;
>    auto i = bar;
>    i.writeln;
> }
> ```
>
> 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).

-- Bastiaan.


More information about the Digitalmars-d-learn mailing list