Make const_eval of literals more type aware

Fixes #16745.
Fixes #17631.