fix #8768, make `isimmutable` a generic function

(cherry picked from commit 7efbf2005b233ad9f26eda123ce4d4213432a9b0)