[Issue 12495] New: CTFE slice cast can cause allocation

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 30 12:18:49 PDT 2014


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 at puremagic.com
        ReportedBy: monarchdodra at gmail.com


--- Comment #0 from monarchdodra at gmail.com 2014-03-30 12:18:45 PDT ---
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: -------


More information about the Digitalmars-d-bugs mailing list