[Dlang-internal] DIP1000 discussion and testing

Mathias Lang via Dlang-internal dlang-internal at puremagic.com
Sat Dec 17 07:38:51 PST 2016


On Saturday, 17 December 2016 at 14:09:59 UTC, Walter Bright 
wrote:
> On 12/16/2016 9:08 AM, Mathias Lang wrote:
>> void main () @safe
>> {
>>     int* a = escape();
>> }
>>
>> int* escape () @safe
>> {
>>     int i;
>>     int*[3] a = [ &i, null, null ];
>>     return bar(&a[0]);
>> }
>>
>> int* bar (scope int** x) @safe
>> {
>>     return foo(*x);
>> }
>>
>> int* foo (int* x) @safe { return x; }
>
> https://github.com/dlang/dmd/pull/6329

Merged both of your P.R. in a local branch, and tested again:

```
void main () @safe
{
     int[] a = escape();
}

int[] escape () @safe
{
     Foo f;
     return f.foo;
}

struct Foo
{
     int[10] v;
     int[] foo () return @safe { return this.v; }
}
```

> Taking the address of a scope pointer is invalid

Thanks for the clarification. I think this will prove pretty 
limiting in the future. For example, a stack allocator will 
return data that is already typed as `scope`, and as a 
consequence, interior pointers will not be expressible. I'll see 
if I can form an example in terms of code.


And since I started toying around with aggregate methods, here's 
another reason why changing the STC of a field member is a very 
bad idea:
```
void main () @safe
{
     int* a = escape();
}

int* escape () @safe
{
     int i;
     Foo f;
     f.v = &i;
     return f.foo;
}

struct Foo
{
     int* v;
     int* foo () @safe { return this.v; }
}
```

Note: The definition of `Foo` might be in another module, and 
just visible via an header (.di) file.


More information about the Dlang-internal mailing list