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: nob...@puremagic.com
        ReportedBy: qwe...@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: -------

Reply via email to