Issue 22244 - Key tail immutability should allow hashmap to be impl converted from immutable to mutable
Summary: Key tail immutability should allow hashmap to be impl converted from immutabl...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P3 minor
Assignee: No Owner
URL:
Keywords: industry
Depends on:
Blocks:
 
Reported: 2021-08-27 09:03 UTC by FeepingCreature
Modified: 2022-12-17 10:35 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description FeepingCreature 2021-08-27 09:03:18 UTC
Consider this code:

struct Key
{
    immutable(int)[] reference;
}

struct Foo
{
    immutable(int)[Key] hashmap;
}

void main()
{
    immutable Foo foo = Foo();
    Foo bar = foo;
}

'Key' is clearly tail-immutable.

So 'Foo' is likewise tail-immutable, because the values of 'hashmap' cannot be changed, and the keys cannot be changed anyways - not by head, because key heads cannot be changed in a hashmap period (cannot be referenced, cannot be mutated), nor by tail, because Key is tail immutable.

There is a rule that tail immutable types can implicitly drop qualifier immutability, because they don't expose any further mutability on their referenced data that way.

Compare:

immutable string a;
string b = a;

So under the same logic, Foo should be able to convert to mutable.

(But it can't.)
Comment 1 FeepingCreature 2022-10-05 12:25:57 UTC
Simpler demonstration of the issue:

```
immutable int[string] foo;
immutable(int)[string] bar = foo;
```

This should work, because if we cast it:

```
immutable int[string] foo;
immutable(int)[string] bar = foo;
bar["foo"] = 5; // cannot modify `immutable` expression `bar["foo"]`
```

we see that the original `foo` is still protected. So the implconv should go through.