Originally reported in the forum: https://forum.dlang.org/thread/gkoqjxixfiwftivjkxqp@forum.dlang.org I'm now reporting it here, since more experienced D users seem to agree it's a bug. I was surprised to find that this code compiles with no complaints: @safe: string test(ubyte[] arr) { import std.string; return arr.assumeUTF; // why does this compile? } void main() { import std.stdio; ubyte[] buf = ['g', 'o', 'o', 'd']; string dodgy = buf.test; // this string points to mutable data buf[] = ['b', 'a', 'd', '!']; writeln(typeof(dodgy).stringof); writeln(dodgy); }
This is fixed in master but the fix is behind a preview switch (-preview=fixImmutableConv).
The above example does indeed trigger an error with -preview=fixImmutableConv on run.dlang.org. However, changing the test function's argument to const(ubyte)[] slips right past with no complaint. Shouldn't that be rejected as well? string test(const(ubyte)[] arr) { import std.string; return arr.assumeUTF; // why does this compile? }