www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2509] New: You can not create the function returns ref instead of statement

reply d-bugmail puremagic.com writes:
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;
}


-- 
Dec 12 2008
next sibling parent d-bugmail puremagic.com writes:
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





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;
}


-- 
Dec 12 2008
prev sibling parent d-bugmail puremagic.com writes:
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



*** 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: -------
Jun 06 2011