Originally reported in the forum:
https://forum.dlang.org/thread/[email protected]
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);
}
Comment #1 by razvan.nitu1305 — 2024-02-14T10:58:25Z
This is fixed in master but the fix is behind a preview switch (-preview=fixImmutableConv).
Comment #2 by forestix — 2024-02-14T11:49:53Z
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?
}
Comment #3 by robert.schadek — 2024-12-13T19:33:19Z