Bug 19348 – Struct casts should be better documented.

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-11-02T12:27:34Z
Last change time
2024-12-04T13:48:08Z
Keywords
pull
Assigned to
No Owner
Creator
Stanislav Blinov

Comments

Comment #0 by stanislav.blinov — 2018-11-02T12:27:34Z
struct S { int i; } struct X { byte[4] b; } union U { S s; } void main() { U u; auto s1 = cast(S)u; auto s2 = cast(X)s1; //auto s3 = S(u); // cannot implicitly convert... //auto s4 = X(s1); // cannot implicitly convert... } Documentation on such casts is lacking. https://dlang.org/spec/expression.html#cast_expressions says: 8. Casting a value v to a struct S, when value is not a struct of the same type, is equivalent to: S(v) ...which is obviously not the case, as evidenced by uncommenting the `s3/s4` lines in the snipped above. Perhaps it should state something like: 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; no postblits/copy constructors are called; v.sizeof must be equal to S.sizeof. 9. Casting a value v to a struct S, when v is also of type S, is equivalent to making a copy of v. Such a cast will generate an error if appropriate postblit/copy constructor is @disabled for S.
Comment #1 by nick — 2022-08-11T17:07:03Z
*** Issue 20570 has been marked as a duplicate of this issue. ***
Comment #2 by nick — 2024-11-25T21:38:52Z
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.
Comment #3 by nick — 2024-12-04T13:16:45Z
> 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.
Comment #4 by dlang-bot — 2024-12-04T13:34:05Z
@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
Comment #5 by dlang-bot — 2024-12-04T13:48:08Z
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