- -> InstOrigin s
- -> TcType s -- Instance type
- -> Id -- The method selector
- -> RenamedMonoBinds -- Method binding (just one)
- -> TcM s (TcMonoBinds s, LIE s, (LIE s, TcIdOcc s))
-
-tcMethodBind clas origin inst_ty sel_id meth_bind
- = tcAddSrcLoc src_loc $
- newMethod origin (RealId sel_id) [inst_ty] `thenNF_Tc` \ meth@(_, TcId local_meth_id) ->
- tcInstSigTcType (idType local_meth_id) `thenNF_Tc` \ (tyvars', rho_ty') ->
- let
- (theta', tau') = splitRhoTy rho_ty'
- sig_info = TySigInfo bndr_name local_meth_id tyvars' theta' tau' src_loc
- in
- tcBindWithSigs [bndr_name] meth_bind [sig_info]
- nonRecursive (\_ -> NoPragmaInfo) `thenTc` \ (binds, insts, _) ->
-
- returnTc (binds, insts, meth)
+ -> InstOrigin
+ -> [TcTyVar] -- Instantiated type variables for the
+ -- enclosing class/instance decl.
+ -- They'll be signature tyvars, and we
+ -- want to check that they don't get bound
+ -> [TcType] -- Instance types
+ -> TcThetaType -- Available theta; this could be used to check
+ -- the method signature, but actually that's done by
+ -- the caller; here, it's just used for the error message
+ -> RenamedMonoBinds -- Method binding (pick the right one from in here)
+ -> [RenamedSig] -- Pramgas (just for this one)
+ -> Bool -- True <=> This method is from an instance declaration
+ -> ClassOpItem -- The method selector and default-method Id
+ -> TcM (TcMonoBinds, LIE, (LIE, TcId))
+
+tcMethodBind clas origin inst_tyvars inst_tys inst_theta
+ 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
+ doptsTc Opt_WarnMissingMethods `thenNF_Tc` \ warn ->
+ warnTc (is_inst_decl && warn)
+ (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