Burton Radons wrote:
Obviously I made it contrived so that it's as clear as possible what
the issue is. In reality, it will be going through more layers.
Here's one layer:

int [] a = new int [1]; a [0] = 1;

invariant (int) [] func (invariant (int) [] a) { return a; }

auto b = func (cast (invariant (int) []) a);

Notice this has the same pattern as std.string.replace; that's why I
did that cast.

a [0] += b [0]; a [0] += b [0]; writef ("%s\n", a [0]); // Not
optimised: 4. // Assuming b cannot be modified: 3.

When this actually crops up in bugs the reality will be far more
complex and practically impossible to discover.


The solution is, when you cast things to invariant, that's a one way ticket. You cannot continue to mutate the original. If you find you need to, the data must be duplicated, and the duplicate made invariant. If this is done a lot, it's time to refactor the program.

Casts are greppable, and should be reviewed anyway. In this case, casting an array to immutable should be followed by NO further references to the mutable version.

Reply via email to