c2hs icon indicating copy to clipboard operation
c2hs copied to clipboard

Redundant import when creating foreign #pointer

Open dbeacham opened this issue 8 years ago • 1 comments

e.g.

module Test where

data Test
{#pointer *test as TestPtr foreign -> Test}

generates

module Test where

import qualified Foreign.ForeignPtr as C2HSImp
import qualified Foreign.Ptr as C2HSImp

data Test
type TestPtr = C2HSImp.ForeignPtr (Test)

The extra (unused) Foreign.Ptr import blows up on -Werror.

dbeacham avatar Sep 01 '16 13:09 dbeacham

I've run into something very similar to fix alongside the above, when the (admittedly low priority) bug does get addressed: those same two imports are added even if the macro includes nocode. The type safety (-> Test) doesn't affect things.

Basically, it seems like every #pointer macro imports Foreign.Ptr and every #pointer ... foreign also imports Foreign.ForeignPtr. In certain cases, those are not necessary:

  • #pointer ... nocode never needs any imports; the user will probably need them in defining the type, but even if that does occur in the same module, they'll probably be using their own qualified name.
  • #pointer ... foreign needs Foreign.Ptr only if newtype is also specified; otherwise C2HS doesn't create the with* marshalling function that uses it.

I suppose it's possible that the registration requires those types to be present, but not knowing the backend code, I'd guess it's probably fine leaving (or moving) the importing of those modules to #fun or other macros, which would probably be needing them for other reasons anyway.

ag-eitilt avatar Dec 02 '18 02:12 ag-eitilt