Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing post-blit calls when copying a static array from a slice. #1608

Closed
EyalIO opened this issue Jul 6, 2016 · 4 comments
Closed

Missing post-blit calls when copying a static array from a slice. #1608

EyalIO opened this issue Jul 6, 2016 · 4 comments

Comments

@EyalIO
Copy link

EyalIO commented Jul 6, 2016

This program exhibits buggy behavior:

import std.stdio:writeln;

struct Elem {
    int x = -1;
    this(int x) { this.x = x; writeln("CTOR ", x, " (", cast(void*)&this, ")"); }
    this(this) { writeln("POSTBLIT ", x, " (", cast(void*)&this, ")"); }
    ~this()    { if (x!=-1) writeln("DTOR "    , x, " (", cast(void*)&this, ")"); }
}

struct Ctr {
    Elem[3] arr;
    Elem[] slice() { return arr; }
    Elem[3] arrVal() { return arr; }
}

void main() {
    writeln("construct Ctr {");
    auto p = Ctr();
    writeln("}");
    writeln("assign arr {");
    p.arr = [Elem(1), Elem(2), Elem(3)];
    writeln("}");
    {
        writeln("slice rval -> arr {");
        Elem[3] _arr = p.slice;
        writeln("}");
    }
    {
        writeln("arr rval -> arr {");
        Elem[3] _arr = p.arrVal();
        writeln("}");
    }
}

This program fails to call POSTBLIT when copying from a slice-type, thus causing the number of CTOR/POSTBLITS to mismatch the number of DTORs.

Program's output:

construct Ctr {
}
assign arr {
CTOR 1 (7FFFCA8A2468)
CTOR 2 (7FFFCA8A2458)
CTOR 3 (7FFFCA8A2448)
}
slice rval -> arr {
}
DTOR 3 (7FFFCA8A2410)
DTOR 2 (7FFFCA8A240C)
DTOR 1 (7FFFCA8A2408)
arr rval -> arr {
POSTBLIT 1 (7FFFCA8A2370)
POSTBLIT 2 (7FFFCA8A2374)
POSTBLIT 3 (7FFFCA8A2378)
}
DTOR 3 (7FFFCA8A2400)
DTOR 2 (7FFFCA8A23FC)
DTOR 1 (7FFFCA8A23F8)
DTOR 3 (7FFFCA8A24B0)
DTOR 2 (7FFFCA8A24AC)
DTOR 1 (7FFFCA8A24A8)

Note the missing POSTBLIT printouts under slice rval -> arr {...

@EyalIO
Copy link
Author

EyalIO commented Jul 6, 2016

Similar, but different buggy behavior for the same program in dmd: https://issues.dlang.org/show_bug.cgi?id=16197

@dnadlinger
Copy link
Member

@kinke: I guess the correct fix might be immediately obvious to you right now, after working on the other lifetime issues? The 3 elements are not necessary, by the way, just using 1 makes the output (and IR) easier to read.

@kinke
Copy link
Member

kinke commented Jul 6, 2016

The fault is that in visit(AssignExp*), we detect the rhs as rvalue and hence pass canSkipPostblit = true to DtoAssign() -> DtoArrayAssign(). The slice itself returned by p.slice() is an rvalue, but slices need to be treated as lvalues here.

kinke added a commit to kinke/ldc that referenced this issue Jul 6, 2016
kinke added a commit to kinke/ldc that referenced this issue Jul 27, 2016
@kinke kinke mentioned this issue Jul 27, 2016
dnadlinger added a commit that referenced this issue Jul 29, 2016
@kinke
Copy link
Member

kinke commented Jul 29, 2016

Fixed by #1609.

@kinke kinke closed this as completed Jul 29, 2016
redstar pushed a commit to redstar/ldc that referenced this issue Jul 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants