www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12495] New: CTFE slice cast can cause allocation

https://d.puremagic.com/issues/show_bug.cgi?id=12495

           Summary: CTFE slice cast can cause allocation
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: monarchdodra gmail.com



Casting a slice from "string" to "immutable(ubyte)[]" can cause allocation in
CTFE (apparently).

To trigger this, we must start from an "empty slice of immutable pointing to
mutable data".

/----
import std.string;

void main()
{
    string getStr()
    {    
        char[1] dummyBuf = void; //dummy starting point.
        string slice = cast(string)dummyBuf[0 .. 0]; //empty slice, .ptr points
mutable.
        slice ~= 'a'; //This should allocate. May or may not point immutable
memory?
        return slice.idup; //This should allocate again, and definitly point
immutable memory;
    }    
    enum k = indexOf(getStr());
}

auto indexOf(string s)
{
   auto p1 = s.ptr;
   //auto p2 = (cast(immutable(ubyte)[])s).ptr;
   auto p2 = s.representation.ptr; //raw cast also reproduces
   //assert(cast(void*)p1 == cast(void*)p2); //Fails
   return cast(void*)p2 - cast(void*)p1; //cannot subtract pointers to two
different memory blocks
}
/----

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 30 2014