www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2270] New: cast produces invalid arrays at runtime

reply d-bugmail puremagic.com writes:
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 digitalmars.com
        ReportedBy: benoit 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


-- 
Aug 06 2008
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2270


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com
           Keywords|                            |wrong-code




------- Comment #1 from smjg iname.com  2009-01-11 16:17 -------
Oh dear, a case of issue 85 that's still pending.


-- 
Jan 11 2009