Skip to content

"foreign import capi" causes panic with sqlite

Summary

Trying to foreign import capi a sqlite function causes a panic.

Steps to reproduce

Install the sqlite library in your system. For example in Ubuntu, run apt-get install libsqlite3-0 libsqlite3-dev.

Then try to compile the following program:

{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI #-}
module Main where

import Foreign
import Foreign.C

type CTraceCallback a
     = Ptr a
    -> CString      -- ^ UTF-8 rendering of the SQL statement text as
                    --   the statement first begins executing.
    -> IO ()

foreign import capi "wrapper"
    mkCTraceCallback :: CTraceCallback a -> IO (FunPtr (CTraceCallback a))

main :: IO ()
main = pure ()

The compilation fails with:

[1 of 1] Compiling Main             ( Main.hs, Main.o )
ghc: panic! (the 'impossible' happened)
  (GHC version 9.2.2:
        ccallConvToInt CApiConv

If the calling convention is changed to ccall, the program compiles.

Expected behavior

The program should compile with the capi calling convention.

Environment

  • I tried with GHC versions 8.10.7 and 9.2.2

  • Operating System: Ubuntu Linux (running inside WSL2 in Windows)

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