[Issue 997] New: [Regression] Struct-returning function that conditionally passes the result of another function straight through doesn't work (NRVO bug?)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Feb 22 13:42:22 PST 2007


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

           Summary: [Regression] Struct-returning function that
                    conditionally passes the result of another function
                    straight through doesn't work (NRVO bug?)
           Product: D
           Version: 1.007
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: smjg at iname.com


I found this when one of my programs had stopped working.  Clearly there is
work to be done to get NRVO behaving itself.

----------
import std.stdio, std.string;

struct Rect {
    int left, top, right, bottom;
}

int main() {
    print(sizeTest(false));
    print(sizeTest(true));
    print(defaultRect);
    return 0;
}

static Rect sizeTest(bool empty) {
    if (empty) {
        Rect result;
        return result;
        //return Rect.init;
    } else {
        return defaultRect;
        /+Rect result = defaultRect;
        return result;+/
    }
}

void print(Rect r) {
    writefln("(%d, %d)-(%d, %d)", r.left, r.top, r.right, r.bottom);
}

Rect defaultRect() {
    return Rect.init;
}
----------
(1, 4212215)-(0, 4291676)
(0, 0)-(0, 0)
(0, 0)-(0, 0)
----------

If I remove the if, or use either of the commented out portions instead, then I
get the expected output:

----------
(0, 0)-(0, 0)
(0, 0)-(0, 0)
(0, 0)-(0, 0)
----------


-- 



More information about the Digitalmars-d-bugs mailing list