www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20570] New: Struct Cast Not Documented or Incorrectly Accepted


          Issue ID: 20570
           Summary: Struct Cast Not Documented or Incorrectly Accepted
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: johnnymarler gmail.com

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?

Feb 08 2020