From 0d917dbb46f91151314a2d50573d5acd4b70213f Mon Sep 17 00:00:00 2001 From: Alec Theriault Date: Thu, 10 Jan 2019 23:44:04 -0800 Subject: Error out of invalid Int/Word bit shifts Although the Haddock's for `shiftL` and `shiftR` do require the number of bits to be non-negative, we should still check this before calling out to primitives (which also have undefined behaviour for negative bit shifts). If a user _really_ wants to bypass checks that the number of bits is sensible, they already have the aptly-named `unsafeShiftL`/`unsafeShiftR` at their disposal. See #16111. --- compiler/prelude/PrelRules.hs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'compiler/prelude/PrelRules.hs') diff --git a/compiler/prelude/PrelRules.hs b/compiler/prelude/PrelRules.hs index f8b8f91bcc..7111c7b07a 100644 --- 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)) -- Do the shift at type Integer, but shift length is Int Lit (LitNumber nt x t) + | 0 < shift_len + , shift_len <= wordSizeInBits dflags -> let op = shift_op dflags y = x `op` fromInteger shift_len in liftMaybe $ Just (Lit (mkLitNumberWrap dflags nt y t)) -- cgit v1.2.1