Bug 997 – [Regression] Struct-returning function that conditionally passes the result of another function straight through doesn't work (NRVO bug?)
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2007-02-22T15:42:00Z
Last change time
2014-02-16T15:23:03Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
smjg
Comments
Comment #0 by smjg — 2007-02-22T15:42:15Z
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)
----------