[project @ 2003-02-21 12:16:44 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcInstDcls.lhs
index 866741e..d312cee 100644 (file)
@@ -545,9 +545,6 @@ tcInstDecl2 (InstInfo { iDFunId = dfun_id, iBinds = binds })
 
        -- Create the result bindings
     let
-       local_dfun_id = setIdLocalExported dfun_id
-               -- Reason for setIdLocalExported: see notes with MkId.mkDictFunId
-
         dict_constr   = classDataCon clas
        scs_and_meths = map instToId sc_dicts ++ meth_ids
        this_dict_id  = instToId this_dict
@@ -593,7 +590,7 @@ tcInstDecl2 (InstInfo { iDFunId = dfun_id, iBinds = binds })
        main_bind = AbsBinds
                         zonked_inst_tyvars
                         (map instToId dfun_arg_dicts)
-                        [(inst_tyvars', local_dfun_id, this_dict_id)] 
+                        [(inst_tyvars', dfun_id, this_dict_id)] 
                         inlines all_binds
     in
     showLIE "instance"                 `thenM_`
@@ -613,7 +610,7 @@ tcMethods clas inst_tyvars inst_tyvars' dfun_theta' inst_tys'
     let
        mk_method_bind = mkMethodBind InstanceDeclOrigin clas inst_tys' monobinds
     in
-    mapAndUnzipM mk_method_bind  op_items      `thenM` \ (meth_insts, meth_infos) ->
+    mapAndUnzipM mk_method_bind op_items       `thenM` \ (meth_insts, meth_infos) ->
 
        -- And type check them
        -- It's really worth making meth_insts available to the tcMethodBind
@@ -632,14 +629,27 @@ tcMethods clas inst_tyvars inst_tyvars' dfun_theta' inst_tys'
        --
        -- Solution: make meth_insts available, so that 'then' refers directly
        --           to the local 'bind' rather than going via the dictionary.
+       --
+       -- BUT WATCH OUT!  If the method type mentions the class variable, then
+       -- this optimisation is not right.  Consider
+       --      class C a where
+       --        op :: Eq a => a
+       --
+       --      instance C Int where
+       --        op = op
+       -- The occurrence of 'op' on the rhs gives rise to a constraint
+       --      op at Int
+       -- The trouble is that the 'meth_inst' for op, which is 'available', also
+       -- looks like 'op at Int'.  But they are not the same.
     let
-       all_insts      = avail_insts ++ meth_insts
+       all_insts      = avail_insts ++ catMaybes meth_insts
        xtve           = inst_tyvars `zip` inst_tyvars'
        tc_method_bind = tcMethodBind xtve inst_tyvars' dfun_theta' all_insts uprags 
     in
     mapM tc_method_bind meth_infos             `thenM` \ meth_binds_s ->
    
-    returnM (map instToId meth_insts, andMonoBindList meth_binds_s)
+    returnM ([meth_id | (_,meth_id,_) <- meth_infos], 
+            andMonoBindList meth_binds_s)
 
 
 -- Derived newtype instances