[Issue 11176] array.ptr in @safe code

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Feb 24 07:35:55 PST 2016


https://issues.dlang.org/show_bug.cgi?id=11176

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com

--- Comment #10 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to Kenji Hara from comment #9)
> One another way I can think is, array.ptr property would add a hidden check
> `arr.length != 0` under @safe code, then returns `null` instead when the
> length is 0.

As someone who lives in the camp of "empty arrays and null arrays should be
treated the same", I think this behavior wouldn't affect me.

However, many significant people depend on this NOT being the case. Think of
the pushback for the if(!arr) fix.

To make the behavior different if you add a @safe tag may not affect them, but
since the compiler can *infer* safety, this will be bad for anyone. Imagine you
have template code not marked @safe, and you find a legitimate use for arr[$ ..
$].ptr. The compiler may infer @safe, and then your code fails at runtime even
though it would pass if not inferred @safe.

In order to avoid such an issue, I think you have to just disallow ptr access
in @safe code. That's the only thing that's checkable at compile-time, and will
prevent a @safe inference.

--


More information about the Digitalmars-d-bugs mailing list