-tcDefMeth :: Class -> [TyVar] -> Inst -> LHsBinds Name
- -> TcSigFun -> TcPragFun -> Id
- -> TcM (Id, LHsBind Id)
-tcDefMeth clas tyvars this_dict binds_in sig_fn prag_fn sel_id
- = do { let sel_name = idName sel_id
- ; dm_name <- lookupTopBndrRn (mkDefMethRdrName sel_name)
- ; local_dm_name <- newLocalName sel_name
- -- Base the local_dm_name on the selector name, becuase
- -- type errors from tcInstanceMethodBody come from here
-
- -- See Note [Silly default-method bind]
- -- (possibly out of date)
-
- ; let meth_bind = findMethodBind sel_name local_dm_name binds_in
- `orElse` pprPanic "tcDefMeth" (ppr sel_id)
- -- We only call tcDefMeth on selectors for which
- -- there is a binding in binds_in
-
- dm_sig_fn _ = sig_fn sel_name
- dm_ty = idType sel_id
- dm_id = mkDefaultMethodId dm_name dm_ty
- local_dm_type = instantiateMethod clas sel_id (mkTyVarTys tyvars)
- local_dm_id = mkLocalId local_dm_name local_dm_type
-
- ; (dm_id_w_inline, spec_prags)
- <- tcPrags NonRecursive False True dm_id (prag_fn sel_name)
-
- ; warnTc (not (null spec_prags))
- (ptext (sLit "Ignoring SPECIALISE pragmas on default method")
- <+> quotes (ppr sel_name))
-
- ; tcInstanceMethodBody (instLoc this_dict)
- tyvars [this_dict]
- ([], emptyBag)
- dm_id_w_inline local_dm_id
- dm_sig_fn IsDefaultMethod meth_bind }
+tcDefMeth :: Class -> [TyVar] -> EvVar -> LHsBinds Name -> [LSig Name]
+ -> SigFun -> PragFun -> ClassOpItem
+ -> TcM (LHsBinds TcId)
+-- Generate code for polymorphic default methods only (hence DefMeth)
+-- (Generic default methods have turned into instance decls by now.)
+-- This is incompatible with Hugs, which expects a polymorphic
+-- default method for every class op, regardless of whether or not
+-- the programmer supplied an explicit default decl for the class.
+-- (If necessary we can fix that, but we don't have a convenient Id to hand.)
+tcDefMeth clas tyvars this_dict binds_in sigs sig_fn prag_fn (sel_id, dm_info)
+ | NoDefMeth <- dm_info = return emptyBag
+ | otherwise
+ = do { (dm_id, tvs, sig_loc) <- tc_dm_id dm_info
+ ; let L loc meth_bind = findMethodBind sel_name binds_in
+ `orElse` pprPanic "tcDefMeth" (ppr sel_id)
+ dm_bind = L loc (meth_bind { fun_id = L loc (idName dm_id) })
+ -- Substitute the meth_name for the binder
+ -- NB: the binding is always a FunBind
+
+ dm_sig_fn _ = Just (clas_tv_names ++ tvs, sig_loc)
+ dm_prag_fn _ = prag_fn sel_name
+
+ ; (binds,_) <- tcExtendIdEnv [dm_id] $
+ tcPolyBinds TopLevel dm_sig_fn dm_prag_fn
+ NonRecursive NonRecursive
+ [dm_bind]
+ ; return binds }
+ where
+ sel_name = idName sel_id
+ clas_tv_names = map getName tyvars
+
+ -- Find the 'generic op :: ty' signature among the sigs
+ -- If dm_info is GenDefMeth, the corresponding signature
+ -- should jolly well exist! Hence the panic
+ genop_lhs_ty = case [lty | L _ (GenericSig (L _ n) lty) <- sigs
+ , n == sel_name ] of
+ [lty] -> lty
+ _ -> pprPanic "tcDefMeth" (ppr sel_name $$ ppr sigs)
+
+ tc_dm_id :: DefMeth -> TcM (Id, [Name], SrcSpan)
+ -- Make a default-method Id of the appropriate type
+ -- That may entail getting the generic-default signature
+ -- from the type signatures.
+ -- Also return the in-scope tyvars for the default method, and their binding site
+ tc_dm_id NoDefMeth = panic "tc_dm_id"
+ tc_dm_id (DefMeth dm_name)
+ | Just (tvs, loc) <- sig_fn sel_name
+ = return (mkDefaultMethodId sel_id dm_name, tvs, loc)
+ | otherwise
+ = pprPanic "No sig for" (ppr sel_name)
+ tc_dm_id (GenDefMeth dm_name)
+ = setSrcSpan loc $
+ do { tau <- tcHsKindedType genop_lhs_ty
+ ; checkValidType (FunSigCtxt sel_name) tau
+ ; return ( mkExportedLocalId dm_name (mkForAllTys tyvars tau)
+ , hsExplicitTvs genop_lhs_ty, loc ) }
+ where
+ loc = getLoc genop_lhs_ty