capi panic (toCType Int#)
The following program crashes in 8.2.2 and HEAD (8.5.20171228).
```hs
{-# LANGUAGE ForeignFunctionInterface, CApiFFI, GHCForeignImportPrim,
QuasiQuotes, TemplateHaskell, JavaScriptFFI, MagicHash,
UnliftedFFITypes #-}
module TH_foreignCallingConventions where
import GHC.Prim
import Control.Applicative
import Language.Haskell.TH
import System.IO
import Foreign.Ptr
$( do let fi cconv safety lbl name ty =
ForeignD (ImportF cconv safety lbl name ty)
dec1 <- fi CCall Interruptible "&" (mkName "foo") <$> [t| Ptr () |]
dec2 <- fi CApi Safe "bar" (mkName "bar") <$> [t| Int# -> Int# |]
-- the declarations below would result in warnings or errors when returned
dec3 <- fi CApi Unsafe "baz" (mkName "baz") <$> [t| Double -> IO () |]
dec4 <- fi StdCall Safe "bay" (mkName "bay") <$> [t| (Int -> Bool) -> IO Int |]
dec5 <- fi JavaScript Unsafe "bax" (mkName "bax") <$> [t| Ptr Int -> IO String |]
runIO $
mapM_ (putStrLn . pprint) [dec1, dec2, dec3, dec4, dec5] >> hFlush stdout
return [dec1, dec2]
)
```
This program is derived by mutating test TH_foreignCallingConventions.hs.
```diff
diff --git a/testsuite/tests/th/TH_foreignCallingConventions.hs b/testsuite/tests/th/TH_foreignCallingConventions.hs
index ee39510..16789af 100644
--- a/testsuite/tests/th/TH_foreignCallingConventions.hs
+++ b/testsuite/tests/th/TH_foreignCallingConventions.hs
@@ -13,7 +13,7 @@ import Foreign.Ptr
$( do let fi cconv safety lbl name ty =
ForeignD (ImportF cconv safety lbl name ty)
dec1 <- fi CCall Interruptible "&" (mkName "foo") <$> [t| Ptr () |]
- dec2 <- fi Prim Safe "bar" (mkName "bar") <$> [t| Int# -> Int# |]
+ dec2 <- fi CApi Safe "bar" (mkName "bar") <$> [t| Int# -> Int# |]
-- the declarations below would result in warnings or errors when returned
dec3 <- fi CApi Unsafe "baz" (mkName "baz") <$> [t| Double -> IO () |]
dec4 <- fi StdCall Safe "bay" (mkName "bay") <$> [t| (Int -> Bool) -> IO Int |]
```
Log:
```
foreign import capi safe "bar" bar :: GHC.Prim.Int# ->
GHC.Prim.Int#
foreign import capi unsafe "baz" baz :: GHC.Types.Double ->
GHC.Types.IO ()
foreign import stdcall safe "bay" bay :: (GHC.Types.Int ->
GHC.Types.Bool) ->
GHC.Types.IO GHC.Types.Int
foreign import javascript unsafe "bax" bax :: GHC.Ptr.Ptr GHC.Types.Int ->
GHC.Types.IO GHC.Base.String
ghc: panic! (the 'impossible' happened)
(GHC version 8.5.20171228 for x86_64-unknown-linux):
toCType
Int#
Call stack:
CallStack (from HasCallStack):
callStackDoc, called at compiler/utils/Outputable.hs:1150:37 in ghc:Outputable
pprPanic, called at compiler/deSugar/DsForeign.hs:730:17 in ghc:DsForeign
Please report this as a GHC bug: http://www.haskell.org/ghc/reportabug
```
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | -------------- |
| Version | 8.2.2 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | Linux |
| Architecture | x86_64 (amd64) |
</details>
<!-- {"blocked_by":[],"summary":"HEAD panic in ghc:DsForeign: toCType","status":"New","operating_system":"Linux","component":"Compiler","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.2.2","keywords":[],"differentials":[],"test_case":"","architecture":"x86_64 (amd64)","cc":[""],"type":"Bug","description":"The following program crashes in 8.2.2 and HEAD (8.5.20171228).\r\n\r\n{{{#!hs\r\n{-# LANGUAGE ForeignFunctionInterface, CApiFFI, GHCForeignImportPrim,\r\n QuasiQuotes, TemplateHaskell, JavaScriptFFI, MagicHash,\r\n UnliftedFFITypes #-}\r\n\r\nmodule TH_foreignCallingConventions where\r\n\r\nimport GHC.Prim\r\nimport Control.Applicative\r\nimport Language.Haskell.TH\r\nimport System.IO\r\nimport Foreign.Ptr\r\n\r\n$( do let fi cconv safety lbl name ty =\r\n ForeignD (ImportF cconv safety lbl name ty)\r\n dec1 <- fi CCall Interruptible \"&\" (mkName \"foo\") <$> [t| Ptr () |]\r\n dec2 <- fi CApi Safe \"bar\" (mkName \"bar\") <$> [t| Int# -> Int# |]\r\n -- the declarations below would result in warnings or errors when returned\r\n dec3 <- fi CApi Unsafe \"baz\" (mkName \"baz\") <$> [t| Double -> IO () |]\r\n dec4 <- fi StdCall Safe \"bay\" (mkName \"bay\") <$> [t| (Int -> Bool) -> IO Int |]\r\n dec5 <- fi JavaScript Unsafe \"bax\" (mkName \"bax\") <$> [t| Ptr Int -> IO String |]\r\n runIO $\r\n mapM_ (putStrLn . pprint) [dec1, dec2, dec3, dec4, dec5] >> hFlush stdout\r\n return [dec1, dec2]\r\n )\r\n}}}\r\n\r\nThis program is derived by mutating test TH_foreignCallingConventions.hs.\r\n\r\n{{{#!diff\r\ndiff --git a/testsuite/tests/th/TH_foreignCallingConventions.hs b/testsuite/tests/th/TH_foreignCallingConventions.hs\r\nindex ee39510..16789af 100644\r\n--- a/testsuite/tests/th/TH_foreignCallingConventions.hs\r\n+++ b/testsuite/tests/th/TH_foreignCallingConventions.hs\r\n@@ -13,7 +13,7 @@ import Foreign.Ptr\r\n $( do let fi cconv safety lbl name ty =\r\n ForeignD (ImportF cconv safety lbl name ty)\r\n dec1 <- fi CCall Interruptible \"&\" (mkName \"foo\") <$> [t| Ptr () |]\r\n- dec2 <- fi Prim Safe \"bar\" (mkName \"bar\") <$> [t| Int# -> Int# |]\r\n+ dec2 <- fi CApi Safe \"bar\" (mkName \"bar\") <$> [t| Int# -> Int# |]\r\n -- the declarations below would result in warnings or errors when returned\r\n dec3 <- fi CApi Unsafe \"baz\" (mkName \"baz\") <$> [t| Double -> IO () |]\r\n dec4 <- fi StdCall Safe \"bay\" (mkName \"bay\") <$> [t| (Int -> Bool) -> IO Int |]\r\n}}}\r\n\r\nLog:\r\n\r\n{{{\r\nforeign import capi safe \"bar\" bar :: GHC.Prim.Int# ->\r\n GHC.Prim.Int#\r\nforeign import capi unsafe \"baz\" baz :: GHC.Types.Double ->\r\n GHC.Types.IO ()\r\nforeign import stdcall safe \"bay\" bay :: (GHC.Types.Int ->\r\n GHC.Types.Bool) ->\r\n GHC.Types.IO GHC.Types.Int\r\nforeign import javascript unsafe \"bax\" bax :: GHC.Ptr.Ptr GHC.Types.Int ->\r\n GHC.Types.IO GHC.Base.String\r\nghc: panic! (the 'impossible' happened)\r\n (GHC version 8.5.20171228 for x86_64-unknown-linux):\r\n\ttoCType\r\n Int#\r\n Call stack:\r\n CallStack (from HasCallStack):\r\n callStackDoc, called at compiler/utils/Outputable.hs:1150:37 in ghc:Outputable\r\n pprPanic, called at compiler/deSugar/DsForeign.hs:730:17 in ghc:DsForeign\r\n\r\nPlease report this as a GHC bug: http://www.haskell.org/ghc/reportabug\r\n\r\n}}}","type_of_failure":"OtherFailure","blocking":[]} -->
issue