Bug 7801 – Nested function returning garbage instead of closed-over parameter
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-03-30T14:53:00Z
Last change time
2013-02-01T00:51:02Z
Keywords
wrong-code
Assigned to
nobody
Creator
edmccard
Comments
Comment #0 by edmccard — 2012-03-30T14:53:02Z
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);
}
Comment #1 by clugdbug — 2013-02-01T00:51:02Z
*** This issue has been marked as a duplicate of issue 1841 ***