Thread overview
[Issue 2509] New: You can not create the function returns ref instead of statement
Dec 12, 2008
d-bugmail
[Issue 2509] Compiler rejects inner function that return references
Dec 13, 2008
d-bugmail
Jun 07, 2011
yebblies
December 12, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2509

           Summary: You can not create the function returns ref instead of
                    statement
           Product: D
           Version: 2.021
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: resume755@mail.ru


this code causes the error:

void main()
{
    int i;

    ref int func()
    {
      return i;
    }

    func() = 4;
}

lval.d(5): found 'ref' instead of statement
lval.d(10): no identifier for declarator func
lval.d(11): unrecognized declaration


but code like this compiles:

ref int func()
{
  int* i = new int;
  return *i;
}

void main()
{
    func() = 4;
}


-- 

December 13, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2509


2korden@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |2korden@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@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;
}


-- 

June 07, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=2509


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |DUPLICATE


--- Comment #2 from yebblies <yebblies@gmail.com> 2011-06-06 21:28:24 PDT ---
*** This issue has been marked as a duplicate of issue 5959 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------