[Issue 3270] New: pure functions returning struct

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Aug 28 13:54:21 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=3270

           Summary: pure functions returning struct
           Product: D
           Version: 2.031
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: qwerty at mailinator.com


If a pure function tries to return a struct, the return value becomes garbage.

Example:

struct Foo
{
    int x, y
    real z;
}

pure Foo makeFoo(const int x, const int y)
{
    return Foo(x, y, 3.0);
}

int main()
{
    auto f = makeFoo(1, 2);
    writeln(f.x, f.y, f.z);
}

Possible cause:

The compiler might be optimizing makeFoo to

pure void makeFoo(ref Foo f, const int x, const int y)
{
    f = Foo(x, y, 3.0);
}

in order to prevent returning the entire struct on the stack. Since makeFoo is
pure, this optimization breaks the program.

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