On Thu, Feb 26, 2015 at 11:35:55PM -0800, Walter Bright via Digitalmars-d wrote:
> On 2/26/2015 5:37 PM, H. S. Teoh via Digitalmars-d wrote:
> >Wait, are you saying that forgetting the 'return' annotation will
> >still compile without any warning?? Wow, that's ... not nice. :-(
> 
> Indeed, and the compiler will error out if you try that:
> 
> ---
> struct S {
>     int x;
> }
> 
> ref int foo(ref S s) {
>     return s.x;
> }
> ---
> 
> dmd -c foo -dip25
> foo.d(7): Error: escaping reference to local ref variable s
> 
> There'd be no point to DIP25 if it didn't.

Huh? We weren't talking about that -- it's already a given that this
case works, otherwise DIP25 would truly have no point.

The problematic case comes from the manually-managed array member that
you can freely return, yet it may become a dangling reference once
opRelease deallocates it:

        class S {
                int[] data;
                int[] foo() { return data; } // <-- happily compiles
                void opAddRef() { ... }
                void opRelease() {
                        ...
                        GC.free(data); // <-- uh oh
                }
        }

        int[] fun() {
                S s = /* create instance of S */
                return s.foo();
                // s goes out of scope here, and opRelease() cleans up
                // but we now have an escaping reference to s.data
        }

        void main() {
                int[] dangling = fun();
                dangling[0] = 1;        // kaboom
        }


S.foo() should have been annotated with 'return', but the programmer
forgot and the compiler still accepts the code without any warnings,
thereby violating @safe.


T

-- 
Bomb technician: If I'm running, try to keep up.

Reply via email to