Fix ICE when casting on constant expressions. Closes #9867