Safe Regions with Deterministic Destruction

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Tue Oct 27 16:56:40 UTC 2020


On Tuesday, 27 October 2020 at 15:33:08 UTC, ag0aep6g wrote:
> On 27.10.20 08:07, Ola Fosheim Grøstad wrote:
>> But technically the t.root pointer address is a value, so 
>> apparently scope does not apply only to t, but also to the 
>> member root?
>
> I'm not sure what you mean by "is a value". `t.root` is a class 
> reference, which (to `scope`) is a pointer with a fancy name. 
> It being an indirection is what makes it interesting. `scope` 
> doesn't do anything for non-indirections.

Yes, what I meant is that it is a value on the stack. But there 
is a disconnect with classes. "oddball" passes as a class, even 
though it would not pass as a struct:

class A { int x; }
class B { A a; }

A oddball() {
     scope B b = new B;
     b.a = new A;
     return b.a;
}


But WHY does this pass?

class A { int x; }
struct B { A a; }

A misbehave() @safe {
     scope A tmp = new A;
     scope B b;
     b.a = tmp;
     return b.a;
}

> But yes, `scope` applies to `t.root`, because it's the first 
> level of indirection in `t`. If it was `t.x.y.z.root`, with 
> `x`, `y`, and `z` being more structs, `scope` would still apply 
> to `root`. But if `x`, `y`, or `z` were some kind of 
> indirection, `scope` would apply to that and not to `root`.

I understand. So for structs it works the same way as 
scope-parameters?

> then it stops. If you have two or more levels of indirections, 
> `scope` still only affects the first.

So you basically should just apply scope to pointers and smart 
pointers.

>> So if Node was a linked list, then t.root is limited by escape 
>> analysis, but not t.root.next?
>
> Exactly.

I wonder how a transitive scope would play out?



More information about the Digitalmars-d mailing list