std.string.assumeUTF() silently casting mutable to immutable?

Johan j at j.nl
Tue Feb 13 14:05:03 UTC 2024


On Tuesday, 13 February 2024 at 08:10:20 UTC, Jonathan M Davis 
wrote:
> 
> So, there's definitely a bug here, but it's a dmd bug. Its 
> checks for whether it can safely change the constness of the 
> return type apparently aren't sophisticated enough to catch 
> this case.

This is a pretty severe bug.
Some test cases: https://d.godbolt.org/z/K1fjdj76M
```d
ubyte[] pure_ubyte(ubyte[] arr) pure @safe;
ubyte[] pure_void(void[] arr) pure @safe;
ubyte[] pure_int(int[] arr) pure @safe;
int[] pure_ubyte_to_int(ubyte[] arr) pure @safe;

// All cases below should not compile, yet some do.

immutable(ubyte)[] test(ubyte[] arr) @safe
{
     // return with_ubyte(arr); // ERROR: OK
     return pure_void(arr); // No error: NOK!
}

immutable(ubyte)[] test(int[] arr) @safe
{
     return pure_int(arr); // No error: NOK!
}

immutable(int)[] test2(ubyte[] arr) @safe
{
     return pure_ubyte_to_int(arr); // No error: NOK!
}
```

-Johan




More information about the Digitalmars-d-learn mailing list