haskell 为自定义数值类型启用-Woverflowed-literals

oug3syen  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(164)

默认情况下,GHC允许the -Woverflowed-literals warning在文本的精度对于其类型来说太高时发出消息:

OverflowedLiterals.hs:10:12: warning: [GHC-97441] [-Woverflowed-literals]
    Literal 258 is out of the Word8 range 0..255
   |
10 |     print (258 :: Word8)
   |            ^^^

OverflowedLiterals.hs:15:12: warning: [GHC-97441] [-Woverflowed-literals]
    Literal 9223372036854775817 is out of the Int range -9223372036854775808..9223372036854775807
   |
15 |     print (9223372036854775817 :: Int)
   |            ^^^^^^^^^^^^^^^^^^^

字符串
是否有方法为自定义数值类型启用此警告?
例如,我想要这段代码

data Word4 = Word4 Bool Bool Bool Bool
instance Num Word4 where
  ...

main = print (17 :: Word4)


导致编译器抱怨

StackoverflowExample:LINE:COL: warning: [GHC-97441] [-Woverflowed-literals]
    Literal 17 is out of the Word4 range 0..15

chhqkbe1

chhqkbe11#

,或者至少在写这篇文章时没有使用ghc。检查这一点的代码位于这里[GitHub]:

warnAboutOverflowedLiterals dflags lit
 | wopt Opt_WarnOverflowedLiterals dflags
 , Just (i, tc) <- lit
 = if
    -- These only show up via the 'HsOverLit' route
    | sameUnique tc intTyConName        -> check i tc minInt         maxInt
    | sameUnique tc wordTyConName       -> check i tc minWord        maxWord
    | sameUnique tc int8TyConName       -> check i tc (min' @Int8)   (max' @Int8)
    | sameUnique tc int16TyConName      -> check i tc (min' @Int16)  (max' @Int16)
    | sameUnique tc int32TyConName      -> check i tc (min' @Int32)  (max' @Int32)
    | sameUnique tc int64TyConName      -> check i tc (min' @Int64)  (max' @Int64)
    | sameUnique tc word8TyConName      -> check i tc (min' @Word8)  (max' @Word8)
    | sameUnique tc word16TyConName     -> check i tc (min' @Word16) (max' @Word16)
    | sameUnique tc word32TyConName     -> check i tc (min' @Word32) (max' @Word32)
    | sameUnique tc word64TyConName     -> check i tc (min' @Word64) (max' @Word64)
    | sameUnique tc naturalTyConName    -> checkPositive i tc

    -- These only show up via the 'HsLit' route
    | sameUnique tc intPrimTyConName    -> check i tc minInt         maxInt
    | sameUnique tc wordPrimTyConName   -> check i tc minWord        maxWord
    | sameUnique tc int8PrimTyConName   -> check i tc (min' @Int8)   (max' @Int8)
    | sameUnique tc int16PrimTyConName  -> check i tc (min' @Int16)  (max' @Int16)
    | sameUnique tc int32PrimTyConName  -> check i tc (min' @Int32)  (max' @Int32)
    | sameUnique tc int64PrimTyConName  -> check i tc (min' @Int64)  (max' @Int64)
    | sameUnique tc word8PrimTyConName  -> check i tc (min' @Word8)  (max' @Word8)
    | sameUnique tc word16PrimTyConName -> check i tc (min' @Word16) (max' @Word16)
    | sameUnique tc word32PrimTyConName -> check i tc (min' @Word32) (max' @Word32)
    | sameUnique tc word64PrimTyConName -> check i tc (min' @Word64) (max' @Word64)

字符串
因此,它将不同的(隐式)数据构造器硬编码为与IntWordInt16Word32等一起工作,从而检查相应的下限和上限。
严格地说,这也许是可能的,但目前还没有实现。如果我们知道17被转换为的类型是NumBounded的示例,那么检查可能有点复杂,因为它只是将17转换为fromInteger 17

相关问题