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

RazvanN razvan.nitu1305 at gmail.com
Wed Feb 14 12:29:46 UTC 2024


On Wednesday, 14 February 2024 at 11:56:29 UTC, Forest wrote:
> On Wednesday, 14 February 2024 at 10:57:42 UTC, RazvanN wrote:
>>
>> This has already been fixed, you just need to use 
>> -preview=fixImmutableConv. This was put behind a preview flag 
>> as it introduces a breaking change.
>
> I just tried that flag on run.dlang.org, and although it fixes 
> the case I posted earlier, it doesn't fix this one:
>
> ```d
> string test(const(ubyte)[] arr)
> {
>     import std.string;
>     return arr.assumeUTF;
> }
> ```
>
> Shouldn't this be rejected as well?

Indeed, that should be rejected as well, otherwise you can modify 
immutable table. This code currently happily compiles:

```d
string test(const(ubyte)[] arr)
{
     import std.string;
     return arr.assumeUTF;
}

void main()
{
     import std.stdio;
     ubyte[] arr = ['a', 'b', 'c'];
     auto t = test(arr);
     writeln(t);
     arr[0] = 'x';
     writeln(t);
}
```

And prints:

```
abc
xbc
```

However, this seems to be a different issue.


More information about the Digitalmars-d-learn mailing list