Skip to content

Fix rewriting invalid shifts to errors

Ömer Sinan Ağacan requested to merge osa1/ghc:fix_16449 into master

Fixes #16449 (closed).

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 (closed). 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.

Edited by Ömer Sinan Ağacan

Merge request reports