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
}
/----