To answer your previous question (and why for that example the code doesn't fail) is because for a specific node type, no ancestors are found (hence the loop is never executed - see above). 

In the case of residual definitions, 1 ancestor is found (nt:base) which causes the loop to be executed and exposes the failure.

But I think you're right: something seems wrong about this entire algorithm.