Bug 20570 – Struct Cast Not Documented or Incorrectly Accepted
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2020-02-09T02:04:31Z
Last change time
2022-08-11T17:07:03Z
Assigned to
No Owner
Creator
Jonathan Marler
Comments
Comment #0 by johnnymarler — 2020-02-09T02:04:31Z
The spec (https://dlang.org/spec/expression.html#CastExpression section 8) says that "Casting a value v to a struct S, when value is not a struct of the same type, is equivalent to: S(v)".
However, if I define 2 structs like this
struct Struct1 { }
struct Struct2 { }
I'm able to cast from one struct to the other:
auto c1 = cast(Struct2)Struct1();
auto c2 = cast(Struct1)Struct2();
According to the spec, these would be equivalent to:
auto c1 = Struct2(Struct1());
auto c2 = Struct1(Struct2());
However this "lowered" version of the code does not compile, but the cast version does, therefore they are not equivalent. Note that some forms of the "cast version" cause LDC to segfault, however, the LDC developers are not sure whether they should follow what DMD is doing, or follow the spec and make this a compile error. DMD seems to fully support these types of casts contrary to the spec.
So the question is, does the spec needed to be updated to document this cast behavior, or does this need to become a compile error?
Comment #1 by nick — 2022-08-11T17:07:03Z
*** This issue has been marked as a duplicate of issue 19348 ***