- -- Stop if addInstInfos etc discovers any errors
- -- (they recover, so that we get more than one error each round)
-
- -- (1) Do the ordinary instance declarations
- mappM tcLocalInstDecl1 inst_decls `thenM` \ local_inst_infos ->
-
- let
- local_inst_info = catMaybes local_inst_infos
- clas_decls = filter (isClassDecl.unLoc) tycl_decls
- in
- -- (2) Instances from generic class declarations
- getGenericInstances clas_decls `thenM` \ generic_inst_info ->
-
- -- Next, construct the instance environment so far, consisting of
- -- a) local instance decls
- -- b) generic instances
- addInsts local_inst_info $
- addInsts generic_inst_info $
-
- -- (3) Compute instances from "deriving" clauses;
- -- This stuff computes a context for the derived instance decl, so it
- -- needs to know about all the instances possible; hence inst_env4
- tcDeriving tycl_decls `thenM` \ (deriv_inst_info, deriv_binds) ->
- addInsts deriv_inst_info $
-
- getGblEnv `thenM` \ gbl_env ->
- returnM (gbl_env,
- generic_inst_info ++ deriv_inst_info ++ local_inst_info,
- deriv_binds)
+ do { -- Stop if addInstInfos etc discovers any errors
+ -- (they recover, so that we get more than one error each
+ -- round)
+
+ -- (1) Do the ordinary instance declarations and instances of
+ -- indexed types
+ ; let { idxty_decls = filter (isIdxTyDecl . unLoc) tycl_decls }
+ ; local_info_tycons <- mappM tcLocalInstDecl1 inst_decls
+ ; idxty_info_tycons <- mappM tcIdxTyInstDeclTL idxty_decls
+
+ ; let { (local_infos,
+ local_tycons) = unzip local_info_tycons
+ ; (idxty_infos,
+ idxty_tycons) = unzip idxty_info_tycons
+ ; local_idxty_info = concat local_infos ++ catMaybes idxty_infos
+ ; local_idxty_tycon = concat local_tycons ++
+ catMaybes idxty_tycons
+ ; clas_decls = filter (isClassDecl.unLoc) tycl_decls
+ ; implicit_things = concatMap implicitTyThings local_idxty_tycon
+ }
+
+ -- (2) Add the tycons of associated types and their implicit
+ -- tythings to the global environment
+ ; tcExtendGlobalEnv (local_idxty_tycon ++ implicit_things) $ do {
+
+ -- (3) Instances from generic class declarations
+ ; generic_inst_info <- getGenericInstances clas_decls
+
+ -- Next, construct the instance environment so far, consisting
+ -- of
+ -- a) local instance decls
+ -- b) generic instances
+ ; addInsts local_idxty_info $ do {
+ ; addInsts generic_inst_info $ do {
+
+ -- (4) Compute instances from "deriving" clauses;
+ -- This stuff computes a context for the derived instance
+ -- decl, so it needs to know about all the instances possible
+ ; (deriv_inst_info, deriv_binds) <- tcDeriving tycl_decls
+ ; addInsts deriv_inst_info $ do {
+
+ ; gbl_env <- getGblEnv
+ ; returnM (gbl_env,
+ generic_inst_info ++ deriv_inst_info ++ local_idxty_info,
+ deriv_binds)
+ }}}}}
+ where
+ -- Make sure that toplevel type instance are not for associated types.
+ -- !!!TODO: Need to perform this check for the InstInfo structures of type
+ -- functions, too.
+ tcIdxTyInstDeclTL ldecl@(L loc decl) =
+ do { (info, tything) <- tcIdxTyInstDecl ldecl
+ ; setSrcSpan loc $
+ when (isAssocFamily tything) $
+ addErr $ assocInClassErr (tcdName decl)
+ ; return (info, tything)
+ }
+ isAssocFamily (Just (ATyCon tycon)) =
+ case tyConFamInst_maybe tycon of
+ Nothing -> panic "isAssocFamily: no family?!?"
+ Just (fam, _) -> isAssocTyCon fam
+ isAssocFamily (Just _ ) = panic "isAssocFamily: no tycon?!?"
+ isAssocFamily Nothing = False
+
+assocInClassErr name =
+ ptext SLIT("Associated type must be inside class instance") <+>
+ quotes (ppr name)