[Issue 2270] New: cast produces invalid arrays at runtime
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Aug 6 14:14:53 PDT 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2270
Summary: cast produces invalid arrays at runtime
Product: D
Version: 1.033
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: benoit at tionex.de
interface I{}
class C : I {}
C[] carray = getInstance();
I[] iarray = carray; // compile error
I[] iarray = cast(I[])carray; // runtime error (1)
// correct way:
I[] iarray = new I[carray.length];
foreach( idx, c; carray ){
iarray[idx] = c; // implicit cast
}
I use a template for doing this, but this looks so ugly.
I[] iarray = arraycast!(I)(carray);
I think the D compiler should call a runtime method in (1) to do the cast in a
loop, instead of doing a simple type change that is not working correctly.
Alternatively the compiler can forbid the use of cast in cases where the result
will not more work.
The user has always the option to go over a void* cast, which is than clearly
marked as dangerous code line.
Discussion in NG:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=74614
--
More information about the Digitalmars-d-bugs
mailing list