On Thursday, 16 March 2017 at 17:20:45 UTC, H. S. Teoh wrote:
I'm not convinced casting static array to immutable is OK. Check this out:

        import std.stdio;

        char[32] func() {
            char[32] staticArr = "A123456789abcdefB123456789abcdef";
            return staticArr; // OK, by-value return
        }

        string gunk() {
string x = func(); // implicit conversion char[32] -> string
            writeln(x.ptr);
            writeln(x);         // prints "A123456789abcdefB123456789abcdef"
            return x;
        }

        void main() {
            auto s = gunk();
            writeln(s.ptr);     // prints same address as in gunk()
            writeln(s);         // prints corrupted string
        }

Run this code and you'll see that s.ptr has the same address as x.ptr, and that x.ptr is the address of a local variable. This is blatantly wrong.

Filed a new issue for this:

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

Exactly, if there was a variable of type char[32] on the right hand side of
    string x = func();
instead of the call of func, then the compiler would complain. So this is a bug.



Reply via email to