Bug 2469 – ICE(cod1.c) arbitrary struct accepted as struct initializer
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2008-11-23T03:56:00Z
Last change time
2014-03-01T00:36:24Z
Keywords
ice-on-invalid-code, patch
Assigned to
nobody
Creator
kamm-removethis
Comments
Comment #0 by kamm-removethis — 2008-11-23T03:56:09Z
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.
Comment #1 by kamm-removethis — 2009-06-17T23:19:21Z
*** Issue 3076 has been marked as a duplicate of this issue. ***
Comment #2 by kamm-removethis — 2009-06-17T23:20:41Z
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!
Comment #3 by jarrett.billingsley — 2009-06-18T06:45:55Z
Ah poop. I was searching for ICEs and never even came across this one :)
Comment #4 by dfj1esp02 — 2009-06-23T05:16:12Z
possibly related to bug 3036.
Comment #5 by jarrett.billingsley — 2009-07-30T08:42:40Z
*** Issue 3216 has been marked as a duplicate of this issue. ***
Comment #6 by clugdbug — 2009-08-07T01:28:58Z
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();
}
Comment #7 by clugdbug — 2009-08-07T01:29:07Z
*** Issue 3036 has been marked as a duplicate of this issue. ***
Comment #8 by clugdbug — 2009-08-08T22:21:44Z
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).
Comment #9 by jarrett.billingsley — 2009-09-03T09:48:19Z
*** Issue 3287 has been marked as a duplicate of this issue. ***
Comment #10 by clugdbug — 2009-09-22T01:07:54Z
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);
}
}