Saxon decides statically how to evaluate a variable based on the nature of the select expression (or initializing expression). If the select expression is subsequently rewritten by the optimizer, it should reevaluate this decision, but it is not doing so. If the evaluation strategy chosen is not suitable for the rewritten expression, this may cause a failure at runtime. In particular, one rarely-used evaluation strategy is "shared append expression", and is used when the select expression appends an item to a sequence. This strategy is designed to reduce the need for copying of a sequence in the case of a recursive function or template that appends one item to a sequence on each recursive call; however, it may also be used where this is not the case. In such cases, if the select expression is changed by the optimizer from its original form, a ClassCastException occurs at runtime.
A patch to module GeneralVariable is being placed in Subversion on the 9.1 branch. Test case bug84 added.