2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnEnv]{Environment manipulation for the renamer monad}
9 lookupLocatedBndrRn, lookupBndrRn,
10 lookupLocatedTopBndrRn, lookupTopBndrRn,
11 lookupLocatedOccRn, lookupOccRn,
12 lookupLocatedGlobalOccRn, lookupGlobalOccRn,
13 lookupTopFixSigNames, lookupSrcOcc_maybe,
14 lookupFixityRn, lookupLocatedSigOccRn,
15 lookupLocatedInstDeclBndr,
16 lookupSyntaxName, lookupSyntaxNames, lookupImportedName,
18 newLocalsRn, newIPNameRn,
19 bindLocalNames, bindLocalNamesFV,
20 bindLocatedLocalsFV, bindLocatedLocalsRn,
21 bindPatSigTyVars, bindPatSigTyVarsFV,
22 bindTyVarsRn, extendTyVarEnvFVRn,
25 checkDupNames, mapFvRn,
26 warnUnusedMatches, warnUnusedModules, warnUnusedImports,
27 warnUnusedTopBinds, warnUnusedLocalBinds,
28 dataTcOccs, unknownNameErr,
31 #include "HsVersions.h"
33 import LoadIface ( loadSrcInterface )
34 import IfaceEnv ( lookupOrig, newGlobalBinder, newIPName )
36 import RdrHsSyn ( extractHsTyRdrTyVars )
37 import RdrName ( RdrName, rdrNameModule, rdrNameOcc, isQual, isUnqual, isOrig,
38 mkRdrUnqual, setRdrNameSpace, rdrNameOcc,
39 pprGlobalRdrEnv, lookupGRE_RdrName,
40 isExact_maybe, isSrcRdrName,
41 GlobalRdrElt(..), GlobalRdrEnv, lookupGlobalRdrEnv,
42 isLocalGRE, extendLocalRdrEnv, elemLocalRdrEnv, lookupLocalRdrEnv,
43 Provenance(..), pprNameProvenance, ImportSpec(..)
45 import HsTypes ( hsTyVarName, replaceTyVarName )
46 import HscTypes ( availNames, ModIface(..), FixItem(..), lookupFixity )
48 import Name ( Name, nameIsLocalOrFrom, mkInternalName, isInternalName,
49 nameSrcLoc, nameOccName, nameModuleName, nameParent )
51 import OccName ( tcName, isDataOcc, occNameFlavour, reportIfUnused,
53 import Module ( Module, ModuleName, moduleName, mkHomeModule )
54 import PrelNames ( mkUnboundName, rOOT_MAIN_Name, iNTERACTIVE )
56 import BasicTypes ( IPName, mapIPName )
57 import SrcLoc ( SrcSpan, srcSpanStart, Located(..), eqLocated, unLoc,
60 import ListSetOps ( removeDups )
63 import FastString ( FastString )
66 %*********************************************************
70 %*********************************************************
73 newTopSrcBinder :: Module -> Maybe Name -> Located RdrName -> RnM Name
74 newTopSrcBinder this_mod mb_parent (L loc rdr_name)
75 | Just name <- isExact_maybe rdr_name
76 -- This is here to catch
77 -- (a) Exact-name binders created by Template Haskell
78 -- (b) The PrelBase defn of (say) [] and similar, for which
79 -- the parser reads the special syntax and returns an Exact RdrName
81 -- We are at a binding site for the name, so check first that it
82 -- the current module is the correct one; otherwise GHC can get
83 -- very confused indeed. This test rejects code like
84 -- data T = (,) Int Int
85 -- unless we are in GHC.Tup
86 = do checkErr (isInternalName name || this_mod_name == nameModuleName name)
87 (badOrigBinding rdr_name)
91 = do checkErr (rdr_mod_name == this_mod_name || rdr_mod_name == rOOT_MAIN_Name)
92 (badOrigBinding rdr_name)
93 -- When reading External Core we get Orig names as binders,
94 -- but they should agree with the module gotten from the monad
96 -- We can get built-in syntax showing up here too, sadly. If you type
98 -- the constructor is parsed as a type, and then RdrHsSyn.tyConToDataCon
99 -- uses setRdrNameSpace to make it into a data constructors. At that point
100 -- the nice Exact name for the TyCon gets swizzled to an Orig name.
101 -- Hence the badOrigBinding error message.
103 -- Except for the ":Main.main = ..." definition inserted into
104 -- the Main module; ugh!
106 -- Because of this latter case, we call newGlobalBinder with a module from
107 -- the RdrName, not from the environment. In principle, it'd be fine to
108 -- have an arbitrary mixture of external core definitions in a single module,
109 -- (apart from module-initialisation issues, perhaps).
110 newGlobalBinder (mkHomeModule rdr_mod_name) (rdrNameOcc rdr_name) mb_parent
111 (srcSpanStart loc) --TODO, should pass the whole span
114 = newGlobalBinder this_mod (rdrNameOcc rdr_name) mb_parent (srcSpanStart loc)
116 this_mod_name = moduleName this_mod
117 rdr_mod_name = rdrNameModule rdr_name
120 %*********************************************************
122 Source code occurrences
124 %*********************************************************
126 Looking up a name in the RnEnv.
129 lookupLocatedBndrRn :: Located RdrName -> RnM (Located Name)
130 lookupLocatedBndrRn = wrapLocM lookupBndrRn
132 lookupBndrRn :: RdrName -> RnM Name
133 -- NOTE: assumes that the SrcSpan of the binder has already been addSrcSpan'd
134 lookupBndrRn rdr_name
135 = getLocalRdrEnv `thenM` \ local_env ->
136 case lookupLocalRdrEnv local_env rdr_name of
137 Just name -> returnM name
138 Nothing -> lookupTopBndrRn rdr_name
140 lookupLocatedTopBndrRn :: Located RdrName -> RnM (Located Name)
141 lookupLocatedTopBndrRn = wrapLocM lookupTopBndrRn
143 lookupTopBndrRn :: RdrName -> RnM Name
144 -- Look up a top-level source-code binder. We may be looking up an unqualified 'f',
145 -- and there may be several imported 'f's too, which must not confuse us.
146 -- For example, this is OK:
148 -- infix 9 f -- The 'f' here does not need to be qualified
149 -- f x = x -- Nor here, of course
150 -- So we have to filter out the non-local ones.
152 -- A separate function (importsFromLocalDecls) reports duplicate top level
153 -- decls, so here it's safe just to choose an arbitrary one.
155 -- There should never be a qualified name in a binding position in Haskell,
156 -- but there can be if we have read in an external-Core file.
157 -- The Haskell parser checks for the illegal qualified name in Haskell
158 -- source files, so we don't need to do so here.
160 lookupTopBndrRn rdr_name
161 | Just name <- isExact_maybe rdr_name
165 -- This deals with the case of derived bindings, where
166 -- we don't bother to call newTopSrcBinder first
167 -- We assume there is no "parent" name
168 = do { loc <- getSrcSpanM
169 ; newGlobalBinder (mkHomeModule (rdrNameModule rdr_name))
170 (rdrNameOcc rdr_name) Nothing (srcSpanStart loc) }
173 = do { mb_gre <- lookupGreLocalRn rdr_name
175 Nothing -> unboundName rdr_name
176 Just gre -> returnM (gre_name gre) }
178 -- lookupLocatedSigOccRn is used for type signatures and pragmas
184 -- It's clear that the 'f' in the signature must refer to A.f
185 -- The Haskell98 report does not stipulate this, but it will!
186 -- So we must treat the 'f' in the signature in the same way
187 -- as the binding occurrence of 'f', using lookupBndrRn
188 lookupLocatedSigOccRn :: Located RdrName -> RnM (Located Name)
189 lookupLocatedSigOccRn = lookupLocatedBndrRn
191 -- lookupInstDeclBndr is used for the binders in an
192 -- instance declaration. Here we use the class name to
195 lookupLocatedInstDeclBndr :: Name -> Located RdrName -> RnM (Located Name)
196 lookupLocatedInstDeclBndr cls = wrapLocM (lookupInstDeclBndr cls)
198 lookupInstDeclBndr :: Name -> RdrName -> RnM Name
199 lookupInstDeclBndr cls_name rdr_name
200 | isUnqual rdr_name -- Find all the things the rdr-name maps to
201 = do { -- and pick the one with the right parent name
202 let { is_op gre = cls_name == nameParent (gre_name gre)
203 ; occ = rdrNameOcc rdr_name
204 ; lookup_fn env = filter is_op (lookupGlobalRdrEnv env occ) }
205 ; mb_gre <- lookupGreRn_help rdr_name lookup_fn
207 Just gre -> return (gre_name gre)
208 Nothing -> do { addErr (unknownInstBndrErr cls_name rdr_name)
209 ; return (mkUnboundName rdr_name) } }
211 | otherwise -- Occurs in derived instances, where we just
212 -- refer directly to the right method
213 = ASSERT2( not (isQual rdr_name), ppr rdr_name )
214 -- NB: qualified names are rejected by the parser
215 lookupImportedName rdr_name
217 newIPNameRn :: IPName RdrName -> TcRnIf m n (IPName Name)
218 newIPNameRn ip_rdr = newIPName (mapIPName rdrNameOcc ip_rdr)
220 --------------------------------------------------
222 --------------------------------------------------
224 lookupLocatedOccRn :: Located RdrName -> RnM (Located Name)
225 lookupLocatedOccRn = wrapLocM lookupOccRn
227 -- lookupOccRn looks up an occurrence of a RdrName
228 lookupOccRn :: RdrName -> RnM Name
230 = getLocalRdrEnv `thenM` \ local_env ->
231 case lookupLocalRdrEnv local_env rdr_name of
232 Just name -> returnM name
233 Nothing -> lookupGlobalOccRn rdr_name
235 lookupLocatedGlobalOccRn :: Located RdrName -> RnM (Located Name)
236 lookupLocatedGlobalOccRn = wrapLocM lookupGlobalOccRn
238 lookupGlobalOccRn :: RdrName -> RnM Name
239 -- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
240 -- environment. It's used only for
241 -- record field names
242 -- class op names in class and instance decls
244 lookupGlobalOccRn rdr_name
245 | not (isSrcRdrName rdr_name)
246 = lookupImportedName rdr_name
249 = -- First look up the name in the normal environment.
250 lookupGreRn rdr_name `thenM` \ mb_gre ->
252 Just gre -> returnM (gre_name gre) ;
255 -- We allow qualified names on the command line to refer to
256 -- *any* name exported by any module in scope, just as if
257 -- there was an "import qualified M" declaration for every
259 getModule `thenM` \ mod ->
260 if isQual rdr_name && mod == iNTERACTIVE then
261 -- This test is not expensive,
262 lookupQualifiedName rdr_name -- and only happens for failed lookups
264 unboundName rdr_name }
266 lookupImportedName :: RdrName -> TcRnIf m n Name
267 -- Lookup the occurrence of an imported name
268 -- The RdrName is *always* qualified or Exact
269 -- Treat it as an original name, and conjure up the Name
270 -- Usually it's Exact or Orig, but it can be Qual if it
271 -- comes from an hi-boot file. (This minor infelicity is
272 -- just to reduce duplication in the parser.)
273 lookupImportedName rdr_name
274 | Just n <- isExact_maybe rdr_name
275 -- This happens in derived code
278 | otherwise -- Always Orig, even when reading a .hi-boot file
279 = ASSERT( not (isUnqual rdr_name) )
280 lookupOrig (rdrNameModule rdr_name) (rdrNameOcc rdr_name)
282 unboundName :: RdrName -> RnM Name
284 = do { addErr (unknownNameErr rdr_name)
285 ; env <- getGlobalRdrEnv;
286 ; traceRn (vcat [unknownNameErr rdr_name,
287 ptext SLIT("Global envt is:"),
288 nest 3 (pprGlobalRdrEnv env)])
289 ; returnM (mkUnboundName rdr_name) }
291 --------------------------------------------------
292 -- Lookup in the Global RdrEnv of the module
293 --------------------------------------------------
295 lookupSrcOcc_maybe :: RdrName -> RnM (Maybe Name)
296 -- No filter function; does not report an error on failure
297 lookupSrcOcc_maybe rdr_name
298 = do { mb_gre <- lookupGreRn rdr_name
300 Nothing -> returnM Nothing
301 Just gre -> returnM (Just (gre_name gre)) }
303 -------------------------
304 lookupGreRn :: RdrName -> RnM (Maybe GlobalRdrElt)
305 -- Just look up the RdrName in the GlobalRdrEnv
307 = lookupGreRn_help rdr_name (lookupGRE_RdrName rdr_name)
309 lookupGreLocalRn :: RdrName -> RnM (Maybe GlobalRdrElt)
310 -- Similar, but restricted to locally-defined things
311 lookupGreLocalRn rdr_name
312 = lookupGreRn_help rdr_name lookup_fn
314 lookup_fn env = filter isLocalGRE (lookupGRE_RdrName rdr_name env)
316 lookupGreRn_help :: RdrName -- Only used in error message
317 -> (GlobalRdrEnv -> [GlobalRdrElt]) -- Lookup function
318 -> RnM (Maybe GlobalRdrElt)
319 -- Checks for exactly one match; reports deprecations
320 -- Returns Nothing, without error, if too few
321 lookupGreRn_help rdr_name lookup
322 = do { env <- getGlobalRdrEnv
324 [] -> returnM Nothing
325 [gre] -> returnM (Just gre)
326 gres -> do { addNameClashErrRn rdr_name gres
327 ; returnM (Just (head gres)) } }
329 ------------------------------
331 ------------------------------
333 -- A qualified name on the command line can refer to any module at all: we
334 -- try to load the interface if we don't already have it.
335 lookupQualifiedName :: RdrName -> RnM Name
336 lookupQualifiedName rdr_name
338 mod = rdrNameModule rdr_name
339 occ = rdrNameOcc rdr_name
341 loadSrcInterface doc mod False `thenM` \ iface ->
344 (mod,avails) <- mi_exports iface,
346 name <- availNames avail,
348 ((mod,occ):ns) -> ASSERT (null ns)
350 _ -> unboundName rdr_name
352 doc = ptext SLIT("Need to find") <+> ppr rdr_name
355 %*********************************************************
359 %*********************************************************
362 lookupTopFixSigNames :: RdrName -> RnM [Name]
363 -- GHC extension: look up both the tycon and data con
364 -- for con-like things
365 lookupTopFixSigNames rdr_name
366 | Just n <- isExact_maybe rdr_name
367 -- Special case for (:), which doesn't get into the GlobalRdrEnv
368 = return [n] -- For this we don't need to try the tycon too
370 = do { mb_gres <- mapM lookupGreLocalRn (dataTcOccs rdr_name)
371 ; return [gre_name gre | Just gre <- mb_gres] }
373 --------------------------------
374 bindLocalFixities :: [FixitySig RdrName] -> RnM a -> RnM a
375 -- Used for nested fixity decls
376 -- No need to worry about type constructors here,
377 -- Should check for duplicates but we don't
378 bindLocalFixities fixes thing_inside
379 | null fixes = thing_inside
380 | otherwise = mappM rn_sig fixes `thenM` \ new_bit ->
381 extendFixityEnv new_bit thing_inside
383 rn_sig (FixitySig lv@(L loc v) fix)
384 = addLocM lookupBndrRn lv `thenM` \ new_v ->
385 returnM (new_v, (FixItem (rdrNameOcc v) fix loc))
388 --------------------------------
389 lookupFixity is a bit strange.
391 * Nested local fixity decls are put in the local fixity env, which we
392 find with getFixtyEnv
394 * Imported fixities are found in the HIT or PIT
396 * Top-level fixity decls in this module may be for Names that are
397 either Global (constructors, class operations)
398 or Local/Exported (everything else)
399 (See notes with RnNames.getLocalDeclBinders for why we have this split.)
400 We put them all in the local fixity environment
403 lookupFixityRn :: Name -> RnM Fixity
405 = getModule `thenM` \ this_mod ->
406 if nameIsLocalOrFrom this_mod name
407 then -- It's defined in this module
408 getFixityEnv `thenM` \ local_fix_env ->
409 traceRn (text "lookupFixityRn" <+> (ppr name $$ ppr local_fix_env)) `thenM_`
410 returnM (lookupFixity local_fix_env name)
412 else -- It's imported
413 -- For imported names, we have to get their fixities by doing a
414 -- loadHomeInterface, and consulting the Ifaces that comes back
415 -- from that, because the interface file for the Name might not
416 -- have been loaded yet. Why not? Suppose you import module A,
417 -- which exports a function 'f', thus;
418 -- module CurrentModule where
420 -- module A( f ) where
422 -- Then B isn't loaded right away (after all, it's possible that
423 -- nothing from B will be used). When we come across a use of
424 -- 'f', we need to know its fixity, and it's then, and only
425 -- then, that we load B.hi. That is what's happening here.
426 loadSrcInterface doc name_mod False `thenM` \ iface ->
427 returnM (mi_fix_fn iface (nameOccName name))
429 doc = ptext SLIT("Checking fixity for") <+> ppr name
430 name_mod = nameModuleName name
432 dataTcOccs :: RdrName -> [RdrName]
433 -- If the input is a data constructor, return both it and a type
434 -- constructor. This is useful when we aren't sure which we are
437 -- ToDo: If the user typed "[]" or "(,,)", we'll generate an Exact RdrName,
438 -- and we don't have a systematic way to find the TyCon's Name from
439 -- the DataCon's name. Sigh
441 | isDataOcc occ = [rdr_name_tc, rdr_name]
442 | otherwise = [rdr_name]
444 occ = rdrNameOcc rdr_name
445 rdr_name_tc = setRdrNameSpace rdr_name tcName
448 %************************************************************************
451 Dealing with rebindable syntax is driven by the
452 Opt_NoImplicitPrelude dynamic flag.
454 In "deriving" code we don't want to use rebindable syntax
455 so we switch off the flag locally
458 %************************************************************************
460 Haskell 98 says that when you say "3" you get the "fromInteger" from the
461 Standard Prelude, regardless of what is in scope. However, to experiment
462 with having a language that is less coupled to the standard prelude, we're
463 trying a non-standard extension that instead gives you whatever "Prelude.fromInteger"
464 happens to be in scope. Then you can
466 import MyPrelude as Prelude
467 to get the desired effect.
469 At the moment this just happens for
470 * fromInteger, fromRational on literals (in expressions and patterns)
471 * negate (in expressions)
472 * minus (arising from n+k patterns)
475 We store the relevant Name in the HsSyn tree, in
476 * HsIntegral/HsFractional
480 respectively. Initially, we just store the "standard" name (PrelNames.fromIntegralName,
481 fromRationalName etc), but the renamer changes this to the appropriate user
482 name if Opt_NoImplicitPrelude is on. That is what lookupSyntaxName does.
484 We treat the orignal (standard) names as free-vars too, because the type checker
485 checks the type of the user thing against the type of the standard thing.
488 lookupSyntaxName :: Name -- The standard name
489 -> RnM (Name, FreeVars) -- Possibly a non-standard name
490 lookupSyntaxName std_name
491 = doptM Opt_NoImplicitPrelude `thenM` \ no_prelude ->
492 if not no_prelude then normal_case
494 -- Get the similarly named thing from the local environment
495 lookupOccRn (mkRdrUnqual (nameOccName std_name)) `thenM` \ usr_name ->
496 returnM (usr_name, unitFV usr_name)
498 normal_case = returnM (std_name, emptyFVs)
500 lookupSyntaxNames :: [Name] -- Standard names
501 -> RnM (ReboundNames Name, FreeVars) -- See comments with HsExpr.ReboundNames
502 lookupSyntaxNames std_names
503 = doptM Opt_NoImplicitPrelude `thenM` \ no_prelude ->
504 if not no_prelude then normal_case
506 -- Get the similarly named thing from the local environment
507 mappM (lookupOccRn . mkRdrUnqual . nameOccName) std_names `thenM` \ usr_names ->
509 returnM (std_names `zip` map HsVar usr_names, mkFVs usr_names)
511 normal_case = returnM (std_names `zip` map HsVar std_names, emptyFVs)
515 %*********************************************************
519 %*********************************************************
522 newLocalsRn :: [Located RdrName] -> RnM [Name]
523 newLocalsRn rdr_names_w_loc
524 = newUniqueSupply `thenM` \ us ->
525 returnM (zipWith mk rdr_names_w_loc (uniqsFromSupply us))
527 mk (L loc rdr_name) uniq
528 | Just name <- isExact_maybe rdr_name = name
529 -- This happens in code generated by Template Haskell
530 | otherwise = ASSERT2( isUnqual rdr_name, ppr rdr_name )
531 -- We only bind unqualified names here
532 -- lookupRdrEnv doesn't even attempt to look up a qualified RdrName
533 mkInternalName uniq (rdrNameOcc rdr_name) (srcSpanStart loc)
535 bindLocatedLocalsRn :: SDoc -- Documentation string for error message
539 bindLocatedLocalsRn doc_str rdr_names_w_loc enclosed_scope
540 = -- Check for duplicate names
541 checkDupNames doc_str rdr_names_w_loc `thenM_`
543 -- Warn about shadowing, but only in source modules
544 ifOptM Opt_WarnNameShadowing
545 (checkShadowing doc_str rdr_names_w_loc) `thenM_`
547 -- Make fresh Names and extend the environment
548 newLocalsRn rdr_names_w_loc `thenM` \ names ->
549 getLocalRdrEnv `thenM` \ local_env ->
550 setLocalRdrEnv (extendLocalRdrEnv local_env names)
551 (enclosed_scope names)
554 bindLocalNames names enclosed_scope
555 = getLocalRdrEnv `thenM` \ name_env ->
556 setLocalRdrEnv (extendLocalRdrEnv name_env names)
559 bindLocalNamesFV names enclosed_scope
560 = bindLocalNames names $
561 enclosed_scope `thenM` \ (thing, fvs) ->
562 returnM (thing, delListFromNameSet fvs names)
565 -------------------------------------
566 -- binLocalsFVRn is the same as bindLocalsRn
567 -- except that it deals with free vars
568 bindLocatedLocalsFV :: SDoc -> [Located RdrName] -> ([Name] -> RnM (a,FreeVars))
570 bindLocatedLocalsFV doc rdr_names enclosed_scope
571 = bindLocatedLocalsRn doc rdr_names $ \ names ->
572 enclosed_scope names `thenM` \ (thing, fvs) ->
573 returnM (thing, delListFromNameSet fvs names)
575 -------------------------------------
576 extendTyVarEnvFVRn :: [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
577 -- This tiresome function is used only in rnSourceDecl on InstDecl
578 extendTyVarEnvFVRn tyvars enclosed_scope
579 = bindLocalNames tyvars enclosed_scope `thenM` \ (thing, fvs) ->
580 returnM (thing, delListFromNameSet fvs tyvars)
582 bindTyVarsRn :: SDoc -> [LHsTyVarBndr RdrName]
583 -> ([LHsTyVarBndr Name] -> RnM a)
585 bindTyVarsRn doc_str tyvar_names enclosed_scope
587 located_tyvars = [L loc (hsTyVarName tv) | L loc tv <- tyvar_names]
589 bindLocatedLocalsRn doc_str located_tyvars $ \ names ->
590 enclosed_scope (zipWith replace tyvar_names names)
592 replace (L loc n1) n2 = L loc (replaceTyVarName n1 n2)
594 bindPatSigTyVars :: [LHsType RdrName] -> ([Name] -> RnM a) -> RnM a
595 -- Find the type variables in the pattern type
596 -- signatures that must be brought into scope
597 bindPatSigTyVars tys thing_inside
598 = getLocalRdrEnv `thenM` \ name_env ->
600 located_tyvars = nubBy eqLocated [ tv | ty <- tys,
601 tv <- extractHsTyRdrTyVars ty,
602 not (unLoc tv `elemLocalRdrEnv` name_env)
604 -- The 'nub' is important. For example:
605 -- f (x :: t) (y :: t) = ....
606 -- We don't want to complain about binding t twice!
608 doc_sig = text "In a pattern type-signature"
610 bindLocatedLocalsRn doc_sig located_tyvars thing_inside
612 bindPatSigTyVarsFV :: [LHsType RdrName]
615 bindPatSigTyVarsFV tys thing_inside
616 = bindPatSigTyVars tys $ \ tvs ->
617 thing_inside `thenM` \ (result,fvs) ->
618 returnM (result, fvs `delListFromNameSet` tvs)
620 -------------------------------------
621 checkDupNames :: SDoc
624 checkDupNames doc_str rdr_names_w_loc
625 = -- Check for duplicated names in a binding group
626 mappM_ (dupNamesErr doc_str) dups
628 (_, dups) = removeDups (\n1 n2 -> unLoc n1 `compare` unLoc n2) rdr_names_w_loc
630 -------------------------------------
631 checkShadowing doc_str loc_rdr_names
632 = getLocalRdrEnv `thenM` \ local_env ->
633 getGlobalRdrEnv `thenM` \ global_env ->
635 check_shadow (L loc rdr_name)
636 | rdr_name `elemLocalRdrEnv` local_env
637 || not (null (lookupGRE_RdrName rdr_name global_env ))
638 = addSrcSpan loc $ addWarn (shadowedNameWarn doc_str rdr_name)
639 | otherwise = returnM ()
641 mappM_ check_shadow loc_rdr_names
645 %************************************************************************
647 \subsection{Free variable manipulation}
649 %************************************************************************
653 mapFvRn f xs = mappM f xs `thenM` \ stuff ->
655 (ys, fvs_s) = unzip stuff
657 returnM (ys, plusFVs fvs_s)
661 %************************************************************************
663 \subsection{Envt utility functions}
665 %************************************************************************
668 warnUnusedModules :: [(ModuleName,SrcSpan)] -> RnM ()
669 warnUnusedModules mods
670 = ifOptM Opt_WarnUnusedImports (mappM_ bleat mods)
672 bleat (mod,loc) = addSrcSpan loc $ addWarn (mk_warn mod)
673 mk_warn m = vcat [ptext SLIT("Module") <+> quotes (ppr m) <+>
674 text "is imported, but nothing from it is used",
675 parens (ptext SLIT("except perhaps instances visible in") <+>
678 warnUnusedImports, warnUnusedTopBinds :: [GlobalRdrElt] -> RnM ()
679 warnUnusedImports gres = ifOptM Opt_WarnUnusedImports (warnUnusedGREs gres)
680 warnUnusedTopBinds gres = ifOptM Opt_WarnUnusedBinds (warnUnusedGREs gres)
682 warnUnusedLocalBinds, warnUnusedMatches :: [Name] -> RnM ()
683 warnUnusedLocalBinds names = ifOptM Opt_WarnUnusedBinds (warnUnusedLocals names)
684 warnUnusedMatches names = ifOptM Opt_WarnUnusedMatches (warnUnusedLocals names)
686 -------------------------
689 = warnUnusedBinds [(n,Just p) | GRE {gre_name = n, gre_prov = p} <- gres]
691 warnUnusedLocals names
692 = warnUnusedBinds [(n,Nothing) | n<-names]
694 warnUnusedBinds :: [(Name,Maybe Provenance)] -> RnM ()
695 warnUnusedBinds names = mappM_ warnUnusedName (filter reportable names)
696 where reportable (name,_) = reportIfUnused (nameOccName name)
698 -------------------------
700 warnUnusedName :: (Name, Maybe Provenance) -> RnM ()
701 warnUnusedName (name, prov)
702 = addWarnAt loc (sep [msg <> colon, nest 4 (ppr name)])
703 -- TODO should be a proper span
705 (loc,msg) = case prov of
706 Just (Imported is _) ->
707 ( is_loc (head is), imp_from (is_mod imp_spec) )
711 ( srcLocSpan (nameSrcLoc name), unused_msg )
713 unused_msg = text "Defined but not used"
714 imp_from mod = text "Imported from" <+> quotes (ppr mod) <+> text "but not used"
718 addNameClashErrRn rdr_name (np1:nps)
719 = addErr (vcat [ptext SLIT("Ambiguous occurrence") <+> quotes (ppr rdr_name),
720 ptext SLIT("It could refer to") <+> vcat (msg1 : msgs)])
722 msg1 = ptext SLIT("either") <+> mk_ref np1
723 msgs = [ptext SLIT(" or") <+> mk_ref np | np <- nps]
724 mk_ref gre = quotes (ppr (gre_name gre)) <> comma <+> pprNameProvenance gre
726 shadowedNameWarn doc shadow
727 = hsep [ptext SLIT("This binding for"),
729 ptext SLIT("shadows an existing binding")]
733 = sep [ptext SLIT("Not in scope:"),
734 if isVarOcc occ_name then quotes (ppr name)
735 else text (occNameFlavour occ_name)
736 <+> quotes (ppr name)]
738 occ_name = rdrNameOcc name
740 unknownInstBndrErr cls op
741 = quotes (ppr op) <+> ptext SLIT("is not a (visible) method of class") <+> quotes (ppr cls)
744 = ptext SLIT("Illegal binding of built-in syntax:") <+> ppr (rdrNameOcc name)
745 -- The rdrNameOcc is because we don't want to print Prelude.(,)
747 dupNamesErr descriptor (L loc name : dup_things)
749 addErr ((ptext SLIT("Conflicting definitions for") <+> quotes (ppr name))