The following code compiles struct Foo { double d; } struct Bar { byte b; } void main() { Foo foo; Bar bar = foo; } even though Foo is not implicitly convertible to Bar. In contrast, assigning Foo foo; Bar bar; bar = foo; fails as expected.
*** Issue 3076 has been marked as a duplicate of this issue. ***
Jarett found this pretty ICE in ztc\cod1.c:1673: This can lead to the ICE mentioned in the description, if the source type is smaller than the destination, at least for some values of "smaller," *and* the source is a function call. The following, for instance, causes it: struct Small { uint x; } struct Large { uint x, y, z; } Small foo() { return Small(); } void main() { Large l = foo(); } // bang!
Ah poop. I was searching for ICEs and never even came across this one :)
possibly related to bug 3036.
*** Issue 3216 has been marked as a duplicate of this issue. ***
There are two completely different bugs in this report. One is the implicit conversion one, which is a bad code generation bug. The ICE is actually a quite different bug. Here's a test case which doesn't involve the initialisation bug. struct Small { uint x; } struct Large { uint x, y, z; } Small foo() { return Small(); } void main() { Large l; Small s; l = cast(Large)foo(); }
*** Issue 3036 has been marked as a duplicate of this issue. ***
There's a patch for the original bug in bug 2702. It's unrelated to the ICE. (It's really annoying when new bugs are reported in the comments for existing bugs, it's not clear what to do with them).
*** Issue 3287 has been marked as a duplicate of this issue. ***
The root cause is that all kinds of nonsense is allowed in struct casts. Explicit struct casts only make sense when the source and destination are of the same size. Patch against DMD 2.032. Index: cast.c =================================================================== --- cast.c (revision 196) +++ cast.c (working copy) @@ -822,6 +822,15 @@ return e; } } + // Struct casts are possible only when the sizes match + if (typeb->ty==Tstruct || tb->ty==Tstruct) { + size_t fromsize = tb->size(loc); + size_t tosize = typeb->size(loc); + if (fromsize !=tosize) { + error("Cannot cast from %s to %s", type->toChars(), t->toChars()); + return this; + } + } e = new CastExp(loc, e, tb); } }
Fixed dmd 1.048 and 2.033