Skip to content

Strange behavior when using newtyped version of IO monad in FFI import declarations

Take a look at these two modules:

A.hs:

module A (GL) where

import Control.Monad
import Control.Monad.Trans

newtype GL a = GL (IO a) deriving (Functor, Monad, MonadIO)

B.hs:

module B where

import A

foreign import CALLCONV unsafe "glFlush" flush :: GL ()

Now, compile them with optimizations:

[david@alonzo temp]$ $HOME/ghc-6.10.1/bin/ghc A.hs B.hs -XGeneralizedNewtypeDeriving -XForeignFunctionInterface -DCALLCONV=ccall -XCPP -O
compilation IS NOT required
compilation IS NOT required
A.o:(.text+0x8e): undefined reference to `mtlzm1zi1zi0zi2_ControlziMonadziTrans_ZCDMonadIO_con_info'
A.o:(.text+0x237): undefined reference to `__stginit_mtlzm1zi1zi0zi2_ControlziMonadziTrans_'
B.o:(.text+0x1e): undefined reference to `glFlush'
/home/david/ghc-6.10.1//lib/ghc-6.10.1/libHSrts.a(Main.o): In function `real_main':
Main.c:(.text+0x12): undefined reference to `__stginit_ZCMain'
Main.c:(.text+0x26): undefined reference to `ZCMain_main_closure'
collect2: ld returnerade avslutningsstatus 1

It's OK - we pass the typechecking phase.

Now, see what happens when we compile without -O:

[david@alonzo temp]$ rm -f *.hi *.o
[david@alonzo temp]$ $HOME/ghc-6.10.1/bin/ghc A.hs B.hs -XGeneralizedNewtypeDeriving -XForeignFunctionInterface -DCALLCONV=ccall -XCPP

B.hs:5:0:
    Unacceptable result type in foreign declaration: GL ()
    When checking declaration:
        foreign import ccall unsafe "static  &glFlush" flush :: GL ()

Putting everything into one single module makes the type error go away completely (that is, also when compiling without -O).

Something strange must be going on.

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