Skip to content

Coercion causes "impossible" error

The following, heavily-redacted version of a function in text is causing GHC to die after reporting that the impossible happened.

module Encoding where

import Data.ByteString as B
import Data.ByteString.Internal as B
import Data.Text.Internal (Text(..), safe, textP)
import Data.Word (Word8)
import Foreign.C.Types (CSize)
import Foreign.ForeignPtr (withForeignPtr, ForeignPtr)
import Foreign.Ptr (Ptr, minusPtr, plusPtr)


-- | Encode text using UTF-8 encoding.
encodeUtf8 :: Text -> ByteString
encodeUtf8 (Text arr off len) = inlinePerformIO $ do
  let size0 = max len 4
  mallocByteString size0 >>= start size0 off 0
 where
--  start :: Int -> Int -> Int -> ForeignPtr Word8 -> IO ByteString
  start size n0 m0 fp = withForeignPtr fp $ go n0 m0
   where
      offLen = off + len
      go n m ptr
        | n == offLen = return (PS fp 0 m)
        | otherwise = do
            let ensure k act
                  | size-m >= k = act
                  | otherwise = do
                      let newSize = size
                      fp' <- mallocByteString newSize
                      start newSize n m fp'
                {-# INLINE ensure #-}
            ensure 4 $ return $ PS fp 0 m
$ ghc -fext-core Encoding.hs
[1 of 1] Compiling Encoding ( Encoding.hs, Encoding.o )
ghc: panic! (the 'impossible' happened)
  (GHC version 7.6.3 for x86_64-unknown-linux):
        make_exp (App _ (Coercion _))

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

Note that it only happens when I try to make GHC output the Core, it will happily compile otherwise. The crash can also be prevented by uncommenting the type-annotation or removing the inline-annotation.

Trac metadata
Trac field Value
Version 7.6.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information