On Saturday, 11 January 2014 at 18:29:36 UTC, Manu wrote:
I just managed to assign a const(char)[] to a string... caused crashes when
the original memory disappeared.

inout(char)[] todstr(inout(char)* cstr) pure nothrow
{
return cstr ? cstr[0 .. std.c.string.strlen(cstr)] : cstr[0 .. 0];
}

struct Data
{
char buffer[256] = void;
@property const(char)[] filename() const pure nothrow { return
todstr(buffer.ptr); }
}

struct MyThing
{
private this(in Data* p)
{
filename = p.filename; // *** Uh oh! assigned a const(char)[] @property to
a string! ***
}

string filename;
}

Surely that assignment shouldn't be legal? Shouldn't I need to idup?

Simplified:

const(char)[] slice(ref const char[1] c) pure nothrow
{
        return c[];
}

void main()
{
        char[1] m = ".";

        immutable i = slice(m); // should not compile
        assert(i == ".");

        m = "!"; // uh-oh
        assert(i == "."); // fails
}

Reply via email to