Thread overview
[Issue 19348] Struct casts should be better documented.
Aug 11, 2022
Nick Treleaven
Dec 17, 2022
Iain Buclaw
Dec 04
Dlang Bot
Dec 04
Dlang Bot
August 11, 2022
https://issues.dlang.org/show_bug.cgi?id=19348

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |johnnymarler@gmail.com

--- Comment #1 from Nick Treleaven <nick@geany.org> ---
*** Issue 20570 has been marked as a duplicate of this issue. ***

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=19348

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--
November 25
https://issues.dlang.org/show_bug.cgi?id=19348

Nick Treleaven <nick@geany.org> changed:

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

--- Comment #2 from Nick Treleaven <nick@geany.org> ---
Issue 20570 suggests the non-equivalency may be a bug.

> 8. Casting a value v to a struct S, when v is not a struct of the same type,
creates a rvalue of type S, with representation copied from v, with representation copied from v; no postblits/copy constructors are called; v.sizeof must be equal to S.sizeof.

No constructor at all seems to be called, unlike for S(v). That wording would be better, but doesn't explain this error:

struct S {
    int[2] i;
}

void main()
{
    S s;
    s = cast(S) cast(short[4]) [10,0,20,0]; // OK
    s = cast(S) long.max; // error
}

Error: cannot cast expression `9223372036854775807L` of type `long` to `S`

The sizes do match.

--
December 04
https://issues.dlang.org/show_bug.cgi?id=19348

--- Comment #3 from Nick Treleaven <nick@geany.org> ---
> No constructor at all seems to be called

Sorry, that was wrong. Casting tries S(v) first, then for casting from struct or static array, it falls back to reinterpret casts. I'm making a PR to document this.

--
December 04
https://issues.dlang.org/show_bug.cgi?id=19348

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

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

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@ntrel created dlang/dlang.org pull request #3931 "Fix Bugzilla 19348 - Struct casts should be better documented" fixing this issue:

- Fix Bugzilla 19348 - Struct casts should be better documented

  Document fallback casting from struct/static array to struct.
  Add example.
  Also add link to struct constructors for `T(args)` *PostfixExpression*.

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

--
December 04
https://issues.dlang.org/show_bug.cgi?id=19348

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

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

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dlang.org pull request #3931 "Fix Bugzilla 19348 - Struct casts should be better documented" was merged into master:

- a81bfae802dc4df1324404d93dad3e02b4e1d08b by Nick Treleaven:
  Fix Bugzilla 19348 - Struct casts should be better documented

  Document fallback casting from struct/static array to struct.
  Add example.
  Also add link to struct constructors for `T(args)` *PostfixExpression*.

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

--