www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3270] New: pure functions returning struct

reply d-bugmail puremagic.com writes:
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 puremagic.com
        ReportedBy: qwerty 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: -------
Aug 28 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3270


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #1 from Don <clugdbug yahoo.com.au> 2009-10-28 03:01:58 PDT ---
If my patch to bug 3269 is in place, the functions will need to be "pure
nothrow" in order to reproduce the bug.
The bug clearly lies in the handling of OPucallns. It's failing to deal with
the 'hidden parameter'.
In e2ir.c, line 290, if you change it to ALWAYS use OPucall instead of
OPucallns, the problem disappears. But that's pretty drastic. The bug lies in
the back-end somewhere.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 28 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3270


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #2 from Don <clugdbug yahoo.com.au> 2009-12-30 13:11:55 PST ---
Shouldn't be doing no-side-effect calls if there's a hidden parameter.
This happens if the value is being returned on the stack.

PATCH (e2ir.c line 287):

    else if (ep)
-    e = el_bin((tf->ispure && tf->isnothrow) ? OPcallns : OPcall,tyret,ec,ep);
+    e = el_bin((tf->ispure && tf->isnothrow && (retmethod != RETstack)) ?
OPcallns : OPcall,tyret,ec,ep);
    else
-    e = el_una((tf->ispure && tf->isnothrow) ? OPucallns : OPucall,tyret,ec);
+    e = el_una((tf->ispure && tf->isnothrow && (retmethod != RETstack)) ?
OPucallns : OPucall,tyret,ec);

-------
TEST CASE:

struct Foo {
    int x, y;
    real z;
}

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


void main()
{
    auto f = makeFoo(1, 2);
   assert(f.x==1);
   assert(f.y==2);
   assert(f.z==3);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 30 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3270


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com


--- Comment #3 from Walter Bright <bugzilla digitalmars.com> 2009-12-30
17:38:24 PST ---
Changeset 323

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 30 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3270


Walter Bright <bugzilla digitalmars.com> changed:

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


--- Comment #4 from Walter Bright <bugzilla digitalmars.com> 2009-12-31
11:18:57 PST ---
Fixed dmd 2.038

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 31 2009