dmd icon indicating copy to clipboard operation
dmd copied to clipboard

`__rvalue` breaks immutability

Open limepoutine opened this issue 1 month ago • 1 comments

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
}

limepoutine avatar Nov 13 '25 04:11 limepoutine

Simplified the code a bit:

  • The getter isn’t needed.
  • __gshared isn’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");
}

Bolpat avatar Nov 20 '25 12:11 Bolpat