- -- The "deriv_ty" is a LHsType to take account of the fact that for newtype derivign
- -- we allow deriving (forall a. C [a]).
-
- mk_eqn (loc, orig, new_or_data, tycon_name, hs_deriv_ty)
- = tcLookupTyCon tycon_name `thenM` \ tycon ->
- setSrcSpan loc $
- addErrCtxt (derivCtxt tycon) $
- tcExtendTyVarEnv (tyConTyVars tycon) $ -- Deriving preds may (now) mention
- -- the type variables for the type constructor
- tcHsDeriv hs_deriv_ty `thenM` \ (deriv_tvs, clas, tys) ->
- doptM Opt_GlasgowExts `thenM` \ gla_exts ->
- mk_eqn_help loc orig gla_exts new_or_data tycon deriv_tvs clas tys
+ -- The "deriv_ty" is a LHsType to take account of the fact that for
+ -- newtype deriving we allow deriving (forall a. C [a]).
+
+ mk_eqn (loc, orig, new_or_data, tycon_name, tyvars, mb_tys, hs_deriv_ty)
+ = setSrcSpan loc $
+ addErrCtxt (derivCtxt tycon_name mb_tys) $
+ do { named_tycon <- tcLookupTyCon tycon_name
+
+ -- Enable deriving preds to mention the type variables in the
+ -- instance type
+ ; tcTyVarBndrs tyvars $ \tvs -> do
+ { traceTc (text "TcDeriv.mk_eqn: tyvars:" <+> ppr tvs)
+
+ -- Lookup representation tycon in case of a family instance
+ -- NB: We already need the type variables in scope here for the
+ -- call to `dsHsType'.
+ ; tycon <- case mb_tys of
+ Nothing -> return named_tycon
+ Just hsTys -> do
+ tys <- mapM dsHsType hsTys
+ tcLookupFamInst named_tycon tys
+
+ ; (deriv_tvs, clas, tys) <- tcHsDeriv hs_deriv_ty
+ ; gla_exts <- doptM Opt_GlasgowExts
+ ; mk_eqn_help loc orig gla_exts new_or_data tycon deriv_tvs clas tys
+ }}