2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnEnv]{Environment manipulation for the renamer monad}
7 module RnEnv where -- Export everything
9 #include "HsVersions.h"
11 import CmdLineOpts ( opt_WarnNameShadowing, opt_WarnUnusedMatches,
12 opt_WarnUnusedBinds, opt_WarnUnusedImports )
14 import RdrHsSyn ( RdrNameIE )
15 import RnHsSyn ( RenamedHsType )
16 import RdrName ( RdrName, rdrNameModule, rdrNameOcc, isQual, isUnqual,
17 mkRdrUnqual, qualifyRdrName
19 import HsTypes ( getTyVarName, replaceTyVarName )
22 import Name ( Name, Provenance(..), ExportFlag(..), NamedThing(..),
23 ImportReason(..), getSrcLoc,
24 mkLocalName, mkImportedLocalName, mkGlobalName, isSystemName,
25 nameOccName, setNameModule, nameModule,
26 pprOccName, isLocallyDefined, nameUnique, nameOccName,
27 setNameProvenance, getNameProvenance, pprNameProvenance
30 import OccName ( OccName,
34 import TysWiredIn ( tupleTyCon, unboxedTupleTyCon, listTyCon )
35 import Type ( funTyCon )
36 import Module ( ModuleName, mkThisModule, mkVanillaModule, moduleName )
37 import TyCon ( TyCon )
39 import Unique ( Unique, Uniquable(..) )
40 import UniqFM ( emptyUFM, listToUFM, plusUFM_C )
42 import SrcLoc ( SrcLoc, noSrcLoc )
44 import Util ( removeDups, equivClasses, thenCmp )
46 import Maybes ( mapMaybe )
51 %*********************************************************
53 \subsection{Making new names}
55 %*********************************************************
58 newImportedGlobalName mod_name occ mod
59 = getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
63 case lookupFM cache key of
64 Just name -> returnRn name
65 Nothing -> setNameSupplyRn (us', inst_ns, new_cache) `thenRn_`
68 (us', us1) = splitUniqSupply us
69 uniq = uniqFromSupply us1
70 name = mkGlobalName uniq mod occ (NonLocalDef ImplicitImport False)
71 new_cache = addToFM cache key name
73 updateProvenances :: [Name] -> RnM d ()
74 updateProvenances names
75 = getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
76 setNameSupplyRn (us, inst_ns, update cache names)
78 update cache [] = cache
79 update cache (name:names) = WARN( not (key `elemFM` cache), ppr name )
80 update (addToFM cache key name) names
82 key = (moduleName (nameModule name), nameOccName name)
84 newImportedBinder :: Module -> RdrName -> RnM d Name
85 newImportedBinder mod rdr_name
86 = ASSERT2( isUnqual rdr_name, ppr rdr_name )
87 newImportedGlobalName (moduleName mod) (rdrNameOcc rdr_name) mod
89 -- Make an imported global name, checking first to see if it's in the cache
90 mkImportedGlobalName :: ModuleName -> OccName -> RnM d Name
91 mkImportedGlobalName mod_name occ
92 = newImportedGlobalName mod_name occ (mkVanillaModule mod_name)
94 mkImportedGlobalFromRdrName rdr_name
96 = mkImportedGlobalName (rdrNameModule rdr_name) (rdrNameOcc rdr_name)
99 = -- An Unqual is allowed; interface files contain
100 -- unqualified names for locally-defined things, such as
101 -- constructors of a data type.
102 getModuleRn `thenRn ` \ mod_name ->
103 mkImportedGlobalName mod_name (rdrNameOcc rdr_name)
106 newLocalTopBinder :: Module -> OccName
107 -> (Name -> ExportFlag) -> SrcLoc
109 newLocalTopBinder mod occ rec_exp_fn loc
110 = -- First check the cache
111 getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
113 key = (moduleName mod,occ)
114 mk_prov name = LocalDef loc (rec_exp_fn name)
115 -- We must set the provenance of the thing in the cache
116 -- correctly, particularly whether or not it is locally defined.
118 -- Since newLocallyDefinedGlobalName is used only
119 -- at binding occurrences, we may as well get the provenance
120 -- dead right first time; hence the rec_exp_fn passed in
122 case lookupFM cache key of
124 -- A hit in the cache!
125 -- Overwrite whatever provenance is in the cache already;
126 -- this updates WiredIn things and known-key things,
127 -- which are there from the start, to LocalDef.
129 -- It also means that if there are two defns for the same thing
130 -- in a module, then each gets a separate SrcLoc
132 new_name = setNameProvenance name (mk_prov new_name)
133 new_cache = addToFM cache key new_name
135 setNameSupplyRn (us, inst_ns, new_cache) `thenRn_`
138 -- Miss in the cache!
139 -- Build a new original name, and put it in the cache
141 (us', us1) = splitUniqSupply us
142 uniq = uniqFromSupply us1
143 new_name = mkGlobalName uniq mod occ (mk_prov new_name)
144 new_cache = addToFM cache key new_name
146 setNameSupplyRn (us', inst_ns, new_cache) `thenRn_`
150 %*********************************************************
152 \subsection{Dfuns and default methods}
154 %*********************************************************
156 @newImplicitBinder@ is used for (a) dfuns
157 (b) default methods, defined in this module.
160 newImplicitBinder occ src_loc
161 = getModuleRn `thenRn` \ mod_name ->
162 newLocalTopBinder (mkThisModule mod_name) occ (\_ -> Exported) src_loc
165 Make a name for the dict fun for an instance decl
168 newDFunName :: (OccName, OccName) -> SrcLoc -> RnMS Name
169 newDFunName key@(cl_occ, tycon_occ) loc
170 = newInstUniq key `thenRn` \ inst_uniq ->
171 newImplicitBinder (mkDFunOcc cl_occ tycon_occ inst_uniq) loc
175 getDFunKey :: RenamedHsType -> (OccName, OccName) -- Used to manufacture DFun names
176 getDFunKey (HsForAllTy _ _ ty) = getDFunKey ty
177 getDFunKey (MonoFunTy _ ty) = getDFunKey ty
178 getDFunKey (MonoDictTy cls (ty:_)) = (nameOccName cls, get_tycon_key ty)
180 get_tycon_key (MonoTyVar tv) = nameOccName (getName tv)
181 get_tycon_key (MonoTyApp ty _) = get_tycon_key ty
182 get_tycon_key (MonoTupleTy tys True) = getOccName (tupleTyCon (length tys))
183 get_tycon_key (MonoTupleTy tys False) = getOccName (unboxedTupleTyCon (length tys))
184 get_tycon_key (MonoListTy _) = getOccName listTyCon
185 get_tycon_key (MonoFunTy _ _) = getOccName funTyCon
189 %*********************************************************
193 %*********************************************************
196 -------------------------------------
197 bindLocatedLocalsRn :: SDoc -- Documentation string for error message
198 -> [(RdrName,SrcLoc)]
199 -> ([Name] -> RnMS a)
201 bindLocatedLocalsRn doc_str rdr_names_w_loc enclosed_scope
202 = checkDupOrQualNames doc_str rdr_names_w_loc `thenRn_`
204 getLocalNameEnv `thenRn` \ name_env ->
205 (if opt_WarnNameShadowing
207 mapRn_ (check_shadow name_env) rdr_names_w_loc
212 getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
213 getModeRn `thenRn` \ mode ->
215 n = length rdr_names_w_loc
216 (us', us1) = splitUniqSupply us
217 uniqs = uniqsFromSupply n us1
218 names = [ mk_name uniq (rdrNameOcc rdr_name) loc
219 | ((rdr_name,loc), uniq) <- rdr_names_w_loc `zip` uniqs
221 mk_name = case mode of
222 SourceMode -> mkLocalName
223 InterfaceMode -> mkImportedLocalName
224 -- Keep track of whether the name originally came from
225 -- an interface file.
227 setNameSupplyRn (us', inst_ns, cache) `thenRn_`
230 new_name_env = addListToRdrEnv name_env (map fst rdr_names_w_loc `zip` names)
232 setLocalNameEnv new_name_env (enclosed_scope names)
235 check_shadow name_env (rdr_name,loc)
236 = case lookupRdrEnv name_env rdr_name of
237 Nothing -> returnRn ()
238 Just name -> pushSrcLocRn loc $
239 addWarnRn (shadowedNameWarn rdr_name)
241 bindCoreLocalFVRn :: RdrName -> (Name -> RnMS (a, FreeVars))
242 -> RnMS (a, FreeVars)
243 -- A specialised variant when renaming stuff from interface
244 -- files (of which there is a lot)
246 -- * no checks for shadowing
248 -- * deal with free vars
249 bindCoreLocalFVRn rdr_name enclosed_scope
250 = getSrcLocRn `thenRn` \ loc ->
251 getLocalNameEnv `thenRn` \ name_env ->
252 getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
254 (us', us1) = splitUniqSupply us
255 uniq = uniqFromSupply us1
256 name = mkImportedLocalName uniq (rdrNameOcc rdr_name) loc
258 setNameSupplyRn (us', inst_ns, cache) `thenRn_`
260 new_name_env = extendRdrEnv name_env rdr_name name
262 setLocalNameEnv new_name_env (enclosed_scope name) `thenRn` \ (result, fvs) ->
263 returnRn (result, delFromNameSet fvs name)
265 bindCoreLocalsFVRn [] thing_inside = thing_inside []
266 bindCoreLocalsFVRn (b:bs) thing_inside = bindCoreLocalFVRn b $ \ name' ->
267 bindCoreLocalsFVRn bs $ \ names' ->
268 thing_inside (name':names')
270 -------------------------------------
271 bindLocalRn doc rdr_name enclosed_scope
272 = getSrcLocRn `thenRn` \ loc ->
273 bindLocatedLocalsRn doc [(rdr_name,loc)] $ \ (n:ns) ->
277 bindLocalsRn doc rdr_names enclosed_scope
278 = getSrcLocRn `thenRn` \ loc ->
279 bindLocatedLocalsRn doc
280 (rdr_names `zip` repeat loc)
283 -- binLocalsFVRn is the same as bindLocalsRn
284 -- except that it deals with free vars
285 bindLocalsFVRn doc rdr_names enclosed_scope
286 = bindLocalsRn doc rdr_names $ \ names ->
287 enclosed_scope names `thenRn` \ (thing, fvs) ->
288 returnRn (thing, delListFromNameSet fvs names)
290 -------------------------------------
291 extendTyVarEnvFVRn :: [HsTyVar Name] -> RnMS (a, FreeVars) -> RnMS (a, FreeVars)
292 -- This tiresome function is used only in rnDecl on InstDecl
293 extendTyVarEnvFVRn tyvars enclosed_scope
294 = getLocalNameEnv `thenRn` \ env ->
296 tyvar_names = map getTyVarName tyvars
297 new_env = addListToRdrEnv env [ (mkRdrUnqual (getOccName name), name)
298 | name <- tyvar_names
301 setLocalNameEnv new_env enclosed_scope `thenRn` \ (thing, fvs) ->
302 returnRn (thing, delListFromNameSet fvs tyvar_names)
304 bindTyVarsRn :: SDoc -> [HsTyVar RdrName]
305 -> ([HsTyVar Name] -> RnMS a)
307 bindTyVarsRn doc_str tyvar_names enclosed_scope
308 = bindTyVars2Rn doc_str tyvar_names $ \ names tyvars ->
309 enclosed_scope tyvars
311 -- Gruesome name: return Names as well as HsTyVars
312 bindTyVars2Rn :: SDoc -> [HsTyVar RdrName]
313 -> ([Name] -> [HsTyVar Name] -> RnMS a)
315 bindTyVars2Rn doc_str tyvar_names enclosed_scope
316 = getSrcLocRn `thenRn` \ loc ->
318 located_tyvars = [(getTyVarName tv, loc) | tv <- tyvar_names]
320 bindLocatedLocalsRn doc_str located_tyvars $ \ names ->
321 enclosed_scope names (zipWith replaceTyVarName tyvar_names names)
323 bindTyVarsFVRn :: SDoc -> [HsTyVar RdrName]
324 -> ([HsTyVar Name] -> RnMS (a, FreeVars))
325 -> RnMS (a, FreeVars)
326 bindTyVarsFVRn doc_str rdr_names enclosed_scope
327 = bindTyVars2Rn doc_str rdr_names $ \ names tyvars ->
328 enclosed_scope tyvars `thenRn` \ (thing, fvs) ->
329 returnRn (thing, delListFromNameSet fvs names)
331 bindTyVarsFV2Rn :: SDoc -> [HsTyVar RdrName]
332 -> ([Name] -> [HsTyVar Name] -> RnMS (a, FreeVars))
333 -> RnMS (a, FreeVars)
334 bindTyVarsFV2Rn doc_str rdr_names enclosed_scope
335 = bindTyVars2Rn doc_str rdr_names $ \ names tyvars ->
336 enclosed_scope names tyvars `thenRn` \ (thing, fvs) ->
337 returnRn (thing, delListFromNameSet fvs names)
340 -------------------------------------
341 checkDupOrQualNames, checkDupNames :: SDoc
342 -> [(RdrName, SrcLoc)]
344 -- Works in any variant of the renamer monad
346 checkDupOrQualNames doc_str rdr_names_w_loc
347 = -- Check for use of qualified names
348 mapRn_ (qualNameErr doc_str) quals `thenRn_`
349 checkDupNames doc_str rdr_names_w_loc
351 quals = filter (isQual.fst) rdr_names_w_loc
353 checkDupNames doc_str rdr_names_w_loc
354 = -- Check for duplicated names in a binding group
355 mapRn_ (dupNamesErr doc_str) dups
357 (_, dups) = removeDups (\(n1,l1) (n2,l2) -> n1 `compare` n2) rdr_names_w_loc
361 %*********************************************************
363 \subsection{Looking up names}
365 %*********************************************************
367 Looking up a name in the RnEnv.
370 lookupBndrRn rdr_name
371 = getNameEnvs `thenRn` \ (global_env, local_env) ->
374 case lookupRdrEnv local_env rdr_name of {
375 Just name -> returnRn name ;
378 getModeRn `thenRn` \ mode ->
380 InterfaceMode -> -- Look in the global name cache
381 mkImportedGlobalFromRdrName rdr_name
383 SourceMode -> -- Source mode, so look up a *qualified* version
384 -- of the name, so that we get the right one even
385 -- if there are many with the same occ name
386 -- There must *be* a binding
387 getModuleRn `thenRn` \ mod ->
388 case lookupRdrEnv global_env (qualifyRdrName mod rdr_name) of
389 Just (name:rest) -> ASSERT( null rest )
391 Nothing -> pprPanic "lookupBndrRn" (ppr mod <+> ppr rdr_name)
394 -- Just like lookupRn except that we record the occurrence too
395 -- Perhaps surprisingly, even wired-in names are recorded.
396 -- Why? So that we know which wired-in names are referred to when
397 -- deciding which instance declarations to import.
398 lookupOccRn :: RdrName -> RnMS Name
400 = getNameEnvs `thenRn` \ (global_env, local_env) ->
401 lookup_occ global_env local_env rdr_name
403 -- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
404 -- environment. It's used only for
405 -- record field names
406 -- class op names in class and instance decls
407 lookupGlobalOccRn :: RdrName -> RnMS Name
408 lookupGlobalOccRn rdr_name
409 = getNameEnvs `thenRn` \ (global_env, local_env) ->
410 lookup_global_occ global_env rdr_name
412 -- Look in both local and global env
413 lookup_occ global_env local_env rdr_name
414 = case lookupRdrEnv local_env rdr_name of
415 Just name -> returnRn name
416 Nothing -> lookup_global_occ global_env rdr_name
418 -- Look in global env only
419 lookup_global_occ global_env rdr_name
420 = case lookupRdrEnv global_env rdr_name of
421 Just [name] -> returnRn name
422 Just stuff@(name:_) -> addNameClashErrRn rdr_name stuff `thenRn_`
424 Nothing -> getModeRn `thenRn` \ mode ->
426 -- Not found when processing source code; so fail
427 SourceMode -> failWithRn (mkUnboundName rdr_name)
428 (unknownNameErr rdr_name)
430 -- Not found when processing an imported declaration,
431 -- so we create a new name for the purpose
432 InterfaceMode -> mkImportedGlobalFromRdrName rdr_name
435 @lookupImplicitOccRn@ takes an RdrName representing an {\em original} name,
436 and adds it to the occurrence pool so that it'll be loaded later.
437 This is used when language constructs
438 (such as monad comprehensions, overloaded literals, or deriving clauses)
439 require some stuff to be loaded that isn't explicitly mentioned in the code.
441 This doesn't apply in interface mode, where everything is explicit,
442 but we don't check for this case:
443 it does no harm to record an ``extra'' occurrence
444 and @lookupImplicitOccRn@ isn't used much in interface mode
445 (it's only the @Nothing@ clause of @rnDerivs@ that calls it at all I think).
447 \fbox{{\em Jan 98: this comment is wrong: @rnHsType@ uses it quite a bit.}}
449 For List and Tuple types it's important to get the correct
450 @isLocallyDefined@ flag, which is used in turn when deciding
451 whether there are any instance decls in this module are ``special''.
452 The name cache should have the correct provenance, though.
455 lookupImplicitOccRn :: RdrName -> RnMS Name
456 lookupImplicitOccRn rdr_name = mkImportedGlobalFromRdrName rdr_name
459 @unQualInScope@ returns a function that takes a @Name@ and tells whether
460 its unqualified name is in scope. This is put as a boolean flag in
461 the @Name@'s provenance to guide whether or not to print the name qualified
465 unQualInScope :: GlobalRdrEnv -> Name -> Bool
469 lookup name = case lookupRdrEnv env (mkRdrUnqual (nameOccName name)) of
470 Just [name'] -> name == name'
474 %************************************************************************
476 \subsection{Envt utility functions}
478 %************************************************************************
480 \subsubsection{NameEnv}% ================
483 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
484 plusGlobalRdrEnv env1 env2 = plusFM_C combine_globals env1 env2
486 addOneToGlobalRdrEnv :: GlobalRdrEnv -> RdrName -> Name -> GlobalRdrEnv
487 addOneToGlobalRdrEnv env rdr_name name = addToFM_C combine_globals env rdr_name [name]
489 delOneFromGlobalRdrEnv :: GlobalRdrEnv -> RdrName -> GlobalRdrEnv
490 delOneFromGlobalRdrEnv env rdr_name = delFromFM env rdr_name
492 combine_globals :: [Name] -- Old
495 combine_globals ns_old ns_new -- ns_new is often short
496 = foldr add ns_old ns_new
498 add n ns | any (is_duplicate n) ns_old = map choose ns -- Eliminate duplicates
501 choose n' | n==n' && better_provenance n n' = n
505 -- a local thing over an imported thing
506 -- a user-imported thing over a non-user-imported thing
507 -- an explicitly-imported thing over an implicitly imported thing
508 better_provenance n1 n2
509 = case (getNameProvenance n1, getNameProvenance n2) of
510 (LocalDef _ _, _ ) -> True
511 (NonLocalDef (UserImport _ _ True) _, _ ) -> True
512 (NonLocalDef (UserImport _ _ _ ) _, NonLocalDef ImplicitImport _) -> True
515 is_duplicate :: Name -> Name -> Bool
516 is_duplicate n1 n2 | isLocallyDefined n1 && isLocallyDefined n2 = False
517 | otherwise = n1 == n2
519 We treat two bindings of a locally-defined name as a duplicate,
520 because they might be two separate, local defns and we want to report
521 and error for that, {\em not} eliminate a duplicate.
523 On the other hand, if you import the same name from two different
524 import statements, we {\em d}* want to eliminate the duplicate, not report
527 If a module imports itself then there might be a local defn and an imported
528 defn of the same name; in this case the names will compare as equal, but
529 will still have different provenances.
533 \subsubsection{ExportAvails}% ================
536 mkEmptyExportAvails :: ModuleName -> ExportAvails
537 mkEmptyExportAvails mod_name = (unitFM mod_name [], emptyUFM)
539 mkExportAvails :: ModuleName -> Bool -> GlobalRdrEnv -> [AvailInfo] -> ExportAvails
540 mkExportAvails mod_name unqual_imp name_env avails
541 = (mod_avail_env, entity_avail_env)
543 mod_avail_env = unitFM mod_name unqual_avails
545 -- unqual_avails is the Avails that are visible in *unqualfied* form
546 -- (1.4 Report, Section 5.1.1)
548 -- import T hiding( f )
549 -- we delete f from avails
551 unqual_avails | not unqual_imp = [] -- Short cut when no unqualified imports
552 | otherwise = mapMaybe prune avails
554 prune (Avail n) | unqual_in_scope n = Just (Avail n)
555 prune (Avail n) | otherwise = Nothing
556 prune (AvailTC n ns) | null uqs = Nothing
557 | otherwise = Just (AvailTC n uqs)
559 uqs = filter unqual_in_scope ns
561 unqual_in_scope n = unQualInScope name_env n
563 entity_avail_env = listToUFM [ (name,avail) | avail <- avails,
564 name <- availNames avail]
566 plusExportAvails :: ExportAvails -> ExportAvails -> ExportAvails
567 plusExportAvails (m1, e1) (m2, e2)
568 = (plusFM_C (++) m1 m2, plusUFM_C plusAvail e1 e2)
569 -- ToDo: wasteful: we do this once for each constructor!
573 \subsubsection{AvailInfo}% ================
576 plusAvail (Avail n1) (Avail n2) = Avail n1
577 plusAvail (AvailTC n1 ns1) (AvailTC n2 ns2) = AvailTC n1 (nub (ns1 ++ ns2))
580 plusAvail a1 a2 = pprPanic "RnEnv.plusAvail" (hsep [pprAvail a1,pprAvail a2])
583 addAvailToNameSet :: NameSet -> AvailInfo -> NameSet
584 addAvailToNameSet names avail = addListToNameSet names (availNames avail)
586 availsToNameSet :: [AvailInfo] -> NameSet
587 availsToNameSet avails = foldl addAvailToNameSet emptyNameSet avails
589 availName :: AvailInfo -> Name
590 availName (Avail n) = n
591 availName (AvailTC n _) = n
593 availNames :: AvailInfo -> [Name]
594 availNames (Avail n) = [n]
595 availNames (AvailTC n ns) = ns
597 filterAvail :: RdrNameIE -- Wanted
598 -> AvailInfo -- Available
599 -> Maybe AvailInfo -- Resulting available;
600 -- Nothing if (any of the) wanted stuff isn't there
602 filterAvail ie@(IEThingWith want wants) avail@(AvailTC n ns)
603 | sub_names_ok = Just (AvailTC n (filter is_wanted ns))
604 | otherwise = Nothing
606 is_wanted name = nameOccName name `elem` wanted_occs
607 sub_names_ok = all (`elem` avail_occs) wanted_occs
608 avail_occs = map nameOccName ns
609 wanted_occs = map rdrNameOcc (want:wants)
611 filterAvail (IEThingAbs _) (AvailTC n ns) = ASSERT( n `elem` ns )
614 filterAvail (IEThingAbs _) avail@(Avail n) = Just avail -- Type synonyms
616 filterAvail (IEVar _) avail@(Avail n) = Just avail
617 filterAvail (IEVar v) avail@(AvailTC n ns) = Just (AvailTC n (filter wanted ns))
619 wanted n = nameOccName n == occ
621 -- The second equation happens if we import a class op, thus
623 -- where op is a class operation
625 filterAvail (IEThingAll _) avail@(AvailTC _ _) = Just avail
627 filterAvail ie avail = Nothing
630 -- In interfaces, pprAvail gets given the OccName of the "host" thing
631 pprAvail avail = getPprStyle $ \ sty ->
632 if ifaceStyle sty then
633 ppr_avail (pprOccName . nameOccName) avail
637 ppr_avail pp_name (AvailTC n ns) = hsep [
639 parens $ hsep $ punctuate comma $
642 ppr_avail pp_name (Avail n) = pp_name n
648 %************************************************************************
650 \subsection{Free variable manipulation}
652 %************************************************************************
655 type FreeVars = NameSet
657 plusFV :: FreeVars -> FreeVars -> FreeVars
658 addOneFV :: FreeVars -> Name -> FreeVars
659 unitFV :: Name -> FreeVars
661 plusFVs :: [FreeVars] -> FreeVars
663 isEmptyFVs = isEmptyNameSet
664 emptyFVs = emptyNameSet
665 plusFVs = unionManyNameSets
666 plusFV = unionNameSets
668 -- No point in adding implicitly imported names to the free-var set
669 addOneFV s n = addOneToNameSet s n
670 unitFV n = unitNameSet n
673 mapFvRn f xs = mapRn f xs `thenRn` \ stuff ->
675 (ys, fvs_s) = unzip stuff
677 returnRn (ys, plusFVs fvs_s)
681 %************************************************************************
683 \subsection{Envt utility functions}
685 %************************************************************************
689 warnUnusedLocalBinds, warnUnusedTopNames, warnUnusedMatches :: [Name] -> RnM d ()
691 warnUnusedTopNames names
692 | not opt_WarnUnusedBinds && not opt_WarnUnusedImports
693 = returnRn () -- Don't force ns unless necessary
695 = warnUnusedBinds (\ is_local -> not is_local) names
697 warnUnusedLocalBinds ns
698 | not opt_WarnUnusedBinds = returnRn ()
699 | otherwise = warnUnusedBinds (\ is_local -> is_local) ns
701 warnUnusedMatches names
702 | opt_WarnUnusedMatches = warnUnusedGroup (const True) names
703 | otherwise = returnRn ()
705 -------------------------
707 warnUnusedBinds :: (Bool -> Bool) -> [Name] -> RnM d ()
708 warnUnusedBinds warn_when_local names
709 = mapRn_ (warnUnusedGroup warn_when_local) groups
711 -- Group by provenance
712 groups = equivClasses cmp names
713 name1 `cmp` name2 = getNameProvenance name1 `cmp_prov` getNameProvenance name2
715 cmp_prov (LocalDef _ _) (NonLocalDef _ _) = LT
716 cmp_prov (LocalDef loc1 _) (LocalDef loc2 _) = loc1 `compare` loc2
717 cmp_prov (NonLocalDef (UserImport m1 loc1 _) _)
718 (NonLocalDef (UserImport m2 loc2 _) _) =
719 (m1 `compare` m2) `thenCmp` (loc1 `compare` loc2)
720 cmp_prov (NonLocalDef _ _) (LocalDef _ _) = GT
721 -- In-scope NonLocalDefs must have UserImport info on them
723 -------------------------
725 warnUnusedGroup :: (Bool -> Bool) -> [Name] -> RnM d ()
729 warnUnusedGroup emit_warning names
730 | not (emit_warning is_local) = returnRn ()
732 = pushSrcLocRn def_loc $
734 sep [msg <> colon, nest 4 (fsep (punctuate comma (map ppr names)))]
737 (is_local, def_loc, msg)
738 = case getNameProvenance name1 of
739 LocalDef loc _ -> (True, loc, text "Defined but not used")
740 NonLocalDef (UserImport mod loc _) _ ->
741 (True, loc, text "Imported from" <+> quotes (ppr mod) <+>
743 other -> (False, getSrcLoc name1, text "Strangely defined but not used")
747 addNameClashErrRn rdr_name (name1:names)
748 = addErrRn (vcat [ptext SLIT("Ambiguous occurrence") <+> quotes (ppr rdr_name),
749 ptext SLIT("It could refer to") <+> vcat (msg1 : msgs)])
751 msg1 = ptext SLIT("either") <+> mk_ref name1
752 msgs = [ptext SLIT(" or") <+> mk_ref name | name <- names]
753 mk_ref name = quotes (ppr name) <> comma <+> pprNameProvenance name
755 fixityClashErr (rdr_name, ((_,how_in_scope1), (_, how_in_scope2)))
756 = hang (hsep [ptext SLIT("Conflicting fixities for"), quotes (ppr rdr_name)])
757 4 (vcat [ppr how_in_scope1,
760 shadowedNameWarn shadow
761 = hsep [ptext SLIT("This binding for"),
763 ptext SLIT("shadows an existing binding")]
766 = sep [text flavour, ptext SLIT("not in scope:"), quotes (ppr name)]
768 flavour = occNameFlavour (rdrNameOcc name)
770 qualNameErr descriptor (name,loc)
772 addErrRn (hsep [ ptext SLIT("Invalid use of qualified name"),
777 dupNamesErr descriptor ((name,loc) : dup_things)
779 addErrRn ((ptext SLIT("Conflicting definitions for") <+> quotes (ppr name))
781 (ptext SLIT("in") <+> descriptor))