std.typecons.Rebindable!(immutable A) is implemented as:

    private union {
        immutable(A) original;
        A stripped;

    ...@trusted assignment operators...

@property inout(immutable(A)) get() @trusted pure nothrow @nogc inout
        return original;

    alias get this;

This conforms with the D safety spec: All access to the unsafe union goes through the @trusted get() and the trusted assignment operators.

    Rebindable!(immutable A) r = a1;
    // r.original is a1
    r = a2;
    // r.original is a2

But the compiler may assume that immutable variables -- such as the immutable(A) original -- never change and thus may optimize code. Since immutable(A) original is assignable in the union, such optimization would produce wrong behavior: In the final line, the compiler could think that r.original is a1 without examining r.original.

How does Rebindable prevent the compiler from optimizing according to immutable's rules?

Reply via email to