import HsTypes ( getTyVarName, pprClassAssertion, cmpHsTypes )
import RdrName ( RdrName, isRdrDataCon, rdrNameOcc, isRdrTyVar )
import RdrHsSyn ( RdrNameContext, RdrNameHsType, RdrNameConDecl,
- extractHsTyRdrNames, extractRuleBndrsTyVars
+ extractRuleBndrsTyVars, extractHsTyRdrTyVars
)
import RnHsSyn
import HsCore
bindIO_NAME
)
import Bag ( bagToList )
-import List ( partition )
+import List ( partition, nub )
import Outputable
import SrcLoc ( SrcLoc )
import CmdLineOpts ( opt_WarnUnusedMatches ) -- Warn of unused for-all'd tyvars
import Util
\end{code}
-rnDecl `renames' declarations.
+@rnDecl@ `renames' declarations.
It simultaneously performs dependency analysis and precedence parsing.
It also does the following error checks:
\begin{enumerate}
\item
Checks that all variable occurences are defined.
\item
-Checks the (..) etc constraints in the export list.
+Checks the @(..)@ etc constraints in the export list.
\end{enumerate}
Renaming type variables is a pain. Because they now contain uniques,
it is necessary to pass in an association list which maps a parsed
-tyvar to its Name representation. In some cases (type signatures of
-values), it is even necessary to go over the type first in order to
-get the set of tyvars used by it, make an assoc list, and then go over
-it again to rename the tyvars! However, we can also do some scoping
-checks at the same time.
+tyvar to its @Name@ representation.
+In some cases (type signatures of values),
+it is even necessary to go over the type first
+in order to get the set of tyvars used by it, make an assoc list,
+and then go over it again to rename the tyvars!
+However, we can also do some scoping checks at the same time.
\begin{code}
rnDecl (TyClD (TyData new_or_data context tycon tyvars condecls derivings pragmas src_loc))
= pushSrcLocRn src_loc $
- lookupBndrRn tycon `thenRn` \ tycon' ->
- bindTyVarsFVRn data_doc tyvars $ \ tyvars' ->
- rnContext data_doc context `thenRn` \ (context', cxt_fvs) ->
- checkDupOrQualNames data_doc con_names `thenRn_`
- mapFvRn rnConDecl condecls `thenRn` \ (condecls', con_fvs) ->
- rnDerivs derivings `thenRn` \ (derivings', deriv_fvs) ->
+ lookupBndrRn tycon `thenRn` \ tycon' ->
+ bindTyVarsFVRn data_doc tyvars $ \ tyvars' ->
+ rnContext data_doc context `thenRn` \ (context', cxt_fvs) ->
+ checkDupOrQualNames data_doc con_names `thenRn_`
+ mapFvRn rnConDecl condecls `thenRn` \ (condecls', con_fvs) ->
+ rnDerivs derivings `thenRn` \ (derivings', deriv_fvs) ->
ASSERT(isNoDataPragmas pragmas)
- returnRn (TyClD (TyData new_or_data context' tycon' tyvars' condecls' derivings' noDataPragmas src_loc),
+ returnRn (TyClD (TyData new_or_data context' tycon' tyvars' condecls'
+ derivings' noDataPragmas src_loc),
cxt_fvs `plusFV` con_fvs `plusFV` deriv_fvs)
where
data_doc = text "the data type declaration for" <+> quotes (ppr tycon)
where
syn_doc = text "the declaration for type synonym" <+> quotes (ppr name)
-rnDecl (TyClD (ClassDecl context cname tyvars sigs mbinds pragmas tname dname snames src_loc))
+rnDecl (TyClD (ClassDecl context cname tyvars sigs mbinds pragmas
+ tname dname snames src_loc))
= pushSrcLocRn src_loc $
lookupBndrRn cname `thenRn` \ cname' ->
mapRn mkImportedGlobalFromRdrName snames `thenRn` \ snames' ->
-- Tyvars scope over bindings and context
- bindTyVarsFV2Rn cls_doc tyvars ( \ clas_tyvar_names tyvars' ->
+ bindTyVarsFV2Rn cls_doc tyvars ( \ clas_tyvar_names tyvars' ->
-- Check the superclasses
- rnContext cls_doc context `thenRn` \ (context', cxt_fvs) ->
+ rnContext cls_doc context `thenRn` \ (context', cxt_fvs) ->
-- Check the signatures
let
(fix_sigs, non_sigs) = partition isFixitySig non_op_sigs
in
checkDupOrQualNames sig_doc sig_rdr_names_w_locs `thenRn_`
- mapFvRn (rn_op cname' clas_tyvar_names) op_sigs `thenRn` \ (sigs', sig_fvs) ->
+ mapFvRn (rn_op cname' clas_tyvar_names) op_sigs
+ `thenRn` \ (sigs', sig_fvs) ->
mapRn_ (unknownSigErr) non_sigs `thenRn_`
let
binders = mkNameSet [ nm | (ClassOpSig nm _ _ _) <- sigs' ]
in
- renameSigs False binders lookupOccRn fix_sigs `thenRn` \ (fixs', fix_fvs) ->
+ renameSigs False binders lookupOccRn fix_sigs
+ `thenRn` \ (fixs', fix_fvs) ->
-- Check the methods
checkDupOrQualNames meth_doc meth_rdr_names_w_locs `thenRn_`
- rnMethodBinds mbinds `thenRn` \ (mbinds', meth_fvs) ->
+ rnMethodBinds mbinds
+ `thenRn` \ (mbinds', meth_fvs) ->
-- Typechecker is responsible for checking that we only
-- give default-method bindings for things in this class.
-- for instance decls.
ASSERT(isNoClassPragmas pragmas)
- returnRn (TyClD (ClassDecl context' cname' tyvars' (fixs' ++ sigs')
- mbinds' NoClassPragmas tname' dname' snames' src_loc),
+ returnRn (TyClD (ClassDecl context' cname' tyvars' (fixs' ++ sigs') mbinds'
+ NoClassPragmas tname' dname' snames' src_loc),
sig_fvs `plusFV`
fix_fvs `plusFV`
cxt_fvs `plusFV`
-- Check the signature
rnHsSigType (quotes (ppr op)) ty `thenRn` \ (new_ty, op_ty_fvs) ->
let
- check_in_op_ty clas_tyvar = checkRn (clas_tyvar `elemNameSet` op_ty_fvs)
- (classTyVarNotInOpTyErr clas_tyvar sig)
+ check_in_op_ty clas_tyvar =
+ checkRn (clas_tyvar `elemNameSet` op_ty_fvs)
+ (classTyVarNotInOpTyErr clas_tyvar sig)
in
mapRn_ check_in_op_ty clas_tyvars `thenRn_`
(case (mode, maybe_dm) of
(SourceMode, _)
| op `elem` meth_rdr_names
- -> -- Source class decl with an explicit method decl
- newImplicitBinder (mkDefaultMethodOcc (rdrNameOcc op)) locn `thenRn` \ dm_name ->
- returnRn (Just dm_name, emptyFVs)
+ -> -- Source class decl with an explicit method decl
+ newImplicitBinder (mkDefaultMethodOcc (rdrNameOcc op)) locn
+ `thenRn` \ dm_name ->
+ returnRn (Just dm_name, emptyFVs)
| otherwise
-> -- Source class dec, no explicit method decl
(InterfaceMode, Just dm_rdr_name)
-> -- Imported class that has a default method decl
-- See comments with tname, snames, above
- lookupImplicitOccRn dm_rdr_name `thenRn` \ dm_name ->
+ lookupImplicitOccRn dm_rdr_name `thenRn` \ dm_name ->
returnRn (Just dm_name, unitFV dm_name)
-- An imported class decl mentions, rather than defines,
-- the default method, so we must arrange to pull it in
\begin{code}
rnDecl (InstD (InstDecl inst_ty mbinds uprags dfun_rdr_name src_loc))
= pushSrcLocRn src_loc $
- rnHsSigType (text "an instance decl") inst_ty `thenRn` \ (inst_ty', inst_fvs) ->
+ rnHsSigType (text "an instance decl") inst_ty `thenRn` \ (inst_ty', inst_fvs) ->
let
inst_tyvars = case inst_ty' of
HsForAllTy (Just inst_tyvars) _ _ -> inst_tyvars
getModeRn `thenRn` \ mode ->
(case mode of
- InterfaceMode -> lookupImplicitOccRn dfun_rdr_name `thenRn` \ dfun_name ->
+ InterfaceMode -> lookupImplicitOccRn dfun_rdr_name `thenRn` \ dfun_name ->
returnRn (dfun_name, unitFV dfun_name)
- SourceMode -> newDFunName (getDFunKey inst_ty') src_loc `thenRn` \ dfun_name ->
+ SourceMode -> newDFunName (getDFunKey inst_ty') src_loc
+ `thenRn` \ dfun_name ->
returnRn (dfun_name, emptyFVs)
- ) `thenRn` \ (dfun_name, dfun_fv) ->
+ )
+ `thenRn` \ (dfun_name, dfun_fv) ->
- -- The typechecker checks that all the bindings are for the right class.
+ -- The typechecker checks that all the bindings are for the right class.
returnRn (InstD (InstDecl inst_ty' mbinds' new_uprags dfun_name src_loc),
inst_fvs `plusFV` meth_fvs `plusFV` prag_fvs `plusFV` dfun_fv)
where
= rnHsType (text "the type signature for" <+> doc_str) ty
rnForAll doc forall_tyvars ctxt ty
- = bindTyVarsFVRn doc forall_tyvars $ \ new_tyvars ->
- rnContext doc ctxt `thenRn` \ (new_ctxt, cxt_fvs) ->
- rnHsType doc ty `thenRn` \ (new_ty, ty_fvs) ->
+ = bindTyVarsFVRn doc forall_tyvars $ \ new_tyvars ->
+ rnContext doc ctxt `thenRn` \ (new_ctxt, cxt_fvs) ->
+ rnHsType doc ty `thenRn` \ (new_ty, ty_fvs) ->
returnRn (mkHsForAllTy new_tyvars new_ctxt new_ty,
cxt_fvs `plusFV` ty_fvs)
where
check ct@(_,tys)
| forall_mentioned = returnRn (Just ct)
- | otherwise = addErrRn (ctxtErr explicit_forall doc forall_tyvars ct ty) `thenRn_`
- returnRn Nothing
+ | otherwise = addErrRn (ctxtErr explicit_forall doc forall_tyvars ct ty)
+ `thenRn_` returnRn Nothing
where
- forall_mentioned = foldr ((||) . any (`elem` forall_tyvars) . extractHsTyRdrNames)
+ forall_mentioned = foldr ((||) . any (`elem` forall_tyvars) . extractHsTyRdrTyVars)
False
tys
-
rnHsType :: SDoc -> RdrNameHsType -> RnMS (RenamedHsType, FreeVars)
rnHsType doc (HsForAllTy Nothing ctxt ty)
-- over FV(T) \ {in-scope-tyvars}
= getLocalNameEnv `thenRn` \ name_env ->
let
- mentioned_tyvars = filter isRdrTyVar (extractHsTyRdrNames ty)
- forall_tyvars = filter (not . (`elemFM` name_env)) mentioned_tyvars
+ mentioned_in_tau = extractHsTyRdrTyVars ty
+ forall_tyvars = filter (not . (`elemFM` name_env)) mentioned_in_tau
in
checkConstraints False doc forall_tyvars ctxt ty `thenRn` \ ctxt' ->
rnForAll doc (map UserTyVar forall_tyvars) ctxt' ty
-rnHsType doc (HsForAllTy (Just forall_tyvars) ctxt ty)
+rnHsType doc (HsForAllTy (Just forall_tyvars) ctxt tau)
-- Explicit quantification.
-- Check that the forall'd tyvars are a subset of the
-- free tyvars in the tau-type part
-- That's only a warning... unless the tyvar is constrained by a
-- context in which case it's an error
= let
- mentioned_tyvars = filter isRdrTyVar (extractHsTyRdrNames ty)
- constrained_tyvars = [tv | (_,tys) <- ctxt,
+ mentioned_in_tau = extractHsTyRdrTyVars tau
+ mentioned_in_ctxt = nub [tv | (_,tys) <- ctxt,
ty <- tys,
- tv <- mentioned_tyvars]
- dubious_guys = filter (`notElem` mentioned_tyvars) forall_tyvar_names
- (bad_guys, warn_guys) = partition (`elem` constrained_tyvars) dubious_guys
+ tv <- extractHsTyRdrTyVars ty]
+
+ dubious_guys = filter (`notElem` mentioned_in_tau) forall_tyvar_names
+ -- dubious = explicitly quantified but not mentioned in tau type
+
+ (bad_guys, warn_guys) = partition (`elem` mentioned_in_ctxt) dubious_guys
+ -- bad = explicitly quantified and constrained, but not mentioned in tau
+ -- warn = explicitly quantified but not mentioned in ctxt or tau
+
forall_tyvar_names = map getTyVarName forall_tyvars
in
- mapRn_ (forAllErr doc ty) bad_guys `thenRn_`
- mapRn_ (forAllWarn doc ty) warn_guys `thenRn_`
- checkConstraints True doc forall_tyvar_names ctxt ty `thenRn` \ ctxt' ->
- rnForAll doc forall_tyvars ctxt' ty
+ mapRn_ (forAllErr doc tau) bad_guys `thenRn_`
+ mapRn_ (forAllWarn doc tau) warn_guys `thenRn_`
+ checkConstraints True doc forall_tyvar_names ctxt tau `thenRn` \ ctxt' ->
+ rnForAll doc forall_tyvars ctxt' tau
rnHsType doc (MonoTyVar tyvar)
= lookupOccRn tyvar `thenRn` \ tyvar' ->
%*********************************************************
-%* *
+%* *
\subsection{IdInfo}
-%* *
+%* *
%*********************************************************
\begin{code}
= lookupOccRn worker `thenRn` \ worker' ->
returnRn (HsWorker worker', unitFV worker')
-rnIdInfo (HsUnfold inline (Just expr)) = rnCoreExpr expr `thenRn` \ (expr', fvs) ->
+rnIdInfo (HsUnfold inline (Just expr)) = rnCoreExpr expr `thenRn` \ (expr', fvs) ->
returnRn (HsUnfold inline (Just expr'), fvs)
rnIdInfo (HsUnfold inline Nothing) = returnRn (HsUnfold inline Nothing, emptyFVs)
rnIdInfo (HsArity arity) = returnRn (HsArity arity, emptyFVs)
rnIdInfo (HsUpdate update) = returnRn (HsUpdate update, emptyFVs)
rnIdInfo (HsNoCafRefs) = returnRn (HsNoCafRefs, emptyFVs)
rnIdInfo (HsCprInfo cpr_info) = returnRn (HsCprInfo cpr_info, emptyFVs)
-rnIdInfo (HsSpecialise rule_body) = rnRuleBody rule_body `thenRn` \ (rule_body', fvs) ->
+rnIdInfo (HsSpecialise rule_body) = rnRuleBody rule_body
+ `thenRn` \ (rule_body', fvs) ->
returnRn (HsSpecialise rule_body', fvs)
rnRuleBody (UfRuleBody str vars args rhs)
returnRn (UfRuleBody str vars' args' rhs', fvs1 `plusFV` fvs2)
\end{code}
-UfCore expressions.
+@UfCore@ expressions.
\begin{code}
rnCoreExpr (UfType ty)
\end{code}
%*********************************************************
-%* *
+%* *
\subsection{Rule shapes}
-%* *
+%* *
%*********************************************************
Check the shape of a transformation rule LHS. Currently
-we only allow LHSs of the form (f e1 .. en), where f is
-not one of the forall'd variables.
+we only allow LHSs of the form @(f e1 .. en)@, where @f@ is
+not one of the @forall@'d variables.
\begin{code}
validRuleLhs foralls lhs
%*********************************************************
-%* *
+%* *
\subsection{Errors}
-%* *
+%* *
%*********************************************************
\begin{code}
(ptext SLIT("In") <+> doc))
ctxtErr explicit_forall doc tyvars constraint ty
- = sep [ptext SLIT("The constraint") <+> quotes (pprClassAssertion constraint) <+>
- ptext SLIT("does not mention any of"),
+ = sep [ptext SLIT("None of the type variable(s) in the constraint")
+ <+> quotes (pprClassAssertion constraint),
if explicit_forall then
- nest 4 (ptext SLIT("the universally quantified type variables") <+> braces (interpp'SP tyvars))
+ nest 4 (ptext SLIT("is universally quantified (i.e. bound by the forall)"))
else
- nest 4 (ptext SLIT("the type variables in the type") <+> quotes (ppr ty))
+ nest 4 (ptext SLIT("appears in the type") <+> quotes (ppr ty))
]
$$
(ptext SLIT("In") <+> doc)