- NoDefMeth -> do { warn <- doptM Opt_WarnMissingMethods
- ; warnTc (isInstDecl origin
- && warn -- Warn only if -fwarn-missing-methods
- && reportIfUnused (getOccName sel_id))
- -- Don't warn about _foo methods
- (omittedMethodWarn sel_id)
- ; return (unitBag $ L loc (VarBind meth_id error_rhs)) }
-
- DefMeth -> do { -- An polymorphic default method
- -- Might not be imported, but will be an OrigName
- dm_name <- lookupImportedName (mkDefMethRdrName sel_id)
- ; dm_id <- tcLookupId dm_name
- -- Note [Default methods in instances]
- ; return (unitBag $ L loc (VarBind meth_id (mk_dm_app dm_id))) }
-
- GenDefMeth -> ASSERT( isInstDecl origin ) -- We never get here from a class decl
- do { meth_bind <- mkGenericDefMethBind clas inst_tys sel_id meth_name
- ; tc_method_bind inst_tyvars inst_theta (this_dict:extra_insts)
- sig_fn prag_fn
- sel_id meth_id meth_bind }
-
+ NoDefMeth -> return Nothing
+ GenDefMeth -> return Nothing
+ DefMeth dm_name -> do
+ { let sel_name = idName sel_id
+ ; local_dm_name <- newLocalName sel_name
+ -- Base the local_dm_name on the selector name, because
+ -- type errors from tcInstanceMethodBody come from here
+
+ -- See Note [Silly default-method bind]
+ -- (possibly out of date)
+
+ ; let meth_bind = findMethodBind sel_name binds_in
+ `orElse` pprPanic "tcDefMeth" (ppr sel_id)
+ -- dm_info = DefMeth dm_name only if there is a binding in binds_in
+
+ dm_sig_fn _ = sig_fn sel_name
+ dm_id = mkDefaultMethodId sel_id dm_name
+ local_dm_type = instantiateMethod clas sel_id (mkTyVarTys tyvars)
+ local_dm_id = mkLocalId local_dm_name local_dm_type
+ prags = prag_fn sel_name
+
+ ; dm_id_w_inline <- addInlinePrags dm_id prags
+ ; spec_prags <- tcSpecPrags dm_id prags
+
+ ; warnTc (not (null spec_prags))
+ (ptext (sLit "Ignoring SPECIALISE pragmas on default method")
+ <+> quotes (ppr sel_name))
+
+ ; liftM Just $
+ tcInstanceMethodBody (ClsSkol clas)
+ tyvars
+ [this_dict]
+ Nothing
+ dm_id_w_inline local_dm_id
+ dm_sig_fn IsDefaultMethod meth_bind }
+
+---------------
+tcInstanceMethodBody :: SkolemInfo -> [TcTyVar] -> [EvVar]
+ -> Maybe EvBind
+ -> Id -> Id
+ -> SigFun -> TcSpecPrags -> LHsBind Name
+ -> TcM (LHsBind Id)
+tcInstanceMethodBody skol_info tyvars dfun_ev_vars
+ this_dict meth_id local_meth_id
+ meth_sig_fn specs
+ (L loc bind)
+ = do { -- Typecheck the binding, first extending the envt
+ -- so that when tcInstSig looks up the local_meth_id to find
+ -- its signature, we'll find it in the environment
+ let full_given = case this_dict of
+ Nothing -> dfun_ev_vars
+ Just (EvBind dict _) -> dict : dfun_ev_vars
+ lm_bind = L loc (bind { fun_id = L loc (idName local_meth_id) })
+ -- Substitue the local_meth_name for the binder
+ -- NB: the binding is always a FunBind
+
+ ; (ev_binds, (tc_bind, _))
+ <- checkConstraints skol_info emptyVarSet tyvars full_given $
+ tcExtendIdEnv [local_meth_id] $
+ tcPolyBinds TopLevel meth_sig_fn no_prag_fn
+ NonRecursive NonRecursive
+ [lm_bind]
+
+ -- Add the binding for this_dict, if we have one
+ ; ev_binds' <- case this_dict of
+ Nothing -> return ev_binds
+ Just (EvBind self rhs) -> extendTcEvBinds ev_binds self rhs
+
+ ; let full_bind = AbsBinds { abs_tvs = tyvars, abs_ev_vars = dfun_ev_vars
+ , abs_exports = [(tyvars, meth_id, local_meth_id, specs)]
+ , abs_ev_binds = ev_binds'
+ , abs_binds = tc_bind }
+
+ ; return (L loc full_bind) }