GROOVY-6965: Fix ClassCastException during type checking