www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15869] New: RVO can overwrite argument

https://issues.dlang.org/show_bug.cgi?id=15869

          Issue ID: 15869
           Summary: RVO can overwrite argument
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: yshuiv7 gmail.com

Test case:

struct Set {
    void insert(ulong v) {
        aa[v] = true;
    }
     disable this(this);
    bool[ulong] aa;
}
auto clobber(ref Set x, ref Set o) {
    Set ret; // <- This overwrites x
    ret.aa = x.aa; // <- Now x.aa is empty
    return ret;
}
struct XX {
    Set a, b, tmp;
    this(int n) {
        a.insert(1);
        //a.aa[1] = true;  <- Swap above line with this doesn't trigger the bug
        tmp = a.clobber(b);
        a = a.clobber(b);
    }
}
void main(){
    import std.stdio;
    XX xx = XX(0);
    writeln(xx.a.aa.length); // 0
    writeln(xx.tmp.aa.length); // 1
}

--
Apr 03 2016