strange interaction of `deriving` and `TypeError` makes it so that `TypeError`s get deferred to the useage sites
Summary
When trying to anyclass
or via
derive an instance that uses an instance that has a TypeError
constraint, the TypeError
doesn't where the instance is derived but rather where the instance is used; if the instance is written manually, the error appears as expected.
Steps to reproduce
- load this code and try to run
allFinite @A
, this will result in the expected type error, however, if you comment out the manually written instance at the bottom, this will immediately result in a type error.
the error
<interactive>:1:1: error:
• Bool in constr of type
• In the expression: allFinite @A
In an equation for ‘it’: it = allFinite @A
(0.00 secs,)
class Finite a where
allFinite :: [a]
default allFinite :: (Generic a, GFinite (Rep a)) => [a]
allFinite = gallFinite
newtype Generically a = Generically a
deriving stock (Eq, Ord, Show, Generic)
instance (Generic a, GFinite (Rep a)) => Finite (Generically a) where
allFinite = coerce (gallFinite @a)
gallFinite :: forall a. (GFinite (Rep a), Generic a) => [a]
gallFinite = do
to <$> gfinite @(Rep a)
class GFinite f where
gfinite :: [f a]
instance
TypeError ('ShowType c ':<>: 'Text " in constr of type")
=> GFinite (K1 r c)
where
gfinite = error "someone removed the Type error constraint"
instance GFinite f => GFinite (M1 i t f) where
gfinite = M1 <$> gfinite
newtype A = MkA Bool
deriving stock (Generic, Show)
-- this should err
-- deriving (Finite) via (Generically A)
-- this should also err
deriving anyclass (Finite)
-- this errs as expected with "Bool in constr of type"
-- instance Finite A where
-- allFinite = gallFinite
I hope the reproducer is small enough...
Expected behavior
I expect both of the deriving clauses to err instead of deferring the error to the usage site; especially the anyclass
example I expect to behave identically to the manually written instance.
Note
I first thought that this was deferred to runtime due to my tests in GHCi, that's why I closed and reopened. Sorry for that
Environment
- GHC version used:
925
,944
- Operating System:
NixOS 22.11
- System Architecture:
x86_64-linux