Fix casts in constant expressions

Fixes #17074.