Skip to content

Dodgy-foreign-imports warning seems irrelevant in case of capi value foreign imports

Summary

The dodgy-foreign-imports warning triggers when an imported name has type of a function pointer but not tagged by an ampersand. This is quite legitimate in the ccall foreign imports.

foreign import ccall "exp" a_exp :: FunPtr (Double -> Double)

GHC will warn that exp is not tagged by an ampersand, and this is a sound warning as the only thing that can be imported in the ccall API is a function. As such, type FunPtr ... unambiguously prompts that a_exp is destined to be a function pointer converted from a C function exp of type Double -> Double and as such, it's expected to be tagged by an ampersand.

The capi value is quite another case.

foreign import capi "pcre.h value pcre_free" c_pcre_free :: FinalizerPtr a

This is a real use-case from this issue. The explicit value keyword should prompt the compiler that I want to import a value of the exact type printed on the right-hand side, i.e. FinalizerPtr a. Unfortunately, this type happens to be a FunPtr as soon as pcre_free is really a function pointer from C PCRE library. As such, the dodgy-foreign-imports warning triggers prompting to put an ampersand before pcre_free. Note that

foreign import capi "pcre.h value &pcre_free" c_pcre_free :: FinalizerPtr a

is not only looks dodgier, but also is a mere parse error.

Therefore, this warning seems irrelevant in the capi value case. If what written here makes sense (perhaps I'm missing a deeper idea), there is a merge request I've already made. The fix includes tests that check the warning is not triggered for the capi value anymore.

Steps to reproduce

Put

foreign import capi "pcre.h value pcre_free" c_pcre_free :: FinalizerPtr a

and you'll get the warning.

Expected behavior

No warning in the capi value cases.

Environment

  • GHC version used: any (mine is 9.6.2)

Optional:

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