Discussion Thread: DIP 1035-- at system Variables--Community Review Round 1

Stanislav Blinov stanislav.blinov at gmail.com
Wed Jun 10 12:59:16 UTC 2020


On Wednesday, 10 June 2020 at 10:00:40 UTC, WebFreak001 wrote:

> struct Wrapper(T) {
>     @system T t;
>     this(T t) @trusted {
>         this.t = t; // Oops! Calls a `@system` copy constructor
>     }
> }
>
> I don't see a solution proposed to this pitfall in the DIP but 
> wouldn't this be solvable from a library function? Something 
> like
>
> ref T trustedAssign(T)(const return ref T value) @trusted { 
> return *cast(T*)&value; }
>
> struct Wrapper(T) {
>     @system T t;
>     this(T t) @safe {
>         this.t.trustedAssign = t; // "cast away @system"
>     }
> }

That one is just an incorrect example in the DIP. It 
uncoditionally trusts a (possibly @system) destructor of T. And 
yes, absolutely, it should not use the copy constructor at all. 
It should use a core.lifetime : moveEmplace, a call to which it 
*can* put in a @trusted delegate if, and only if, T itself is 
@safe (because moveEmplace would write into the argument).
And later down the line, if/when Walters move ctors come into 
play, `this.t = t`; would just [hopefully *have to*, if Walter 
listens] move, thus removing destructor call, and leaving in 
inference from the move ctor.


More information about the Digitalmars-d mailing list