Fix rewriting invalid shifts to errors
authorÖmer Sinan Ağacan <omeragacan@gmail.com>
Fri, 24 May 2019 08:23:01 +0000 (11:23 +0300)
committerMarge Bot <ben+marge-bot@smart-cactus.org>
Sat, 1 Jun 2019 15:18:57 +0000 (11:18 -0400)
commit1503da32d26fb59fb6ebb620bfd0f8c08638f627
tree8933e408efdbdec39de3e547b153aa1522c5714f
parent76e5889017ee4ac688901d37f2fa684e807769b6
Fix rewriting invalid shifts to errors

Fixes #16449.

5341edf3 removed a code in rewrite rules for bit shifts, which broke the
"silly shift guard", causing generating invalid bit shifts or heap
overflow in compile time while trying to evaluate those invalid bit
shifts.

The "guard" is explained in Note [Guarding against silly shifts] in
PrelRules.hs.

More specifically, this was the breaking change:

    --- a/compiler/prelude/PrelRules.hs
    +++ b/compiler/prelude/PrelRules.hs
    @@ -474,12 +474,11 @@ shiftRule shift_op
            ; case e1 of
                _ | shift_len == 0
                  -> return e1
    -             | shift_len < 0 || wordSizeInBits dflags < shift_len
    -             -> return (mkRuntimeErrorApp rUNTIME_ERROR_ID wordPrimTy
    -                                        ("Bad shift length" ++ show shift_len))

This patch reverts this change.

Two new tests added:

- T16449_1: The original reproducer in #16449. This was previously
  casing a heap overflow in compile time when CmmOpt tries to evaluate
  the large (invalid) bit shift in compile time, using `Integer` as the
  result type. Now it builds as expected. We now generate an error for
  the shift as expected.

- T16449_2: Tests code generator for large (invalid) bit shifts.
compiler/prelude/PrelRules.hs
testsuite/tests/codeGen/should_compile/T16449_1.hs [new file with mode: 0644]
testsuite/tests/codeGen/should_compile/all.T
testsuite/tests/codeGen/should_run/T16449_2.hs [new file with mode: 0644]
testsuite/tests/codeGen/should_run/T16449_2.stderr [new file with mode: 0644]
testsuite/tests/codeGen/should_run/all.T