Should we allow a newtype with a representation-polymorphic field with NoFieldSelectors?
The following newtype declaration is accepted:
{-# LANGUAGE PolyKinds, UnliftedNewtypes#-}
import GHC.Exts
newtype X (a :: TYPE rep) = MkX a
However, the following is rejected:
{-# LANGUAGE PolyKinds, UnliftedNewtypes, NoFieldSelectors #-}
import GHC.Exts
newtype Y (a :: TYPE rep) = MkY { y_fld :: a }
A representation-polymorphic type is not allowed here:
Type: a
Kind: TYPE rep
In the type of binder `$sel:y_fld:MkY'
|
4 | newtype Y (a :: TYPE rep) = MkY { y_fld :: a }
| ^^^^^
The problem is that the field selector is still generated internally (whence the mangled selector name $sel:y_fld:MkY
leaking out into the error message), which runs into trouble with the representation polymorphism checks.
I'm not sure exactly what to do. We could continue to reject the program (possibly improving the error message); we could not generate the field selector at all; or we could somehow attempt to generate a representation polymorphic selector which must be representation-monomorphic at use-sites.
cc-ing @adamgundry and @simonpj for thoughts.