Reapply r176133 with testcase fixes.