www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15762] New: Array casts involving const enums can be made safe


          Issue ID: 15762
           Summary: Array casts involving const enums can be made  safe
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: minor
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dhasenan gmail.com

According to dmd 2.070.0:

enum Windows1252Char : ubyte { init }

void main()  safe {
    ubyte[] a = [1, 2, 3, 4];
    auto aw = cast(Windows1252Char[]) a;          // valid
    auto caw = cast(const(Windows1252Char)[]) a;  // valid
    const(ubyte)[] c = [1, 2, 3, 4];
    auto d = cast(const(Windows1252Char)[]) c;    // invalid

arrcast.d(9): Error: cast from const(ubyte)[] to const(Windows1252Char)[] not
allowed in safe code

The compiler is fine if I cast mutable to mutable, or mutable to const, but I
can't cast const to const. Why not?

Switching from an enum type to a builtin type or struct makes things work, so
this looks like an oversight.

Context: I'm trying to make a  safe version of std.encoding, which does a ton
of casting between array types. I have to make much more code  trusted than I'd
like, and allowing const(ubyte) -> const(enum) casts would lead to far less
 trusted code.

Mar 04 2016