ref auto getRange() return scope move struct ?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Aug 16 13:51:49 UTC 2019


On Friday, August 16, 2019 6:29:19 AM MDT Newbie2019 via Digitalmars-d-learn 
wrote:
> On Friday, 16 August 2019 at 12:23:01 UTC, Newbie2019 wrote:
> > I has this simple function has some memory bugs:
> >
> > -------
> > struct TreeRange {
> >
> >     @disable this() ;
> >     @disable this(this) ;
> >
> > }
> > struct Tree {
> >
> >     ref auto getRange() return scope {
> >
> >         return TreeRange!T(_root);
> >
> >     }
> >
> > }
> > Tree tree;
> > auto range = tree.getRange();
> > ------
> >
> > when I trace the issue, I find the address for TreeRange is
> > moved.  it TreeRange.__ctor the address is 0x7ffeefbfd168,
> > but "auto range = tree.getRange();" address is 0x7ffeefbfd420
> >
> >
> > How to prevent this struct move in this case ?
>
> With dip1014 I can fix the internal pointer(but I guess It will
> need maybe 1 years late to implement).  Right now I just want
> forbit the move action,  is it doable ?

It is not possible to prevent moving in D as things currently stand. DIP
1014 will need to be implemented to either hook into moves or to prevent
them. However, once DIP 1014 has been implemented, I would expect the result
to be that what you're trying to do here simply wouldn't work if you
disallowed moving, since DIP 1014 doesn't affect when the compiler does a
move. It just allows you to hook into when a move takes place so that you
can do stuff like adjust pointers, and presumably, if you @disable the
function that hooks into the move, moving will then be disabled (though
IIRC, that's not explicitly called out in DIP 1014; it's just what would
naturally fall out from how @disable works).

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list