Bug 19793 – no postblit is called if cast is used for structs

Status
RESOLVED
Resolution
WONTFIX
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-04-08T11:27:58Z
Last change time
2019-04-30T15:40:04Z
Keywords
pull, wrong-code
Assigned to
No Owner
Creator
Илья Ярошенко

Comments

Comment #0 by ilyayaroshenko — 2019-04-08T11:27:58Z
struct S(T) { size_t* counter; this(this) { if (counter) ++counter[0]; } ~this() { if (counter) --counter[0]; } static S create() { return S(new size_t(1)); } } auto constOf(S!int a) { return cast(S!(const int)) a; } void main() { auto s = S!int.create; assert(s.counter[0] == 1); // pass auto a = constOf(s); assert(s.counter[0] == 2); // fails }
Comment #1 by ilyayaroshenko — 2019-04-08T11:29:54Z
Comment #2 by john.loughran.colvin — 2019-04-08T12:01:48Z
unlike 19774, this fails as far back as 2.060 (even if you fix the "no constructor for ulong" error you will get when you go back that far)
Comment #3 by razvan.nitu1305 — 2019-04-09T08:11:54Z
(In reply to Илья Ярошенко from comment #0) > struct S(T) > { > size_t* counter; > > this(this) > { > if (counter) > ++counter[0]; > } > > ~this() > { > if (counter) > --counter[0]; > } > > static S create() > { > return S(new size_t(1)); > } > } > > auto constOf(S!int a) > { > return cast(S!(const int)) a; > } > > void main() > { > auto s = S!int.create; > assert(s.counter[0] == 1); // pass > auto a = constOf(s); > assert(s.counter[0] == 2); // fails > } Up untile recently, casts were considered rvalues in dmd while postblits get called solely on lvalues. However this was recently changed in the specs [1] so that `cast()` and `cast(qualifier)` are lvalues. A PR was submitted to get the implementation in sync with the spec [2], so I suspect that that will also fix this issue. [1] https://github.com/dlang/dlang.org/pull/2606 [2] https://github.com/dlang/dmd/pull/9505
Comment #4 by dlang-bot — 2019-04-22T09:39:51Z
@RazvanN7 updated dlang/dmd pull request #9505 "Fix Issue 19754 - cast() sometimes yields lvalue, sometimes yields rvalue" fixing this issue: - Fix Issue 19754 and 19793 - cast() sometimes yields lvalue, sometimes yields rvalue https://github.com/dlang/dmd/pull/9505
Comment #5 by razvan.nitu1305 — 2019-04-24T09:08:39Z
(In reply to RazvanN from comment #3) > (In reply to Илья Ярошенко from comment #0) > > struct S(T) > > { > > size_t* counter; > > > > this(this) > > { > > if (counter) > > ++counter[0]; > > } > > > > ~this() > > { > > if (counter) > > --counter[0]; > > } > > > > static S create() > > { > > return S(new size_t(1)); > > } > > } > > > > auto constOf(S!int a) > > { > > return cast(S!(const int)) a; > > } > > > > void main() > > { > > auto s = S!int.create; > > assert(s.counter[0] == 1); // pass > > auto a = constOf(s); > > assert(s.counter[0] == 2); // fails > > } > > Up untile recently, casts were considered rvalues in dmd while postblits get > called solely on lvalues. However this was recently changed in the specs [1] > so that `cast()` and `cast(qualifier)` are lvalues. A PR was submitted to > get the > implementation in sync with the spec [2], so I suspect that that will also > fix this issue. > > [1] https://github.com/dlang/dlang.org/pull/2606 > [2] https://github.com/dlang/dmd/pull/9505 I didn't notice that the cast is done to a different template instantiation: from S!int to S!(const int); does are different types that are not implicitly convertible one to the other so the cast in this case will remain an rvalue. My PR will not fix this and this is an invalid issue according to the spec. Now that copy constructors are in the master branch, I suggest that you stop using the postblit.
Comment #6 by andrei — 2019-04-30T15:21:12Z
@Ilya okay to close this as won't fix since it's legacy? New code would use copy ctors, which is hopefully much nicer.
Comment #7 by ilyayaroshenko — 2019-04-30T15:40:04Z
Looks good