- meth_binds prags is_inst_decl
- (sel_id, dm_id, explicit_dm)
- = tcGetSrcLoc `thenNF_Tc` \ loc ->
-
- newMethod origin sel_id inst_tys `thenNF_Tc` \ meth@(_, meth_id) ->
- mkTcSig meth_id loc `thenNF_Tc` \ sig_info ->
-
- let
- meth_name = idName meth_id
- maybe_user_bind = find_bind meth_name meth_binds
-
- no_user_bind = case maybe_user_bind of {Nothing -> True; other -> False}
-
- meth_bind = case maybe_user_bind of
- Just bind -> bind
- Nothing -> mk_default_bind meth_name loc
-
- meth_prags = find_prags meth_name prags
- in
-
- -- Warn if no method binding, only if -fwarn-missing-methods
- warnTc (is_inst_decl && opt_WarnMissingMethods && no_user_bind && not explicit_dm)
- (omittedMethodWarn sel_id clas) `thenNF_Tc_`
-
- -- Check the bindings; first add inst_tyvars to the envt
- -- so that we don't quantify over them in nested places
- -- The *caller* put the class/inst decl tyvars into the envt
- tcExtendGlobalTyVars (mkVarSet inst_tyvars) (
- tcAddErrCtxt (methodCtxt sel_id) $
- tcBindWithSigs NotTopLevel meth_bind
- [sig_info] meth_prags NonRecursive
- ) `thenTc` \ (binds, insts, _) ->
-
-
- tcExtendLocalValEnv [(meth_name, meth_id)] (
- tcSpecSigs meth_prags
- ) `thenTc` \ (prag_binds1, prag_lie) ->
-
- -- The prag_lie for a SPECIALISE pragma will mention the function
- -- itself, so we have to simplify them away right now lest they float
- -- outwards!
- bindInstsOfLocalFuns prag_lie [meth_id] `thenTc` \ (prag_lie', prag_binds2) ->
-
-
- -- Now check that the instance type variables
- -- (or, in the case of a class decl, the class tyvars)
- -- have not been unified with anything in the environment
- tcAddErrCtxtM (sigCtxt sig_msg inst_tyvars inst_theta (idType meth_id)) $
- checkSigTyVars inst_tyvars emptyVarSet `thenTc_`
-
- returnTc (binds `AndMonoBinds` prag_binds1 `AndMonoBinds` prag_binds2,
- insts `plusLIE` prag_lie',
- meth)
- where
- sig_msg = ptext SLIT("When checking the expected type for class method") <+> ppr sel_name
-
- sel_name = idName sel_id
-
- -- The renamer just puts the selector ID as the binder in the method binding
- -- but we must use the method name; so we substitute it here. Crude but simple.
- find_bind meth_name (FunMonoBind op_name fix matches loc)
- | op_name == sel_name = Just (FunMonoBind meth_name fix matches loc)
- find_bind meth_name (PatMonoBind (VarPatIn op_name) grhss loc)
- | op_name == sel_name = Just (PatMonoBind (VarPatIn meth_name) grhss loc)
- find_bind meth_name (AndMonoBinds b1 b2)
- = find_bind meth_name b1 `seqMaybe` find_bind meth_name b2
- find_bind meth_name other = Nothing -- Default case
-
-
- -- Find the prags for this method, and replace the
- -- selector name with the method name
- find_prags meth_name [] = []
- find_prags meth_name (SpecSig name ty loc : prags)
- | name == sel_name = SpecSig meth_name ty loc : find_prags meth_name prags
- find_prags meth_name (InlineSig name phase loc : prags)
- | name == sel_name = InlineSig meth_name phase loc : find_prags meth_name prags
- find_prags meth_name (NoInlineSig name phase loc : prags)
- | name == sel_name = NoInlineSig meth_name phase loc : find_prags meth_name prags
- find_prags meth_name (prag:prags) = find_prags meth_name prags
-
- mk_default_bind local_meth_name loc
- = PatMonoBind (VarPatIn local_meth_name)
- (GRHSs (unguardedRHS (default_expr loc) loc) EmptyBinds Nothing)
- loc
-
- default_expr loc
- | explicit_dm = HsVar (getName dm_id) -- There's a default method
- | otherwise = error_expr loc -- No default method
-
- error_expr loc = HsApp (HsVar (getName nO_METHOD_BINDING_ERROR_ID))
- (HsLit (HsString (_PK_ (error_msg loc))))
-
- error_msg loc = showSDoc (hcat [ppr loc, text "|", ppr sel_id ])
+ meth_binds prags is_inst_decl (sel_id, dm_info)
+ = tcGetSrcLoc `thenNF_Tc` \ loc ->
+ newMethod origin sel_id inst_tys `thenNF_Tc` \ meth@(_, meth_id) ->
+ mkTcSig meth_id loc `thenNF_Tc` \ sig_info ->
+ let
+ meth_name = idName meth_id
+ sig_msg = ptext SLIT("When checking the expected type for class method") <+> ppr sel_id
+ meth_prags = find_prags (idName sel_id) meth_name prags
+ in
+ -- Figure out what method binding to use
+ -- If the user suppplied one, use it, else construct a default one
+ (case find_bind (idName sel_id) meth_name meth_binds of
+ Just user_bind -> returnTc user_bind
+ Nothing -> mkDefMethRhs is_inst_decl clas inst_tys sel_id loc dm_info `thenTc` \ rhs ->
+ returnTc (FunMonoBind meth_name False -- Not infix decl
+ [mkSimpleMatch [] rhs Nothing loc] loc)
+ ) `thenTc` \ meth_bind ->
+ -- Check the bindings; first add inst_tyvars to the envt
+ -- so that we don't quantify over them in nested places
+ -- The *caller* put the class/inst decl tyvars into the envt
+ tcExtendGlobalTyVars (mkVarSet inst_tyvars)
+ (tcAddErrCtxt (methodCtxt sel_id) $
+ tcBindWithSigs NotTopLevel meth_bind
+ [sig_info] meth_prags NonRecursive
+ ) `thenTc` \ (binds, insts, _) ->
+
+ tcExtendLocalValEnv [(meth_name, meth_id)]
+ (tcSpecSigs meth_prags) `thenTc` \ (prag_binds1, prag_lie) ->
+
+ -- The prag_lie for a SPECIALISE pragma will mention the function
+ -- itself, so we have to simplify them away right now lest they float
+ -- outwards!
+ bindInstsOfLocalFuns prag_lie [meth_id] `thenTc` \ (prag_lie', prag_binds2) ->
+
+ -- Now check that the instance type variables
+ -- (or, in the case of a class decl, the class tyvars)
+ -- have not been unified with anything in the environment
+ --
+ -- We do this for each method independently to localise error messages
+ -- ...and this is why the call to tcExtendGlobalTyVars must be here
+ -- rather than in the caller
+ tcAddErrCtxtM (sigCtxt sig_msg inst_tyvars inst_theta (idType meth_id)) $
+ checkSigTyVars inst_tyvars emptyVarSet `thenTc_`
+
+ returnTc (binds `AndMonoBinds` prag_binds1 `AndMonoBinds` prag_binds2,
+ insts `plusLIE` prag_lie',
+ meth)
+
+ -- The user didn't supply a method binding,
+ -- so we have to make up a default binding
+ -- The RHS of a default method depends on the default-method info
+mkDefMethRhs is_inst_decl clas inst_tys sel_id loc (DefMeth dm_id)
+ = -- An polymorphic default method
+ returnTc (HsVar (idName dm_id))
+
+mkDefMethRhs is_inst_decl clas inst_tys sel_id loc NoDefMeth
+ = -- No default method
+ -- Warn only if -fwarn-missing-methods
+ warnTc (is_inst_decl && opt_WarnMissingMethods)
+ (omittedMethodWarn sel_id clas) `thenNF_Tc_`
+ returnTc error_rhs
+ where
+ error_rhs = HsApp (HsVar (getName nO_METHOD_BINDING_ERROR_ID))
+ (HsLit (HsString (_PK_ error_msg)))
+ error_msg = showSDoc (hcat [ppr loc, text "|", ppr sel_id ])
+
+
+mkDefMethRhs is_inst_decl clas inst_tys sel_id loc GenDefMeth
+ = -- A generic default method
+ -- If the method is defined generically, we can only do the job if the
+ -- instance declaration is for a single-parameter type class with
+ -- a type constructor applied to type arguments in the instance decl
+ -- (checkTc, so False provokes the error)
+ checkTc (not is_inst_decl || simple_inst)
+ (badGenericInstance sel_id clas) `thenTc_`
+
+ ioToTc (dumpIfSet opt_PprStyle_Debug "Generic RHS" stuff) `thenNF_Tc_`
+ returnTc rhs
+ where
+ rhs = mkGenericRhs sel_id clas_tyvar tycon
+
+ stuff = vcat [ppr clas <+> ppr inst_tys,
+ nest 4 (ppr sel_id <+> equals <+> ppr rhs)]
+
+ -- The tycon is only used in the generic case, and in that
+ -- case we require that the instance decl is for a single-parameter
+ -- type class with type variable arguments:
+ -- instance (...) => C (T a b)
+ simple_inst = maybeToBool maybe_tycon
+ clas_tyvar = head (classTyVars clas)
+ Just tycon = maybe_tycon
+ maybe_tycon = case inst_tys of
+ [ty] -> case splitTyConApp_maybe ty of
+ Just (tycon, arg_tys) | all isTyVarTy arg_tys -> Just tycon
+ other -> Nothing
+ other -> Nothing