Make const_eval of literals more type aware

Closes #12501.
Fixes #16745.
Fixes #17631.