Skip to content
Snippets Groups Projects
Commit 27587df3 authored by Matthew Farkas-Dyck's avatar Matthew Farkas-Dyck :musical_note: Committed by Marge Bot
Browse files

Avoid incomplete-uni-patterns in `GHC.Types.Literal`.

We do so by introducing `mkLitNumberWrap'` whose ultimate codomain is `Integer` rather than `Literal`, and then use that rather than `mkLitNumberWrap` where we just need the number rather than the `Literal`.
parent bed812b7
No related branches found
No related tags found
No related merge requests found
......@@ -9,8 +9,6 @@
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
-- | Core literals
module GHC.Types.Literal
(
......@@ -328,7 +326,12 @@ Int/Word range.
-- | Make a literal number using wrapping semantics if the value is out of
-- bound.
mkLitNumberWrap :: Platform -> LitNumType -> Integer -> Literal
mkLitNumberWrap platform nt i = case nt of
mkLitNumberWrap platform nt i = LitNumber nt $ mkLitNumberWrap' platform nt i
-- | Make a literal number using wrapping semantics if the value is out of
-- bound.
mkLitNumberWrap' :: Platform -> LitNumType -> Integer -> Integer
mkLitNumberWrap' platform nt i = case nt of
LitNumInt -> case platformWordSize platform of
PW4 -> wrap @Int32
PW8 -> wrap @Int64
......@@ -345,10 +348,10 @@ mkLitNumberWrap platform nt i = case nt of
LitNumWord64 -> wrap @Word64
LitNumBigNat
| i < 0 -> panic "mkLitNumberWrap: trying to create a negative BigNat"
| otherwise -> LitNumber nt i
| otherwise -> i
where
wrap :: forall a. (Integral a, Num a) => Literal
wrap = LitNumber nt (toInteger (fromIntegral i :: a))
wrap :: forall a. (Integral a, Num a) => Integer
wrap = toInteger (fromIntegral i :: a)
-- | Wrap a literal number according to its type using wrapping semantics.
litNumWrap :: Platform -> Literal -> Literal
......@@ -364,9 +367,7 @@ litNumCoerce _ _ l = pprPanic "litNumWrapCoerce: not a n
-- converting it back to its original type.
litNumNarrow :: LitNumType -> Platform -> Literal -> Literal
litNumNarrow pt platform (LitNumber nt i)
= case mkLitNumberWrap platform pt i of
LitNumber _ j -> mkLitNumberWrap platform nt j
l -> pprPanic "litNumNarrow: got invalid literal" (ppr l)
= mkLitNumberWrap platform nt . mkLitNumberWrap' platform pt $ i
litNumNarrow _ _ l = pprPanic "litNumNarrow: invalid literal" (ppr l)
......@@ -429,9 +430,9 @@ mkLitIntUnchecked i = LitNumber LitNumInt i
-- the argument is wrapped and the overflow flag will be set.
-- See Note [Word/Int underflow/overflow]
mkLitIntWrapC :: Platform -> Integer -> (Literal, Bool)
mkLitIntWrapC platform i = (n, i /= i')
mkLitIntWrapC platform i = (LitNumber LitNumInt i', i /= i')
where
n@(LitNumber _ i') = mkLitIntWrap platform i
i' = mkLitNumberWrap' platform LitNumInt i
-- | Creates a 'Literal' of type @Word#@
mkLitWord :: Platform -> Integer -> Literal
......@@ -453,9 +454,9 @@ mkLitWordUnchecked i = LitNumber LitNumWord i
-- the argument is wrapped and the carry flag will be set.
-- See Note [Word/Int underflow/overflow]
mkLitWordWrapC :: Platform -> Integer -> (Literal, Bool)
mkLitWordWrapC platform i = (n, i /= i')
mkLitWordWrapC platform i = (LitNumber LitNumWord i', i /= i')
where
n@(LitNumber _ i') = mkLitWordWrap platform i
i' = mkLitNumberWrap' platform LitNumWord i
-- | Creates a 'Literal' of type @Int8#@
mkLitInt8 :: Integer -> Literal
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment