#include "HsVersions.h"
import HsSyn ( HsDecl(..), TyClDecl(..), Sig(..), MonoBinds(..),
- HsExpr(..), HsLit(..), HsType(..), HsPred(..),
+ HsExpr(..), HsLit(..), HsType(..), HsPred(..),
mkSimpleMatch, andMonoBinds, andMonoBindList,
isClassDecl, isClassOpSig, isPragSig,
- fromClassDeclNameList, tyClDeclName
+ getClassDeclSysNames, tyClDeclName
)
-import BasicTypes ( NewOrData(..), TopLevelFlag(..), RecFlag(..), EP(..) )
+import BasicTypes ( TopLevelFlag(..), RecFlag(..) )
import RnHsSyn ( RenamedTyClDecl,
RenamedClassOpSig, RenamedMonoBinds,
RenamedContext, RenamedHsDecl, RenamedSig,
- RenamedHsExpr, maybeGenericMatch
+ maybeGenericMatch
)
import TcHsSyn ( TcMonoBinds, idsToMonoBinds )
-import Inst ( InstOrigin(..), LIE, emptyLIE, plusLIE, plusLIEs, newDicts, newMethod )
-import TcEnv ( TcId, ValueEnv, TyThing(..), TyThingDetails(..), tcAddImportedIdInfo,
+import Inst ( InstOrigin(..), LIE, emptyLIE, plusLIE, plusLIEs,
+ newDicts, newMethod )
+import TcEnv ( TcId, TcEnv, RecTcEnv, TyThingDetails(..), tcAddImportedIdInfo,
tcLookupClass, tcExtendTyVarEnvForMeths, tcExtendGlobalTyVars,
tcExtendLocalValEnv, tcExtendTyVarEnv, newDefaultMethodName
)
import TcBinds ( tcBindWithSigs, tcSpecSigs )
-import TcMonoType ( tcHsSigType, tcClassContext, checkSigTyVars, sigCtxt, mkTcSig )
+import TcMonoType ( tcHsSigType, tcClassContext, checkSigTyVars, checkAmbiguity, sigCtxt, mkTcSig )
import TcSimplify ( tcSimplifyAndCheck, bindInstsOfLocalFuns )
import TcType ( TcType, TcTyVar, tcInstTyVars, zonkTcSigTyVars )
import TcMonad
import Generics ( mkGenericRhs, validGenericMethodType )
import PrelInfo ( nO_METHOD_BINDING_ERROR_ID )
-import Class ( classTyVars, classBigSig, classSelIds, classTyCon, Class, ClassOpItem,
- DefMeth (..) )
-import Bag ( bagToList )
-import CmdLineOpts ( opt_GlasgowExts, opt_WarnMissingMethods, opt_PprStyle_Debug )
+import Class ( classTyVars, classBigSig, classSelIds, classTyCon, classTvsFds,
+ Class, ClassOpItem, DefMeth (..), FunDep )
import MkId ( mkDictSelId, mkDataConId, mkDataConWrapId, mkDefaultMethodId )
import DataCon ( mkDataCon, notMarkedStrict )
import Id ( Id, idType, idName )
-import Name ( Name, nameOccName, isLocallyDefined, NamedThing(..), mkSysLocalName,
- NameEnv, lookupNameEnv, emptyNameEnv, unitNameEnv, plusNameEnv, nameEnvElts )
-import NameSet ( NameSet, mkNameSet, elemNameSet, emptyNameSet )
+import Module ( Module )
+import Name ( Name, NamedThing(..), isFrom )
+import Name ( NameEnv, lookupNameEnv, emptyNameEnv, unitNameEnv, plusNameEnv, nameEnvElts )
+import NameSet ( emptyNameSet )
import Outputable
-import Type ( Type, ClassContext, mkTyVarTys, mkDictTys, mkSigmaTy, mkClassPred,
+import Type ( Type, ClassContext, mkTyVarTys, mkDictTys, mkClassPred,
splitTyConApp_maybe, isTyVarTy
)
import Var ( TyVar )
import VarSet ( mkVarSet, emptyVarSet )
+import CmdLineOpts
import ErrUtils ( dumpIfSet )
import Util ( count )
import Maybes ( seqMaybe, maybeToBool, orElse )
%************************************************************************
\begin{code}
-tcClassDecl1 :: ValueEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
+tcClassDecl1 :: RecTcEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
tcClassDecl1 rec_env
(ClassDecl context class_name
- tyvar_names fundeps class_sigs def_methods pragmas
+ tyvar_names fundeps class_sigs def_methods
sys_names src_loc)
= -- CHECK ARITY 1 FOR HASKELL 1.4
- checkTc (opt_GlasgowExts || length tyvar_names == 1)
+ doptsTc Opt_GlasgowExts `thenTc` \ glaExts ->
+ checkTc (glaExts || length tyvar_names == 1)
(classArityErr class_name) `thenTc_`
-- LOOK THINGS UP IN THE ENVIRONMENT
tcLookupClass class_name `thenTc` \ clas ->
let
- tyvars = classTyVars clas
+ (tyvars, fds) = classTvsFds clas
op_sigs = filter isClassOpSig class_sigs
op_names = [n | ClassOpSig n _ _ _ <- op_sigs]
- (_, datacon_name, datacon_wkr_name, sc_sel_names) = fromClassDeclNameList sys_names
+ (_, datacon_name, datacon_wkr_name, sc_sel_names) = getClassDeclSysNames sys_names
in
tcExtendTyVarEnv tyvars $
tcSuperClasses clas context sc_sel_names `thenTc` \ (sc_theta, sc_sel_ids) ->
-- CHECK THE CLASS SIGNATURES,
- mapTc (tcClassSig rec_env clas tyvars dm_info) op_sigs `thenTc` \ sig_stuff ->
+ mapTc (tcClassSig rec_env clas tyvars fds dm_info)
+ op_sigs `thenTc` \ sig_stuff ->
-- MAKE THE CLASS DETAILS
let
-- only the type variable of the class decl.
-- For std Haskell check that the context constrains only tyvars
- (if opt_GlasgowExts then
+ doptsTc Opt_GlasgowExts `thenTc` \ glaExts ->
+ (if glaExts then
returnTc ()
else
mapTc_ check_constraint context
- ) `thenTc_`
+ ) `thenTc_`
-- Context is already kind-checked
tcClassContext context `thenTc` \ sc_theta ->
is_tyvar other = False
-tcClassSig :: ValueEnv -- Knot tying only!
+tcClassSig :: RecTcEnv
-> Class -- ...ditto...
-> [TyVar] -- The class type variable, used for error check only
+ -> [FunDep TyVar]
-> NameEnv (DefMeth Name) -- Info about default methods
-> RenamedClassOpSig
-> TcM (Type, -- Type of the method
-- so we distinguish them in checkDefaultBinds, and pass this knowledge in the
-- Class.DefMeth data structure.
-tcClassSig rec_env clas clas_tyvars dm_info
+tcClassSig unf_env clas clas_tyvars fds dm_info
(ClassOpSig op_name maybe_dm op_ty src_loc)
= tcAddSrcLoc src_loc $
-- Check the type signature. NB that the envt *already has*
-- bindings for the type variables; see comments in TcTyAndClassDcls.
- -- NB: Renamer checks that the class type variable is mentioned in local_ty,
- -- and that it is not constrained by theta
tcHsSigType op_ty `thenTc` \ local_ty ->
let
- global_ty = mkSigmaTy clas_tyvars
- [mkClassPred clas (mkTyVarTys clas_tyvars)]
- local_ty
+ theta = [mkClassPred clas (mkTyVarTys clas_tyvars)]
+ in
+ -- Check for ambiguous class op types
+ checkAmbiguity True clas_tyvars theta local_ty `thenTc` \ global_ty ->
+ let
-- Build the selector id and default method id
sel_id = mkDictSelId op_name clas
dm_info_id = case dm_info_name of
NoDefMeth -> NoDefMeth
GenDefMeth -> GenDefMeth
- DefMeth dm_name -> DefMeth (tcAddImportedIdInfo rec_env dm_id)
+ DefMeth dm_name -> DefMeth (tcAddImportedIdInfo unf_env dm_id)
where
dm_id = mkDefaultMethodId dm_name clas global_ty
in
and superclass dictionary.
\begin{code}
-mkImplicitClassBinds :: [Class] -> NF_TcM ([Id], TcMonoBinds)
-mkImplicitClassBinds classes
+mkImplicitClassBinds :: Module -> [Class] -> NF_TcM ([Id], TcMonoBinds)
+mkImplicitClassBinds this_mod classes
= returnNF_Tc (concat cls_ids_s, andMonoBindList binds_s)
-- The selector binds are already in the selector Id's unfoldings
-- We don't return the data constructor etc from the class,
mk_implicit clas = (sel_ids, binds)
where
sel_ids = classSelIds clas
- binds | isLocallyDefined clas = idsToMonoBinds sel_ids
- | otherwise = EmptyMonoBinds
+ binds | isFrom this_mod clas = idsToMonoBinds sel_ids
+ | otherwise = EmptyMonoBinds
\end{code}
each local class decl.
\begin{code}
-tcClassDecls2 :: [RenamedHsDecl] -> NF_TcM (LIE, TcMonoBinds)
+tcClassDecls2 :: Module -> [RenamedHsDecl] -> NF_TcM (LIE, TcMonoBinds)
-tcClassDecls2 decls
+tcClassDecls2 this_mod decls
= foldr combine
(returnNF_Tc (emptyLIE, EmptyMonoBinds))
[tcClassDecl2 cls_decl | TyClD cls_decl <- decls,
isClassDecl cls_decl,
- isLocallyDefined (tyClDeclName cls_decl)]
+ isFrom this_mod (tyClDeclName cls_decl)]
where
combine tc1 tc2 = tc1 `thenNF_Tc` \ (lie1, binds1) ->
tc2 `thenNF_Tc` \ (lie2, binds2) ->
-> NF_TcM (LIE, TcMonoBinds)
tcClassDecl2 (ClassDecl context class_name
- tyvar_names _ sigs default_binds pragmas _ src_loc)
+ tyvar_names _ sigs default_binds _ src_loc)
= -- A locally defined class
recoverNF_Tc (returnNF_Tc (emptyLIE, EmptyMonoBinds)) $
tcAddSrcLoc src_loc $
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)
+ doptsTc Opt_WarnMissingMethods `thenNF_Tc` \ warn ->
+ warnTc (is_inst_decl && warn)
(omittedMethodWarn sel_id clas) `thenNF_Tc_`
returnTc error_rhs
where
-- (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