Thread overview
[Issue 16455] Wrong code when calling a struct delegate
Aug 31, 2016
ag0aep6g@gmail.com
Aug 31, 2016
apham
Sep 01, 2016
apham
Sep 01, 2016
ag0aep6g@gmail.com
August 31, 2016
https://issues.dlang.org/show_bug.cgi?id=16455

ag0aep6g@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |ag0aep6g@gmail.com
         Resolution|---                         |INVALID

--- Comment #1 from ag0aep6g@gmail.com ---
(In reply to apham from comment #0)
> struct NodeRange(S)
> {
[...]
>     void delegate() doPopFront;
> 
>     void doMove()
>     {
[...]
>     }
[...]
>     this(Node!S aParent)
>     {
[...]
>         doPopFront = &doMove;
>     }
[...]
> }

As far as I see, that code is invalid. &doMove refers to the current location of the struct. But structs can be copied and moved around (the compiler is even allowed to do it on its own). Whenever that happens, doPopFront is invalidated.

Closing as invalid. Please reopen if you disagree.

--
August 31, 2016
https://issues.dlang.org/show_bug.cgi?id=16455

apham <apz28@hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |---

--- Comment #2 from apham <apz28@hotmail.com> ---
Based on this https://dlang.org/spec/function.html#closures and the code, the struct var is not moving anywhere and not out of scope, so it must work

--
September 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16455

apham <apz28@hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from apham <apz28@hotmail.com> ---
need to move the setting "doPopFront = &doMove;" out of constructor to make it work. Can move it to empty() to complete the initialization

--
September 01, 2016
https://issues.dlang.org/show_bug.cgi?id=16455

--- Comment #4 from ag0aep6g@gmail.com ---
(In reply to apham from comment #2)
> Based on this https://dlang.org/spec/function.html#closures and the code, the struct var is not moving anywhere and not out of scope, so it must work

The NodeRange struct may be moved during construction (constructed at one location, then moved to the target location). If this happens, it invalidates the internal pointer you set up in the constructor. Returning from `children` may mean another copy/move.

There are no hard rules here. The compiler has some leeway. From <https://dlang.org/spec/struct.html> (second paragraph):

> A struct is defined to not have an identity; that is, the implementation is free to make bit copies of the struct as convenient.

(In reply to apham from comment #3)
> need to move the setting "doPopFront = &doMove;" out of constructor to make it work. Can move it to empty() to complete the initialization

Note that any copy/move of the struct will still invalidate the pointer. And as far as I understand, the compiler may assume that the struct does not point to itself. So you may run into trouble, even when you have no explicit copy/move in your code.

--