bindTyVarsRn, extendTyVarEnvFVRn,
bindLocalFixities,
- checkDupNames, checkShadowing, mapFvRn, mapFvRnCPS,
+ checkDupRdrNames, checkDupNames, checkShadowedNames,
+ checkDupAndShadowedRdrNames,
+ mapFvRn, mapFvRnCPS,
warnUnusedMatches, warnUnusedModules, warnUnusedImports,
warnUnusedTopBinds, warnUnusedLocalBinds,
dataTcOccs, unknownNameErr,
LHsTyVarBndr, LHsType,
Fixity, hsLTyVarLocNames, replaceTyVarName )
import RdrHsSyn ( extractHsTyRdrTyVars )
-import RdrName ( RdrName, isQual, isUnqual, isOrig_maybe,
- isQual_maybe,
- mkRdrUnqual, setRdrNameSpace, rdrNameOcc,
- pprGlobalRdrEnv, lookupGRE_RdrName,
- isExact_maybe, isSrcRdrName,
- Parent(..),
- GlobalRdrElt(..), GlobalRdrEnv, lookupGlobalRdrEnv,
- isLocalGRE, extendLocalRdrEnv, elemLocalRdrEnv, lookupLocalRdrEnv,
- Provenance(..), pprNameProvenance,
- importSpecLoc, importSpecModule
- )
+import RdrName
import HscTypes ( availNames, ModIface(..), FixItem(..), lookupFixity)
import TcEnv ( tcLookupDataCon )
import TcRnMonad
import Name ( Name, nameIsLocalOrFrom, mkInternalName, isWiredInName,
- nameSrcLoc, nameOccName, nameModule, isExternalName )
+ nameSrcLoc, nameSrcSpan, nameOccName, nameModule, isExternalName )
import NameSet
import NameEnv
import UniqFM
import DataCon ( dataConFieldLabels )
-import OccName ( tcName, isDataOcc, pprNonVarNameSpace, occNameSpace,
+import OccName ( OccName, tcName, isDataOcc, pprNonVarNameSpace, occNameSpace,
reportIfUnused, occNameFS )
import Module ( Module, ModuleName )
import PrelNames ( mkUnboundName, rOOT_MAIN, iNTERACTIVE, consDataConKey, hasKey )
import Maybes
import ListSetOps ( removeDups )
import List ( nubBy )
-import Monad ( when )
import DynFlags
import FastString
+import Control.Monad
+\end{code}
+
+\begin{code}
+-- XXX
+thenM :: Monad a => a b -> (b -> a c) -> a c
+thenM = (>>=)
+
+thenM_ :: Monad a => a b -> a c -> a c
+thenM_ = (>>)
+
+returnM :: Monad m => a -> m a
+returnM = return
+
+mappM :: (Monad m) => (a -> m b) -> [a] -> m [b]
+mappM = mapM
+
+mappM_ :: (Monad m) => (a -> m b) -> [a] -> m ()
+mappM_ = mapM_
+
+checkM :: Monad m => Bool -> m () -> m ()
+checkM = unless
\end{code}
%*********************************************************
getLookupOccRn :: RnM (Name -> Maybe Name)
getLookupOccRn
= getLocalRdrEnv `thenM` \ local_env ->
- return (lookupLocalRdrEnv local_env . mkRdrUnqual . nameOccName)
+ return (lookupLocalRdrOcc local_env . nameOccName)
lookupLocatedOccRn :: Located RdrName -> RnM (Located Name)
lookupLocatedOccRn = wrapLocM lookupOccRn
Just new_v -> returnM (Left (new_v, (FixItem (rdrNameOcc v) fix)))
Nothing -> returnM (Right (occNameFS $ rdrNameOcc v, (L loc fix)))
- nowAndLater (ls :: [Either (Name, FixItem) (FastString, Located Fixity)]) =
+ nowAndLater :: [Either (Name, FixItem) (FastString, Located Fixity)]
+ -> ([(Name,FixItem)], UniqFM (Located Fixity))
+ nowAndLater ls =
foldr (\ cur -> \ (now, later) ->
case cur of
Left (n, f) -> ((n, f) : now, later)
-- lookupRdrEnv doesn't even attempt to look up a qualified RdrName
mkInternalName uniq (rdrNameOcc rdr_name) loc
+---------------------
+checkDupAndShadowedRdrNames :: SDoc -> [Located RdrName] -> RnM ()
+checkDupAndShadowedRdrNames doc loc_rdr_names
+ = do { checkDupRdrNames doc loc_rdr_names
+ ; envs <- getRdrEnvs
+ ; checkShadowedNames doc envs
+ [(loc,rdrNameOcc rdr) | L loc rdr <- loc_rdr_names] }
+
+---------------------
bindLocatedLocalsRn :: SDoc -- Documentation string for error message
-> [Located RdrName]
-> ([Name] -> RnM a)
-> RnM a
bindLocatedLocalsRn doc_str rdr_names_w_loc enclosed_scope
- = -- Check for duplicate names
- checkDupNames doc_str rdr_names_w_loc `thenM_`
-
- -- Warn about shadowing
- checkShadowing doc_str rdr_names_w_loc `thenM_`
+ = checkDupAndShadowedRdrNames doc_str rdr_names_w_loc `thenM_`
-- Make fresh Names and extend the environment
newLocalsRn rdr_names_w_loc `thenM` \names ->
extendTyVarEnvFVRn tyvars thing_inside = bindLocalNamesFV tyvars thing_inside
-------------------------------------
+checkDupRdrNames :: SDoc
+ -> [Located RdrName]
+ -> RnM ()
+checkDupRdrNames doc_str rdr_names_w_loc
+ = -- Check for duplicated names in a binding group
+ mappM_ (dupNamesErr getLoc doc_str) dups
+ where
+ (_, dups) = removeDups (\n1 n2 -> unLoc n1 `compare` unLoc n2) rdr_names_w_loc
+
checkDupNames :: SDoc
- -> [Located RdrName]
+ -> [Name]
-> RnM ()
-checkDupNames doc_str rdr_names_w_loc
+checkDupNames doc_str names
= -- Check for duplicated names in a binding group
- mappM_ (dupNamesErr doc_str) dups
+ mappM_ (dupNamesErr nameSrcSpan doc_str) dups
where
- (_, dups) = removeDups (\n1 n2 -> unLoc n1 `compare` unLoc n2) rdr_names_w_loc
+ (_, dups) = removeDups (\n1 n2 -> nameOccName n1 `compare` nameOccName n2) names
-------------------------------------
-checkShadowing doc_str loc_rdr_names
- = traceRn (text "shadow" <+> ppr loc_rdr_names) `thenM_`
- getLocalRdrEnv `thenM` \ local_env ->
- getGlobalRdrEnv `thenM` \ global_env ->
- let
- check_shadow (L loc rdr_name)
- | Just n <- mb_local = complain [ptext SLIT("bound at") <+> ppr (nameSrcLoc n)]
+checkShadowedNames :: SDoc -> (GlobalRdrEnv, LocalRdrEnv) -> [(SrcSpan,OccName)] -> RnM ()
+checkShadowedNames doc_str (global_env,local_env) loc_rdr_names
+ = ifOptM Opt_WarnNameShadowing $
+ do { traceRn (text "shadow" <+> ppr loc_rdr_names)
+ ; mappM_ check_shadow loc_rdr_names }
+ where
+ check_shadow (loc, occ)
+ | Just n <- mb_local = complain [ptext SLIT("bound at") <+> ppr loc]
| not (null gres) = complain (map pprNameProvenance gres)
| otherwise = return ()
where
- complain pp_locs = addWarnAt loc (shadowedNameWarn doc_str rdr_name pp_locs)
- mb_local = lookupLocalRdrEnv local_env rdr_name
- gres = lookupGRE_RdrName rdr_name global_env
- in
- ifOptM Opt_WarnNameShadowing (mappM_ check_shadow loc_rdr_names)
+ complain pp_locs = addWarnAt loc (shadowedNameWarn doc_str occ pp_locs)
+ mb_local = lookupLocalRdrOcc local_env occ
+ gres = lookupGRE_RdrName (mkRdrUnqual occ) global_env
+ -- Make an Unqualified RdrName and look that up, so that
+ -- we don't find any GREs that are in scope qualified-only
\end{code}
msgs = [ptext SLIT(" or") <+> mk_ref np | np <- nps]
mk_ref gre = quotes (ppr (gre_name gre)) <> comma <+> pprNameProvenance gre
-shadowedNameWarn doc rdr_name shadowed_locs
- = sep [ptext SLIT("This binding for") <+> quotes (ppr rdr_name)
+shadowedNameWarn doc occ shadowed_locs
+ = sep [ptext SLIT("This binding for") <+> quotes (ppr occ)
<+> ptext SLIT("shadows the existing binding") <> plural shadowed_locs,
nest 2 (vcat shadowed_locs)]
$$ doc
= ptext SLIT("Illegal binding of built-in syntax:") <+> ppr (rdrNameOcc name)
-- The rdrNameOcc is because we don't want to print Prelude.(,)
-dupNamesErr :: SDoc -> [Located RdrName] -> RnM ()
-dupNamesErr descriptor located_names
+dupNamesErr :: Outputable n => (n -> SrcSpan) -> SDoc -> [n] -> RnM ()
+dupNamesErr get_loc descriptor names
= addErrAt big_loc $
- vcat [ptext SLIT("Conflicting definitions for") <+> quotes (ppr name1),
+ vcat [ptext SLIT("Conflicting definitions for") <+> quotes (ppr (head names)),
locations, descriptor]
where
- L _ name1 = head located_names
- locs = map getLoc located_names
+ locs = map get_loc names
big_loc = foldr1 combineSrcSpans locs
one_line = isOneLineSpan big_loc
locations | one_line = empty