| |
![Bolpat's Gravatar profile Gravatar of Bolpat](//www.gravatar.com/avatar/8b6a7fd1336f54f3da8bf99bafac2218?d=identicon&s=32) | Posted by Bolpat | Permalink Reply |
|
Bolpat ![Bolpat's Gravatar profile Gravatar of Bolpat](//www.gravatar.com/avatar/8b6a7fd1336f54f3da8bf99bafac2218?d=identicon&s=80)
| 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.
--
|