www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17654] New: return value incorrectly considered unique when

https://issues.dlang.org/show_bug.cgi?id=17654

          Issue ID: 17654
           Summary: return value incorrectly considered unique when
                    casting to another pointer type
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ag0aep6g gmail.com

Found by Namal in D.learn:
http://forum.dlang.org/post/eqoejftfcptbiclybwjg forum.dlang.org

Original code:
----
void main()
{
    import std.algorithm;
    import std.string;
    char[] line;
    auto bytes = line.representation.dup;
    bytes.sort;
    string result = bytes.assumeUTF; /* should be rejected */
}
----

Reduced to show it's a compiler bug:
----
char[] assumeUTF(ubyte[] str) pure { return cast(char[]) str; }

void main()
{
    ubyte[] b = ['a', 'b', 'c'];
    string s = assumeUTF(b); /* should be rejected */
    assert(s == "abc"); /* passes */
    b[0] = '!';
    assert(s == "abc"); /* fails */
}
----

Another variant to show it's not about arrays or the char type:
----
ubyte* toBytePointer(uint* p) pure { return cast(ubyte*) p; }

void main()
{
    uint* i = new uint;
    immutable ubyte* b = toBytePointer(i); /* should be rejected */
    *i = 0xFF_FF_FF_FF;
    assert(*b != 0xFF); /* fails */
}
----

--
Jul 14 2017