summaryrefslogtreecommitdiff
path: root/compiler
diff options
context:
space:
mode:
authorAlec Theriault <alec.theriault@gmail.com>2019-01-10 23:44:04 -0800
committerBen Gamari <ben@well-typed.com>2019-01-23 14:07:28 -0500
commit5341edf3635f2875271acc469570481c52000374 (patch)
tree98fad051d47888913fa8492170ff537330e5c7eb /compiler
parenta90a2aea94b306cf557e74c4c3ed65959d05c20c (diff)
downloadhaskell-5341edf3635f2875271acc469570481c52000374.tar.gz
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.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/prelude/PrelRules.hs5
1 files changed, 2 insertions, 3 deletions
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))