Skip to content

Blacklist error, undefined, unsafeCoerce, and unsafeCoerce# from valid hole fits

Motivation

Let's say I'm writing some code and I remember that there a function that converts Word8# to Word#, but I cannot remember what it is:

import GHC.Exts

index# :: ByteArray# -> Int# -> Word8#
index# a i = _ (indexWord8Array# a i)

GHC makes several recommendations:

    • Found hole: _ :: Word# -> Word8#
    ...
    • Relevant bindings include
      ...
      Valid hole fits include
        narrowWord8# :: Word# -> Word8#
          (imported from ‘GHC.Exts’ at src-imp/Word8.hs:10:1-15
           (and originally defined in ‘ghc-prim-0.5.3:GHC.Prim’))
        magicDict :: forall a. a
          with magicDict @(Word# -> Word8#)
          (imported from ‘GHC.Exts’ at src-imp/Word8.hs:10:1-15
           (and originally defined in ‘ghc-prim-0.5.3:GHC.Prim’))
        unsafeCoerce# :: forall a b. a -> b
          with unsafeCoerce# @'WordRep @'Word8Rep @Word# @Word8#
          (imported from ‘GHC.Exts’ at src-imp/Word8.hs:10:1-15
           (and originally defined in ‘ghc-prim-0.5.3:GHC.Prim’))

GHC telling me that unsafeCoerce# typechecks as a fit for a hole is not helpful. I've seen this happen with other functions too. Namely: error, undefined, errorWithoutStackTrace, unsafeCoerce. Also, in this example, magicDict shows up. I don't what that is, but it looks like something that shouldn't be recommended.

Proposal

Have a blacklist for valid hole fits. This wouldn't always shoot down everything worthless (it could only target known-key stuff), but it could clean up some of these unhelpful suggestions that GHC makes fairly often.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information