[Dlang-internal] DIP1000 discussion and testing

Mathias Lang via Dlang-internal dlang-internal at puremagic.com
Sun Dec 18 03:04:25 PST 2016


On Sunday, 18 December 2016 at 05:56:42 UTC, Walter Bright wrote:
> On 12/17/2016 7:56 PM, Martin Nowak wrote:
>>> On 12/17/2016 7:38 AM, Mathias Lang wrote:
>>>> 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.
>>>
>>> It should work fine.
>>
>> What does "it" refer to here, doesn't hurt much to write 
>> "Internal pointers in
>> scope structs" or so.
>> Equally "work fine" doesn't state how the "it" is supposed to 
>> work.
>
> A stack allocator is conceptually returning a slice of a static 
> array allocated on the stack. This works with scope.
>
> Understanding scope always goes back to understanding how it 
> works with pointers and addresses. Adding layers of abstraction 
> over that can be confusing by obfuscating that relationship, 
> but rewrite the abstract as pointers and addresses, and the 
> behavior then becomes clear.
>
> The bugs Mathias has posted have (so far) all been the result 
> of an abstraction not conforming to how pointers and addresses 
> work, due to a bug in dmd.
>

Thanks for expanding on that, as Martin pointed out, it helps 
reviewers a lot.
I don't feel the point about internal pointers to stack-allocated 
data have been addressed, but maybe it'll be better if I just 
craft a test case.

> https://github.com/dlang/dmd/pull/6331

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. It might be your intention, but it needs 
to be clearly expressed in the DIP.
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.


More information about the Dlang-internal mailing list