+
+ where
+ bale_out msg = failWithTc (derivingThingErr False cls cls_tys tc_app msg)
+
+ mk_alg_eqn tycon tc_args
+ | className cls `elem` typeableClassNames
+ = do { dflags <- getDOpts
+ ; case checkTypeableConditions (dflags, tycon) of
+ Just err -> bale_out err
+ Nothing -> mk_typeable_eqn orig tvs cls tycon tc_args mtheta }
+
+ | isDataFamilyTyCon tycon
+ , length tc_args /= tyConArity tycon
+ = bale_out (ptext (sLit "Unsaturated data family application"))
+
+ | otherwise
+ = do { (rep_tc, rep_tc_args) <- tcLookupDataFamInst tycon tc_args
+ -- Be careful to test rep_tc here: in the case of families,
+ -- we want to check the instance tycon, not the family tycon
+
+ -- For standalone deriving (mtheta /= Nothing),
+ -- check that all the data constructors are in scope.
+ ; rdr_env <- getGlobalRdrEnv
+ ; let hidden_data_cons = isAbstractTyCon rep_tc ||
+ any not_in_scope (tyConDataCons rep_tc)
+ not_in_scope dc = null (lookupGRE_Name rdr_env (dataConName dc))
+ ; unless (isNothing mtheta || not hidden_data_cons)
+ (bale_out (derivingHiddenErr tycon))
+
+ ; dflags <- getDOpts
+ ; if isDataTyCon rep_tc then
+ mkDataTypeEqn orig dflags tvs cls cls_tys
+ tycon tc_args rep_tc rep_tc_args mtheta
+ else
+ mkNewTypeEqn orig dflags tvs cls cls_tys
+ tycon tc_args rep_tc rep_tc_args mtheta }