Saxon incorrectly optimizes the expression X[position() = N to last()], or more generally, any expression of the form X[position() = N to M] where N or M depend on the focus. The effect is that N and M are evaluated with respect to the focus that applies outside the predicate.
A patch is being committed to Subversion for the 9.1 and 9.2 branches. Affects module FilterExpression.java, around line 595. A test case last-23 is being added to the W3C XQuery test suite.