Bug 7130 – NRVO Bug: Wrong Code With D'tor + Conditional Return

Status
RESOLVED
Resolution
DUPLICATE
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2011-12-18T17:23:00Z
Last change time
2012-05-10T23:05:16Z
Keywords
wrong-code
Assigned to
nobody
Creator
dsimcha

Comments

Comment #0 by dsimcha — 2011-12-18T17:23:26Z
import core.stdc.stdio; struct S { this(this) { printf("Postblit\n"); } ~this() { printf("D'tor\n"); } } S doIt(int i) { S s1; S s2; printf("s1 lives at %p.\n", &s1); printf("s2 lives at %p.\n", &s2); return (i == 42) ? s1 : s2; } void main() { auto s = doIt(3); printf("s lives at %p.\n", &s); } Output: s1 lives at 0xffc54368. s2 lives at 0xffc54369. D'tor D'tor s lives at 0xffc5437c. D'tor Both D'tors are called and the returned result lives at a different address after being returned than before, as expected if not using NRVO. On the other hand, no postblit being called for whichever struct is returned, as expected if using NRVO.
Comment #1 by k.hara.pg — 2012-05-10T23:05:16Z
(In reply to comment #0) > Both D'tors are called and the returned result lives at a different address > after being returned than before, as expected if not using NRVO. On the other > hand, no postblit being called for whichever struct is returned, as expected if > using NRVO. The function 'doit' cannot NRVO, because &s1 and &s2 should have different addresses. S doIt(int i) { S s1; S s2; printf("s1 lives at %p.\n", &s1); printf("s2 lives at %p.\n", &s2); return (i == 42) ? s1 : s2; // postblit should run } I'll make this a dup of bug 7516. I have posted more better test code in there. *** This issue has been marked as a duplicate of issue 7516 ***