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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Mar 30 14:52:34 PDT 2012


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 at puremagic.com
        ReportedBy: edmccard at verizon.net


--- Comment #0 from edmccard at verizon.net 2012-03-30 14:53:02 PDT ---
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: -------


More information about the Digitalmars-d-bugs mailing list