Rebase + squash + fixup of #814.

This isn't a problem introduced by this PR, but while checking the correctness of this PR I realized this comment says the range restriction needs to happen after the prepro, but preprocessor.apply is called at the very end of the function.