jcpetruzza / barbies

BSD 3-Clause "New" or "Revised" License
92 stars 15 forks source link

Compilation error on GHC 9.2.2 #43

Closed clintonmead closed 1 year ago

clintonmead commented 1 year ago

The following code:

data T f = T (f Int) deriving (Generic, FunctorB)

Fails to build on GHC 9.2.2 for me, giving the error:

    • Couldn't match representation of type: barbies-2.0.3.1:Data.Generics.GenericN.Rec
                                               (barbies-2.0.3.1:Data.Generics.GenericN.Param
                                                  0 f Int)
                                               (f Int)
                               with that of: K1 R (f Int)
        arising from the 'deriving' clause of a data type declaration
      The data constructor ‘barbies-2.0.3.1:Data.Generics.GenericN.Rec’
        of newtype ‘barbies-2.0.3.1:Data.Generics.GenericN.Rec’
        is not in scope
    • When deriving the instance for (FunctorB T)

I think I'm following the docs close enough here and this trivial example should compile, but I'm not sure if this is a bug or just me doing something wrong.

jcpetruzza commented 1 year ago

The error message is quite unfriendly, but I think the issue is that you are probably doing something like

import Barbies(FunctorB(..))

and you need to import Rec as well:

import Barbies(FunctorB(..), Rec(..))

The hint that this is missing comes from here:

The data constructor 'barbies-2.0.3.1:Data.Generics.GenericN.Rec' is not in scope.

Why it is needed? It is a newtype used to make the generic derivations work, and we rely on coerce for the whole thing to work. However, this means that the newtype constructor needs to be in scope whenever you derive an instance (I guess it would be nice to have a COERCIBLE pragma that drops this requirement for certain newtypes?)

I hope that solves the issue. If you can suggest a good place in the docs where we should mention this, that would be great (a PR would be even more great! :))

clintonmead commented 1 year ago

This sorted the issue. Thank you!