[Dlang-internal] DIP1000 discussion and testing

Walter Bright via Dlang-internal dlang-internal at puremagic.com
Sun Dec 18 07:24:34 PST 2016


On 12/18/2016 3:04 AM, Mathias Lang wrote:
> The problem with this solution is that it forces `scope` on unrelated method.
> E.g.
>
> ```
> int* escape () @safe
> {
>     int i;
>     Foo f;
>     f.v = &i;
>     return f.foo;
> }
>
> struct Foo
> {
>     int* v;
>     int* foo () @safe { return null; }
> }
> ```
>
> Doesn't compile anymore.

And it shouldn't. f contains a pointer to a local, and so f must be scope. A 
scoped object can only be used on a scoped method, because otherwise the method 
can squirrel away an escaping copy.


> It might be your intention, but it needs to be clearly
> expressed in the DIP.

The rules need to be clearly expressed, but I don't know how to express every 
consequence of the rules.


> Moreover, this code is obviously correct to the reader:
>
> ```
> struct Foo
> {
>     int* v;
>     int foo () @safe { return *v; }
> }
> ```
>
> But won't compile. I tried with static arrays just to be sure, same outcome.

It compiles when I try it.



More information about the Dlang-internal mailing list