Thread overview
[Issue 19793] no postblit is called if cast is used for structs
Apr 08, 2019
John Colvin
Apr 09, 2019
RazvanN
Apr 22, 2019
Dlang Bot
Apr 24, 2019
RazvanN
April 08, 2019
https://issues.dlang.org/show_bug.cgi?id=19793

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code

--
April 08, 2019
https://issues.dlang.org/show_bug.cgi?id=19793

--- Comment #1 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
Maybe related to https://issues.dlang.org/show_bug.cgi?id=19774

--
April 08, 2019
https://issues.dlang.org/show_bug.cgi?id=19793

John Colvin <john.loughran.colvin@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |john.loughran.colvin@gmail.
                   |                            |com

--- Comment #2 from John Colvin <john.loughran.colvin@gmail.com> ---
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)

--
April 09, 2019
https://issues.dlang.org/show_bug.cgi?id=19793

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #3 from RazvanN <razvan.nitu1305@gmail.com> ---
(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

--
April 22, 2019
https://issues.dlang.org/show_bug.cgi?id=19793

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@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

--
April 24, 2019
https://issues.dlang.org/show_bug.cgi?id=19793

--- Comment #5 from RazvanN <razvan.nitu1305@gmail.com> ---
(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.

--
April 30, 2019
https://issues.dlang.org/show_bug.cgi?id=19793

Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@erdani.com

--- Comment #6 from Andrei Alexandrescu <andrei@erdani.com> ---
@Ilya okay to close this as won't fix since it's legacy? New code would use copy ctors, which is hopefully much nicer.

--
April 30, 2019
https://issues.dlang.org/show_bug.cgi?id=19793

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |WONTFIX

--- Comment #7 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
Looks good

--