[Issue 2509] Compiler rejects inner function that return references

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Dec 12 16:11:32 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2509


2korden at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |2korden at gmail.com
            Summary|ref return doesn't work for |Compiler rejects inner
                   |inner function (found 'ref' |function that return
                   |instead of statement )      |references




------- Comment #1 from 2korden at gmail.com  2008-12-12 18:11 -------
The following code doesn't work either:

void main()
{
    int i;

    int* func() {
        return &i; // Error: escaping reference to local variable i
    }
}

Replace a pointer with a reference and you get the same semantics and thus your
code wont work, too.

An errot message reminds me about escape analysis discussion past month and I
think that this behaviour needs some additional elaboration from Walter because
it never was announces nor documented.

The analysis implemente, however, is not too smart as there are ways to trick
it:

void main()
{
    int i;

    void func(ref int* p)
    {
        p = &i; // Okay, no complains here
    }
}

Other than this, looks like compiler isn't yet aware of inner functions that
return references (it doesn't expect to find a 'ref' keyword at a function
level). It makes the following valid code fail to compile:

void main()
{
    ref int func()
    {
        static int foo;
        return foo;
    }

    func = 4;
}


-- 



More information about the Digitalmars-d-bugs mailing list