can we un-deprecate .ptr on arrays in @safe code? cf issue 18529

Simen Kjærås simen.kjaras at gmail.com
Tue Feb 27 11:33:04 UTC 2018


On Tuesday, 27 February 2018 at 09:58:00 UTC, bauss wrote:
> On Tuesday, 27 February 2018 at 09:47:51 UTC, Stefan Koch wrote:
>> On Tuesday, 27 February 2018 at 09:23:19 UTC, bauss wrote:
>>> On Tuesday, 27 February 2018 at 08:43:32 UTC, Timothee Cour 
>>> wrote:
>>>> see rationale in 
>>>> https://issues.dlang.org/show_bug.cgi?id=18529
>>>
>>> All I see is a rationale for how it can't be replaced, but 
>>> not a rationale for actually doing it.
>>>
>>> I can't imagine a "safe" situation where you want to do it.
>>>
>>> Tbh. just put your code in @trusted
>>
>> Checking if an array is the slice of another.
>
> Like?
>
> assert(a.ptr != b.ptr);
>
> Which already works in @safe.

That only checks if the first element is the same. For a full 'is 
slice' check you'd need something like this:

     assert(a.ptr <= b.ptr && b.ptr + b.length <= a.ptr + 
a.length);

// Or:

     auto c = b.ptr - a.ptr;
     assert(c >= 0 && c + b.length <= a.length);

And trust me, the compiler complains about both of these. 
Possibly rightfully in the first example, but the latter never 
does anything scary with the given pointers.

--
   Simen


More information about the Digitalmars-d mailing list