www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7801] New: Nested function returning garbage instead of closed-over parameter

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7801

           Summary: Nested function returning garbage instead of
                    closed-over parameter
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: edmccard verizon.net



I ran into a strange and hard-to-describe problem with nested functions closing
over the argument to their enclosing function.

When a nested function (A) returns the value of another nested function (B)
that returns a parameter of the enclosing function (C), and when (A) is
returned from (C), then calling (A) returns an incorrect value if either run
from a 32-bit program, or run from 64-bit and (A) has a parameter of class type
(it works when (A) has no class parameters).

The following code demonstrates the issue:


auto foo(T)(int val)
{
    int nested()
    {
        return val;
    }

    int escaping(T ignored)
    {
        return nested();
    }

    return &escaping;
}

struct Bar {}

class Baz {}

void main()
{
    auto func1 = foo!int(55);
    auto val1 = func1(12);
    // 64-bit ok, 32-bit fail (varying)
    assert(val1 == 55);

    auto func2 = foo!Bar(55);
    Bar bar;
    auto val2 = func2(bar);
    // 64-bit ok, 32-bit fail (equal to incorrect val1)
    assert(val2 == 55);

    auto func3 = foo!Baz(55);
    auto baz = new Baz();
    auto val3 = func3(baz);
    // 64- and 32-bit fail (varying, different from val1/val2)
    assert(val3 == 55);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 30 2012
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7801


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE



*** This issue has been marked as a duplicate of issue 1841 ***

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