scope variable `b` assigned to `a` with longer lifetime (-dip1000)

vit vit at vit.vit
Sat Apr 9 10:39:33 UTC 2022


Hello,
Why doesn't this code compile?


```d

static struct Foo{
     void *ptr;

     void proxySwap1(scope ref typeof(this) rhs)scope pure nothrow 
@trusted @nogc{
         auto tmp = this.ptr;
         this.ptr = rhs.ptr;
         rhs.ptr = tmp;
     }
     void proxySwap2()(scope ref typeof(this) rhs)scope pure 
nothrow @trusted @nogc{
         this.proxySwap1(rhs);
     }
     void proxySwap3()(scope ref typeof(this) rhs)scope pure 
nothrow @trusted @nogc{
         auto tmp = this.ptr;
         this.ptr = rhs.ptr;
         rhs.ptr = tmp;
     }
}

void main()@safe{

     scope Foo a;
     scope Foo b;

     a.proxySwap1(b);	//OK
     a.proxySwap2(b);	//OK
     a.proxySwap3(b);	//Error: scope variable `b` assigned to `a` 
with longer lifetime
}
```



More information about the Digitalmars-d-learn mailing list