haskell / haskell/c2hs

C function takes `Color *` (a Color pointer), c2hs foreign import takes Ptr ()

Open
#246 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
211
Forks
52
PR merge metrics
No merged PRs in 30d

Description

I have the following code in `Lib.chs`:

```haskell
data Color = Color !Word8 !Word8 !Word8 !Word8 deriving (Show, Eq)

instance Storable Color where
...

#include "foo.h"

{# fun unsafe ClearBackground as ^ {with* %`Color'} -> `()' #}
```
During the build `Lib.chs.c` is generated by C2HS and contains:

```C
#include "Lib.chs.h"
void __c2hs_wrapped__ClearBackground(Color * color)
{
return ClearBackground(*color);
}
```

And `Lib.hs` is also generated by C2HS and contains, in part:

```haskell
...
foreign import ccall unsafe "Lib.chs.h __c2hs_wrapped__ClearBackground"
clearBackground'_ :: ((C2HSImp.Ptr ()) -> (IO ()))
...
```
Which results in the following type error:

```
src/Lib.chs:26:21: error:
• Couldn't match type ‘Color’ with ‘()’
Expected type: C2HSImp.Ptr ()
Actual type: C2HSImp.Ptr Color
• In the first argument of ‘clearBackground'_’, namely ‘a1'’
In the first argument of ‘(>>)’, namely ‘clearBackground'_ a1'’
In the expression: clearBackground'_ a1' >> return ()
```
The problem is that the generated C wrapper accepts a `Color *` (a Color pointer), but the "foreign import" tells Haskell that it accepts a `Ptr ()`. It should be `Ptr Color` instead of `Ptr ()` I believe, something like:

```haskell
foreign import ccall "Lib.chs.h __c2hs_wrapped__ClearBackground"
clearBackground'_ :: Ptr Color -> IO ()
```

A full (yet minimal) demo can be checked out from a repo I created: https://github.com/DevJac/c2hs-bug-demo

C2HS version:

```
C->Haskell Compiler, version 0.28.6 Switcheroo, 25 November 2017
build platform is "x86_64-linux" <1, True, True, 1>
```

I'm using Cabal 3.0 and GHC 8.6.5.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.