Thread overview
[Issue 24619] Allow pointer slicing for slices of length 0 or 1
[Issue 24619] [DIP1000] Allow pointer slicing for slices of length 0 or 1
Jun 20
Bolpat
Jun 20
Dennis
Jun 20
Bolpat
June 20
https://issues.dlang.org/show_bug.cgi?id=24619

Bolpat <qs.il.paperinik@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe
                 CC|                            |qs.il.paperinik@gmail.com

--
June 20
https://issues.dlang.org/show_bug.cgi?id=24619

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl
            Summary|[DIP1000] Allow pointer     |Allow pointer slicing for
                   |slicing for slices of       |slices of length 0 or 1
                   |length 0 or 1               |

--- Comment #1 from Dennis <dkorpel@live.nl> ---
Similar situation to this:

https://github.com/dlang/dmd/pull/15581#issuecomment-1738649867

Do you have a use case?

--
June 20
https://issues.dlang.org/show_bug.cgi?id=24619

--- Comment #2 from Bolpat <qs.il.paperinik@gmail.com> ---
(In reply to Dennis from comment #1)
> Similar situation to this:
> 
> https://github.com/dlang/dmd/pull/15581#issuecomment-1738649867

It has some vague similarity, but AFAICT, PR 15581 is almost the reverse, going from slice to pointer to the first element. The `@safe` way to get a pointer to the first element is `&xs[0]`. If `xs` has compile-time-known length, surely the compiler does not do a run-time check for index bounds.

On the other hand, going from pointer to 1-element slice, best one can do is `(() @trusted => ptr[0 .. 1])()`, and that is unnecessary. I don’t know about the semantics about a slice with a `null` pointer component and a length of 1, but my bet would be that it should be exactly as any other `null` dereference. Any index except `0` would throw an `ArrayIndexError` and the index `0` is a `null` dereference.

> Do you have a use case?

The use case is passing a single value to a function that expects a slice of such values. While one can create a length-1 array with a copy of the value, not all values are copyable and not all values are cheap to copy.

I can’t imagine a use case for `[0 .. 0]` though. I just included that one because it’s obviously safe and if we make an exception for `[0 .. 1]`, it would be weird to disallow it.

--