dmd
dmd copied to clipboard
`__rvalue` breaks immutability
struct S
{
int i;
~this()
{
i++;
}
}
__gshared immutable S s;
ref immutable(S) get() __rvalue
{
return s;
}
void consume(S s) {}
void main()
{
assert(s.i == 0);
consume(get);
assert(s.i == 0); // immutable variable mutated
}
Simplified the code a bit:
- The getter isn’t needed.
__gsharedisn’t needed.
struct S
{
int i;
~this() { i++; }
}
immutable S s;
void consume(S s) {}
void main()
{
consume(__rvalue(s));
assert(s.i == 0, "immutable broke");
}