Thread overview
[Issue 16357] cast(T[])[x] casts x to T instead of [x] to T[]
Feb 17, 2019
Vladimir Panteleev
Jun 21, 2021
ag0aep6g
Oct 06, 2023
Dlang Bot
Oct 06, 2023
Nick Treleaven
Nov 08, 2023
Dlang Bot
February 17, 2019
https://issues.dlang.org/show_bug.cgi?id=16357

Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl

--- Comment #1 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
*** Issue 19683 has been marked as a duplicate of this issue. ***

--
June 21, 2021
https://issues.dlang.org/show_bug.cgi?id=16357

ag0aep6g <ag0aep6g@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |eyal@weka.io

--- Comment #2 from ag0aep6g <ag0aep6g@gmail.com> ---
*** Issue 22055 has been marked as a duplicate of this issue. ***

--
October 04, 2023
https://issues.dlang.org/show_bug.cgi?id=16357

Steven Schveighoffer <schveiguy@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |spec
                 CC|                            |schveiguy@gmail.com

--- Comment #3 from Steven Schveighoffer <schveiguy@gmail.com> ---
This is technically in the spec: https://dlang.org/spec/expression.html#cast_array_literal

Quoting:
```
When array literals are cast to another array type, each element of the array
is cast to the new element type.
```

But I don't know if the spec (which only shows casting arrays with elements of
value 1) or this issue (which has elements of value 0) explores the destructive
nature of the current implementation:

```d
void main()
{
    uint x = 0x12345;
    ubyte[] data = cast(ubyte[])[x];
    assert(data == [0x45]); // we lost the extra bytes!
}
```

My thought is that this should either be an error, or a warning in the spec is warranted.

--
October 06, 2023
https://issues.dlang.org/show_bug.cgi?id=16357

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@ntrel updated dlang/dlang.org pull request #3701 "[spec] Array literal can implicitly convert to an expected type" fixing this issue:

- Show overflow in example

  Fix Issue 16357 - cast(T[])[x] casts x to T instead of [x] to T[].

https://github.com/dlang/dlang.org/pull/3701

--
October 06, 2023
https://issues.dlang.org/show_bug.cgi?id=16357

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick@geany.org

--- Comment #5 from Nick Treleaven <nick@geany.org> ---
I've made a PR to update the spec. I don't think we can make that an error without risking existing code breakage.

--
November 08, 2023
https://issues.dlang.org/show_bug.cgi?id=16357

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dlang.org pull request #3701 "[spec] Array literal can implicitly convert to an expected type" was merged into master:

- 4e8db30f0bf3c330c3431e83fe8a75f843b40857 by Nick Treleaven:
  Show overflow in example

  Fix Issue 16357 - cast(T[])[x] casts x to T instead of [x] to T[].

https://github.com/dlang/dlang.org/pull/3701

--
November 08, 2023
https://issues.dlang.org/show_bug.cgi?id=16357

Steven Schveighoffer <schveiguy@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|FIXED                       |WONTFIX

--- Comment #7 from Steven Schveighoffer <schveiguy@gmail.com> ---
I'm changing this to WONTFIX because the original bug report is against the behavior, not that it wasn't explained properly.

--