2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-2006
4 \section[RnEnv]{Environment manipulation for the renamer monad}
8 newTopSrcBinder, lookupFamInstDeclBndr,
9 lookupLocatedBndrRn, lookupBndrRn,
10 lookupLocatedTopBndrRn, lookupTopBndrRn,
11 lookupLocatedOccRn, lookupOccRn,
12 lookupLocatedGlobalOccRn, lookupGlobalOccRn,
13 lookupLocalDataTcNames, lookupSrcOcc_maybe,
14 lookupFixityRn, lookupTyFixityRn, lookupLocatedSigOccRn,
15 lookupLocatedInstDeclBndr,
16 lookupSyntaxName, lookupSyntaxTable, lookupImportedName,
17 lookupGreRn, lookupGreRn_maybe,
20 newLocalsRn, newIPNameRn,
21 bindLocalNames, bindLocalNamesFV,
22 bindLocatedLocalsFV, bindLocatedLocalsRn,
23 bindSigTyVarsFV, bindPatSigTyVars, bindPatSigTyVarsFV,
24 bindTyVarsRn, extendTyVarEnvFVRn,
27 checkDupNames, mapFvRn,
28 warnUnusedMatches, warnUnusedModules, warnUnusedImports,
29 warnUnusedTopBinds, warnUnusedLocalBinds,
30 dataTcOccs, unknownNameErr,
33 #include "HsVersions.h"
35 import LoadIface ( loadInterfaceForName, loadSrcInterface )
36 import IfaceEnv ( lookupOrig, newGlobalBinder, newIPName )
37 import HsSyn ( FixitySig(..), HsExpr(..), SyntaxExpr, SyntaxTable,
38 LHsTyVarBndr, LHsType,
39 Fixity, hsLTyVarLocNames, replaceTyVarName )
40 import RdrHsSyn ( extractHsTyRdrTyVars )
41 import RdrName ( RdrName, isQual, isUnqual, isOrig_maybe,
43 mkRdrUnqual, setRdrNameSpace, rdrNameOcc,
44 pprGlobalRdrEnv, lookupGRE_RdrName,
45 isExact_maybe, isSrcRdrName,
47 GlobalRdrElt(..), GlobalRdrEnv, lookupGlobalRdrEnv,
48 isLocalGRE, extendLocalRdrEnv, elemLocalRdrEnv, lookupLocalRdrEnv,
49 Provenance(..), pprNameProvenance,
50 importSpecLoc, importSpecModule
52 import HscTypes ( availNames, ModIface(..), FixItem(..), lookupFixity )
54 import Name ( Name, nameIsLocalOrFrom, mkInternalName, isWiredInName,
55 nameSrcLoc, nameOccName, nameModule, isExternalName )
57 import OccName ( tcName, isDataOcc, pprNonVarNameSpace, occNameSpace,
59 import Module ( Module, ModuleName )
60 import PrelNames ( mkUnboundName, rOOT_MAIN, iNTERACTIVE, consDataConKey, hasKey )
62 import BasicTypes ( IPName, mapIPName )
63 import SrcLoc ( SrcSpan, srcSpanStart, Located(..), eqLocated, unLoc,
64 srcLocSpan, getLoc, combineSrcSpans, isOneLineSpan )
66 import Util ( sortLe )
67 import ListSetOps ( removeDups )
73 %*********************************************************
77 %*********************************************************
80 newTopSrcBinder :: Module -> Located RdrName -> RnM Name
81 newTopSrcBinder this_mod (L loc rdr_name)
82 | Just name <- isExact_maybe rdr_name
83 = -- This is here to catch
84 -- (a) Exact-name binders created by Template Haskell
85 -- (b) The PrelBase defn of (say) [] and similar, for which
86 -- the parser reads the special syntax and returns an Exact RdrName
87 -- We are at a binding site for the name, so check first that it
88 -- the current module is the correct one; otherwise GHC can get
89 -- very confused indeed. This test rejects code like
90 -- data T = (,) Int Int
91 -- unless we are in GHC.Tup
92 ASSERT2( isExternalName name, ppr name )
93 do { checkM (this_mod == nameModule name)
94 (addErrAt loc (badOrigBinding rdr_name))
98 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
99 = do { checkM (rdr_mod == this_mod || rdr_mod == rOOT_MAIN)
100 (addErrAt loc (badOrigBinding rdr_name))
101 -- When reading External Core we get Orig names as binders,
102 -- but they should agree with the module gotten from the monad
104 -- We can get built-in syntax showing up here too, sadly. If you type
106 -- the constructor is parsed as a type, and then RdrHsSyn.tyConToDataCon
107 -- uses setRdrNameSpace to make it into a data constructors. At that point
108 -- the nice Exact name for the TyCon gets swizzled to an Orig name.
109 -- Hence the badOrigBinding error message.
111 -- Except for the ":Main.main = ..." definition inserted into
112 -- the Main module; ugh!
114 -- Because of this latter case, we call newGlobalBinder with a module from
115 -- the RdrName, not from the environment. In principle, it'd be fine to
116 -- have an arbitrary mixture of external core definitions in a single module,
117 -- (apart from module-initialisation issues, perhaps).
118 ; newGlobalBinder rdr_mod rdr_occ (srcSpanStart loc) }
119 --TODO, should pass the whole span
122 = do { checkM (not (isQual rdr_name))
123 (addErrAt loc (badQualBndrErr rdr_name))
124 -- Binders should not be qualified; if they are, and with a different
125 -- module name, we we get a confusing "M.T is not in scope" error later
126 ; newGlobalBinder this_mod (rdrNameOcc rdr_name) (srcSpanStart loc) }
129 %*********************************************************
131 Source code occurrences
133 %*********************************************************
135 Looking up a name in the RnEnv.
138 lookupLocatedBndrRn :: Located RdrName -> RnM (Located Name)
139 lookupLocatedBndrRn = wrapLocM lookupBndrRn
141 lookupBndrRn :: RdrName -> RnM Name
142 -- NOTE: assumes that the SrcSpan of the binder has already been setSrcSpan'd
143 lookupBndrRn rdr_name
144 = getLocalRdrEnv `thenM` \ local_env ->
145 case lookupLocalRdrEnv local_env rdr_name of
146 Just name -> returnM name
147 Nothing -> lookupTopBndrRn rdr_name
149 lookupLocatedTopBndrRn :: Located RdrName -> RnM (Located Name)
150 lookupLocatedTopBndrRn = wrapLocM lookupTopBndrRn
152 lookupTopBndrRn :: RdrName -> RnM Name
153 -- Look up a top-level source-code binder. We may be looking up an unqualified 'f',
154 -- and there may be several imported 'f's too, which must not confuse us.
155 -- For example, this is OK:
157 -- infix 9 f -- The 'f' here does not need to be qualified
158 -- f x = x -- Nor here, of course
159 -- So we have to filter out the non-local ones.
161 -- A separate function (importsFromLocalDecls) reports duplicate top level
162 -- decls, so here it's safe just to choose an arbitrary one.
164 -- There should never be a qualified name in a binding position in Haskell,
165 -- but there can be if we have read in an external-Core file.
166 -- The Haskell parser checks for the illegal qualified name in Haskell
167 -- source files, so we don't need to do so here.
169 lookupTopBndrRn rdr_name
170 | Just name <- isExact_maybe rdr_name
173 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
174 -- This deals with the case of derived bindings, where
175 -- we don't bother to call newTopSrcBinder first
176 -- We assume there is no "parent" name
177 = do { loc <- getSrcSpanM
178 ; newGlobalBinder rdr_mod rdr_occ (srcSpanStart loc) }
181 = do { mb_gre <- lookupGreLocalRn rdr_name
184 traceRn $ text "lookupTopBndrRn"
186 Just gre -> returnM (gre_name gre) }
188 -- lookupLocatedSigOccRn is used for type signatures and pragmas
194 -- It's clear that the 'f' in the signature must refer to A.f
195 -- The Haskell98 report does not stipulate this, but it will!
196 -- So we must treat the 'f' in the signature in the same way
197 -- as the binding occurrence of 'f', using lookupBndrRn
198 lookupLocatedSigOccRn :: Located RdrName -> RnM (Located Name)
199 lookupLocatedSigOccRn = lookupLocatedBndrRn
201 -- lookupInstDeclBndr is used for the binders in an
202 -- instance declaration. Here we use the class name to
205 lookupLocatedInstDeclBndr :: Name -> Located RdrName -> RnM (Located Name)
206 lookupLocatedInstDeclBndr cls rdr = wrapLocM (lookupInstDeclBndr cls) rdr
208 lookupInstDeclBndr :: Name -> RdrName -> RnM Name
209 -- This is called on the method name on the left-hand side of an
210 -- instance declaration binding. eg. instance Functor T where
212 -- ^^^^ called on this
213 -- Regardless of how many unqualified fmaps are in scope, we want
214 -- the one that comes from the Functor class.
215 lookupInstDeclBndr cls_name rdr_name
216 | isUnqual rdr_name -- Find all the things the rdr-name maps to
217 = do { -- and pick the one with the right parent name
218 let { is_op gre@(GRE {gre_par = ParentIs n}) = cls_name == n
219 ; is_op other = False
220 ; occ = rdrNameOcc rdr_name
221 ; lookup_fn env = filter is_op (lookupGlobalRdrEnv env occ) }
222 ; mb_gre <- lookupGreRn_help rdr_name lookup_fn
224 Just gre -> return (gre_name gre)
225 Nothing -> do { addErr (unknownInstBndrErr cls_name rdr_name)
226 ; traceRn (text "lookupInstDeclBndr" <+> ppr rdr_name)
227 ; return (mkUnboundName rdr_name) } }
229 | otherwise -- Occurs in derived instances, where we just
230 -- refer directly to the right method
231 = ASSERT2( not (isQual rdr_name), ppr rdr_name )
232 -- NB: qualified names are rejected by the parser
233 lookupImportedName rdr_name
235 newIPNameRn :: IPName RdrName -> TcRnIf m n (IPName Name)
236 newIPNameRn ip_rdr = newIPName (mapIPName rdrNameOcc ip_rdr)
238 -- Looking up family names in type instances is a subtle affair. The family
239 -- may be imported, in which case we need to lookup the occurence of a global
240 -- name. Alternatively, the family may be in the same binding group (and in
241 -- fact in a declaration processed later), and we need to create a new top
244 -- So, also this is strictly speaking an occurence, we cannot raise an error
245 -- message yet for instances without a family declaration. This will happen
246 -- during renaming the type instance declaration in RnSource.rnTyClDecl.
248 lookupFamInstDeclBndr :: Module -> Located RdrName -> RnM Name
249 lookupFamInstDeclBndr mod lrdr_name@(L _ rdr_name)
250 = do { mb_gre <- lookupGreRn_maybe rdr_name
252 Just gre -> returnM (gre_name gre) ;
253 Nothing -> newTopSrcBinder mod lrdr_name }
255 --------------------------------------------------
257 --------------------------------------------------
259 getLookupOccRn :: RnM (Name -> Maybe Name)
261 = getLocalRdrEnv `thenM` \ local_env ->
262 return (lookupLocalRdrEnv local_env . mkRdrUnqual . nameOccName)
264 lookupLocatedOccRn :: Located RdrName -> RnM (Located Name)
265 lookupLocatedOccRn = wrapLocM lookupOccRn
267 -- lookupOccRn looks up an occurrence of a RdrName
268 lookupOccRn :: RdrName -> RnM Name
270 = getLocalRdrEnv `thenM` \ local_env ->
271 case lookupLocalRdrEnv local_env rdr_name of
272 Just name -> returnM name
273 Nothing -> lookupGlobalOccRn rdr_name
275 lookupLocatedGlobalOccRn :: Located RdrName -> RnM (Located Name)
276 lookupLocatedGlobalOccRn = wrapLocM lookupGlobalOccRn
278 lookupGlobalOccRn :: RdrName -> RnM Name
279 -- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
280 -- environment. It's used only for
281 -- record field names
282 -- class op names in class and instance decls
284 lookupGlobalOccRn rdr_name
285 | not (isSrcRdrName rdr_name)
286 = lookupImportedName rdr_name
289 = -- First look up the name in the normal environment.
290 lookupGreRn_maybe rdr_name `thenM` \ mb_gre ->
292 Just gre -> returnM (gre_name gre) ;
295 -- We allow qualified names on the command line to refer to
296 -- *any* name exported by any module in scope, just as if
297 -- there was an "import qualified M" declaration for every
299 getModule `thenM` \ mod ->
300 if isQual rdr_name && mod == iNTERACTIVE then
301 -- This test is not expensive,
302 lookupQualifiedName rdr_name -- and only happens for failed lookups
304 traceRn $ text "lookupGlobalOccRn"
305 unboundName rdr_name }
307 lookupImportedName :: RdrName -> TcRnIf m n Name
308 -- Lookup the occurrence of an imported name
309 -- The RdrName is *always* qualified or Exact
310 -- Treat it as an original name, and conjure up the Name
311 -- Usually it's Exact or Orig, but it can be Qual if it
312 -- comes from an hi-boot file. (This minor infelicity is
313 -- just to reduce duplication in the parser.)
314 lookupImportedName rdr_name
315 | Just n <- isExact_maybe rdr_name
316 -- This happens in derived code
319 -- Always Orig, even when reading a .hi-boot file
320 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
321 = lookupOrig rdr_mod rdr_occ
324 = pprPanic "RnEnv.lookupImportedName" (ppr rdr_name)
326 unboundName :: RdrName -> RnM Name
328 = do { addErr (unknownNameErr rdr_name)
329 ; env <- getGlobalRdrEnv;
330 ; traceRn (vcat [unknownNameErr rdr_name,
331 ptext SLIT("Global envt is:"),
332 nest 3 (pprGlobalRdrEnv env)])
333 ; returnM (mkUnboundName rdr_name) }
335 --------------------------------------------------
336 -- Lookup in the Global RdrEnv of the module
337 --------------------------------------------------
339 lookupSrcOcc_maybe :: RdrName -> RnM (Maybe Name)
340 -- No filter function; does not report an error on failure
341 lookupSrcOcc_maybe rdr_name
342 = do { mb_gre <- lookupGreRn_maybe rdr_name
344 Nothing -> returnM Nothing
345 Just gre -> returnM (Just (gre_name gre)) }
347 -------------------------
348 lookupGreRn_maybe :: RdrName -> RnM (Maybe GlobalRdrElt)
349 -- Just look up the RdrName in the GlobalRdrEnv
350 lookupGreRn_maybe rdr_name
351 = lookupGreRn_help rdr_name (lookupGRE_RdrName rdr_name)
353 lookupGreRn :: RdrName -> RnM GlobalRdrElt
354 -- If not found, add error message, and return a fake GRE
356 = do { mb_gre <- lookupGreRn_maybe rdr_name
358 Just gre -> return gre ;
360 { traceRn $ text "lookupGreRn"
361 ; name <- unboundName rdr_name
362 ; return (GRE { gre_name = name, gre_par = NoParent,
363 gre_prov = LocalDef }) }}}
365 lookupGreLocalRn :: RdrName -> RnM (Maybe GlobalRdrElt)
366 -- Similar, but restricted to locally-defined things
367 lookupGreLocalRn rdr_name
368 = lookupGreRn_help rdr_name lookup_fn
370 lookup_fn env = filter isLocalGRE (lookupGRE_RdrName rdr_name env)
372 lookupGreRn_help :: RdrName -- Only used in error message
373 -> (GlobalRdrEnv -> [GlobalRdrElt]) -- Lookup function
374 -> RnM (Maybe GlobalRdrElt)
375 -- Checks for exactly one match; reports deprecations
376 -- Returns Nothing, without error, if too few
377 lookupGreRn_help rdr_name lookup
378 = do { env <- getGlobalRdrEnv
380 [] -> returnM Nothing
381 [gre] -> returnM (Just gre)
382 gres -> do { addNameClashErrRn rdr_name gres
383 ; returnM (Just (head gres)) } }
385 ------------------------------
387 ------------------------------
389 -- A qualified name on the command line can refer to any module at all: we
390 -- try to load the interface if we don't already have it.
391 lookupQualifiedName :: RdrName -> RnM Name
392 lookupQualifiedName rdr_name
393 | Just (mod,occ) <- isQual_maybe rdr_name
394 -- Note: we want to behave as we would for a source file import here,
395 -- and respect hiddenness of modules/packages, hence loadSrcInterface.
396 = loadSrcInterface doc mod False `thenM` \ iface ->
399 (mod,avails) <- mi_exports iface,
401 name <- availNames avail,
403 ((mod,occ):ns) -> ASSERT (null ns)
405 _ -> unboundName rdr_name
408 = pprPanic "RnEnv.lookupQualifiedName" (ppr rdr_name)
410 doc = ptext SLIT("Need to find") <+> ppr rdr_name
413 %*********************************************************
417 %*********************************************************
420 lookupLocalDataTcNames :: RdrName -> RnM [Name]
421 -- GHC extension: look up both the tycon and data con
422 -- for con-like things
423 -- Complain if neither is in scope
424 lookupLocalDataTcNames rdr_name
425 | Just n <- isExact_maybe rdr_name
426 -- Special case for (:), which doesn't get into the GlobalRdrEnv
427 = return [n] -- For this we don't need to try the tycon too
429 = do { mb_gres <- mapM lookupGreLocalRn (dataTcOccs rdr_name)
430 ; case [gre_name gre | Just gre <- mb_gres] of
431 [] -> do { addErr (unknownNameErr rdr_name)
433 names -> return names
436 --------------------------------
437 bindLocalFixities :: [FixitySig RdrName] -> RnM a -> RnM a
438 -- Used for nested fixity decls
439 -- No need to worry about type constructors here,
440 -- Should check for duplicates but we don't
441 bindLocalFixities fixes thing_inside
442 | null fixes = thing_inside
443 | otherwise = mappM rn_sig fixes `thenM` \ new_bit ->
444 extendFixityEnv new_bit thing_inside
446 rn_sig (FixitySig lv@(L loc v) fix)
447 = addLocM lookupBndrRn lv `thenM` \ new_v ->
448 returnM (new_v, (FixItem (rdrNameOcc v) fix loc))
451 --------------------------------
452 lookupFixity is a bit strange.
454 * Nested local fixity decls are put in the local fixity env, which we
455 find with getFixtyEnv
457 * Imported fixities are found in the HIT or PIT
459 * Top-level fixity decls in this module may be for Names that are
460 either Global (constructors, class operations)
461 or Local/Exported (everything else)
462 (See notes with RnNames.getLocalDeclBinders for why we have this split.)
463 We put them all in the local fixity environment
466 lookupFixityRn :: Name -> RnM Fixity
468 = getModule `thenM` \ this_mod ->
469 if nameIsLocalOrFrom this_mod name
470 then -- It's defined in this module
471 getFixityEnv `thenM` \ local_fix_env ->
472 traceRn (text "lookupFixityRn" <+> (ppr name $$ ppr local_fix_env)) `thenM_`
473 returnM (lookupFixity local_fix_env name)
475 else -- It's imported
476 -- For imported names, we have to get their fixities by doing a
477 -- loadInterfaceForName, and consulting the Ifaces that comes back
478 -- from that, because the interface file for the Name might not
479 -- have been loaded yet. Why not? Suppose you import module A,
480 -- which exports a function 'f', thus;
481 -- module CurrentModule where
483 -- module A( f ) where
485 -- Then B isn't loaded right away (after all, it's possible that
486 -- nothing from B will be used). When we come across a use of
487 -- 'f', we need to know its fixity, and it's then, and only
488 -- then, that we load B.hi. That is what's happening here.
490 -- loadInterfaceForName will find B.hi even if B is a hidden module,
491 -- and that's what we want.
492 loadInterfaceForName doc name `thenM` \ iface ->
493 returnM (mi_fix_fn iface (nameOccName name))
495 doc = ptext SLIT("Checking fixity for") <+> ppr name
498 lookupTyFixityRn :: Located Name -> RnM Fixity
499 lookupTyFixityRn (L loc n)
500 = do { glaExts <- doptM Opt_GlasgowExts
501 ; when (not glaExts) (addWarnAt loc (infixTyConWarn n))
505 dataTcOccs :: RdrName -> [RdrName]
506 -- If the input is a data constructor, return both it and a type
507 -- constructor. This is useful when we aren't sure which we are
510 | Just n <- isExact_maybe rdr_name -- Ghastly special case
511 , n `hasKey` consDataConKey = [rdr_name] -- see note below
512 | isDataOcc occ = [rdr_name_tc, rdr_name]
513 | otherwise = [rdr_name]
515 occ = rdrNameOcc rdr_name
516 rdr_name_tc = setRdrNameSpace rdr_name tcName
518 -- If the user typed "[]" or "(,,)", we'll generate an Exact RdrName,
519 -- and setRdrNameSpace generates an Orig, which is fine
520 -- But it's not fine for (:), because there *is* no corresponding type
521 -- constructor. If we generate an Orig tycon for GHC.Base.(:), it'll
522 -- appear to be in scope (because Orig's simply allocate a new name-cache
523 -- entry) and then we get an error when we use dataTcOccs in
524 -- TcRnDriver.tcRnGetInfo. Large sigh.
527 %************************************************************************
530 Dealing with rebindable syntax is driven by the
531 Opt_NoImplicitPrelude dynamic flag.
533 In "deriving" code we don't want to use rebindable syntax
534 so we switch off the flag locally
537 %************************************************************************
539 Haskell 98 says that when you say "3" you get the "fromInteger" from the
540 Standard Prelude, regardless of what is in scope. However, to experiment
541 with having a language that is less coupled to the standard prelude, we're
542 trying a non-standard extension that instead gives you whatever "Prelude.fromInteger"
543 happens to be in scope. Then you can
545 import MyPrelude as Prelude
546 to get the desired effect.
548 At the moment this just happens for
549 * fromInteger, fromRational on literals (in expressions and patterns)
550 * negate (in expressions)
551 * minus (arising from n+k patterns)
554 We store the relevant Name in the HsSyn tree, in
555 * HsIntegral/HsFractional/HsIsString
559 respectively. Initially, we just store the "standard" name (PrelNames.fromIntegralName,
560 fromRationalName etc), but the renamer changes this to the appropriate user
561 name if Opt_NoImplicitPrelude is on. That is what lookupSyntaxName does.
563 We treat the orignal (standard) names as free-vars too, because the type checker
564 checks the type of the user thing against the type of the standard thing.
567 lookupSyntaxName :: Name -- The standard name
568 -> RnM (SyntaxExpr Name, FreeVars) -- Possibly a non-standard name
569 lookupSyntaxName std_name
570 = doptM Opt_ImplicitPrelude `thenM` \ implicit_prelude ->
571 if implicit_prelude then normal_case
573 -- Get the similarly named thing from the local environment
574 lookupOccRn (mkRdrUnqual (nameOccName std_name)) `thenM` \ usr_name ->
575 returnM (HsVar usr_name, unitFV usr_name)
577 normal_case = returnM (HsVar std_name, emptyFVs)
579 lookupSyntaxTable :: [Name] -- Standard names
580 -> RnM (SyntaxTable Name, FreeVars) -- See comments with HsExpr.ReboundNames
581 lookupSyntaxTable std_names
582 = doptM Opt_ImplicitPrelude `thenM` \ implicit_prelude ->
583 if implicit_prelude then normal_case
585 -- Get the similarly named thing from the local environment
586 mappM (lookupOccRn . mkRdrUnqual . nameOccName) std_names `thenM` \ usr_names ->
588 returnM (std_names `zip` map HsVar usr_names, mkFVs usr_names)
590 normal_case = returnM (std_names `zip` map HsVar std_names, emptyFVs)
594 %*********************************************************
598 %*********************************************************
601 newLocalsRn :: [Located RdrName] -> RnM [Name]
602 newLocalsRn rdr_names_w_loc
603 = newUniqueSupply `thenM` \ us ->
604 returnM (zipWith mk rdr_names_w_loc (uniqsFromSupply us))
606 mk (L loc rdr_name) uniq
607 | Just name <- isExact_maybe rdr_name = name
608 -- This happens in code generated by Template Haskell
609 | otherwise = ASSERT2( isUnqual rdr_name, ppr rdr_name )
610 -- We only bind unqualified names here
611 -- lookupRdrEnv doesn't even attempt to look up a qualified RdrName
612 mkInternalName uniq (rdrNameOcc rdr_name) (srcSpanStart loc)
614 bindLocatedLocalsRn :: SDoc -- Documentation string for error message
618 bindLocatedLocalsRn doc_str rdr_names_w_loc enclosed_scope
619 = -- Check for duplicate names
620 checkDupNames doc_str rdr_names_w_loc `thenM_`
622 -- Warn about shadowing, but only in source modules
623 ifOptM Opt_WarnNameShadowing
624 (checkShadowing doc_str rdr_names_w_loc) `thenM_`
626 -- Make fresh Names and extend the environment
627 newLocalsRn rdr_names_w_loc `thenM` \ names ->
628 getLocalRdrEnv `thenM` \ local_env ->
629 setLocalRdrEnv (extendLocalRdrEnv local_env names)
630 (enclosed_scope names)
633 bindLocalNames :: [Name] -> RnM a -> RnM a
634 bindLocalNames names enclosed_scope
635 = getLocalRdrEnv `thenM` \ name_env ->
636 setLocalRdrEnv (extendLocalRdrEnv name_env names)
639 bindLocalNamesFV :: [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
640 bindLocalNamesFV names enclosed_scope
641 = do { (result, fvs) <- bindLocalNames names enclosed_scope
642 ; returnM (result, delListFromNameSet fvs names) }
645 -------------------------------------
646 -- binLocalsFVRn is the same as bindLocalsRn
647 -- except that it deals with free vars
648 bindLocatedLocalsFV :: SDoc -> [Located RdrName] -> ([Name] -> RnM (a,FreeVars))
650 bindLocatedLocalsFV doc rdr_names enclosed_scope
651 = bindLocatedLocalsRn doc rdr_names $ \ names ->
652 enclosed_scope names `thenM` \ (thing, fvs) ->
653 returnM (thing, delListFromNameSet fvs names)
655 -------------------------------------
656 bindTyVarsRn :: SDoc -> [LHsTyVarBndr RdrName]
657 -> ([LHsTyVarBndr Name] -> RnM a)
659 -- Haskell-98 binding of type variables; e.g. within a data type decl
660 bindTyVarsRn doc_str tyvar_names enclosed_scope
662 located_tyvars = hsLTyVarLocNames tyvar_names
664 bindLocatedLocalsRn doc_str located_tyvars $ \ names ->
665 enclosed_scope (zipWith replace tyvar_names names)
667 replace (L loc n1) n2 = L loc (replaceTyVarName n1 n2)
669 bindPatSigTyVars :: [LHsType RdrName] -> ([Name] -> RnM a) -> RnM a
670 -- Find the type variables in the pattern type
671 -- signatures that must be brought into scope
672 bindPatSigTyVars tys thing_inside
673 = do { scoped_tyvars <- doptM Opt_ScopedTypeVariables
674 ; if not scoped_tyvars then
677 do { name_env <- getLocalRdrEnv
678 ; let locd_tvs = [ tv | ty <- tys
679 , tv <- extractHsTyRdrTyVars ty
680 , not (unLoc tv `elemLocalRdrEnv` name_env) ]
681 nubbed_tvs = nubBy eqLocated locd_tvs
682 -- The 'nub' is important. For example:
683 -- f (x :: t) (y :: t) = ....
684 -- We don't want to complain about binding t twice!
686 ; bindLocatedLocalsRn doc_sig nubbed_tvs thing_inside }}
688 doc_sig = text "In a pattern type-signature"
690 bindPatSigTyVarsFV :: [LHsType RdrName]
693 bindPatSigTyVarsFV tys thing_inside
694 = bindPatSigTyVars tys $ \ tvs ->
695 thing_inside `thenM` \ (result,fvs) ->
696 returnM (result, fvs `delListFromNameSet` tvs)
698 bindSigTyVarsFV :: [Name]
701 bindSigTyVarsFV tvs thing_inside
702 = do { scoped_tyvars <- doptM Opt_ScopedTypeVariables
703 ; if not scoped_tyvars then
706 bindLocalNamesFV tvs thing_inside }
708 extendTyVarEnvFVRn :: [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
709 -- This function is used only in rnSourceDecl on InstDecl
710 extendTyVarEnvFVRn tyvars thing_inside = bindLocalNamesFV tyvars thing_inside
712 -------------------------------------
713 checkDupNames :: SDoc
716 checkDupNames doc_str rdr_names_w_loc
717 = -- Check for duplicated names in a binding group
718 mappM_ (dupNamesErr doc_str) dups
720 (_, dups) = removeDups (\n1 n2 -> unLoc n1 `compare` unLoc n2) rdr_names_w_loc
722 -------------------------------------
723 checkShadowing doc_str loc_rdr_names
724 = getLocalRdrEnv `thenM` \ local_env ->
725 getGlobalRdrEnv `thenM` \ global_env ->
727 check_shadow (L loc rdr_name)
728 | rdr_name `elemLocalRdrEnv` local_env
729 || not (null (lookupGRE_RdrName rdr_name global_env ))
730 = addWarnAt loc (shadowedNameWarn doc_str rdr_name)
731 | otherwise = returnM ()
733 mappM_ check_shadow loc_rdr_names
737 %************************************************************************
739 \subsection{Free variable manipulation}
741 %************************************************************************
745 mapFvRn f xs = mappM f xs `thenM` \ stuff ->
747 (ys, fvs_s) = unzip stuff
749 returnM (ys, plusFVs fvs_s)
753 %************************************************************************
755 \subsection{Envt utility functions}
757 %************************************************************************
760 warnUnusedModules :: [(ModuleName,SrcSpan)] -> RnM ()
761 warnUnusedModules mods
762 = ifOptM Opt_WarnUnusedImports (mappM_ bleat mods)
764 bleat (mod,loc) = addWarnAt loc (mk_warn mod)
765 mk_warn m = vcat [ptext SLIT("Module") <+> quotes (ppr m)
766 <+> text "is imported, but nothing from it is used,",
767 nest 2 (ptext SLIT("except perhaps instances visible in")
769 ptext SLIT("To suppress this warning, use:")
770 <+> ptext SLIT("import") <+> ppr m <> parens empty ]
773 warnUnusedImports, warnUnusedTopBinds :: [GlobalRdrElt] -> RnM ()
774 warnUnusedImports gres = ifOptM Opt_WarnUnusedImports (warnUnusedGREs gres)
775 warnUnusedTopBinds gres = ifOptM Opt_WarnUnusedBinds (warnUnusedGREs gres)
777 warnUnusedLocalBinds, warnUnusedMatches :: [Name] -> RnM ()
778 warnUnusedLocalBinds names = ifOptM Opt_WarnUnusedBinds (warnUnusedLocals names)
779 warnUnusedMatches names = ifOptM Opt_WarnUnusedMatches (warnUnusedLocals names)
781 -------------------------
784 = warnUnusedBinds [(n,p) | GRE {gre_name = n, gre_prov = p} <- gres]
786 warnUnusedLocals names
787 = warnUnusedBinds [(n,LocalDef) | n<-names]
789 warnUnusedBinds :: [(Name,Provenance)] -> RnM ()
790 warnUnusedBinds names = mappM_ warnUnusedName (filter reportable names)
791 where reportable (name,_)
792 | isWiredInName name = False -- Don't report unused wired-in names
793 -- Otherwise we get a zillion warnings
795 | otherwise = reportIfUnused (nameOccName name)
797 -------------------------
799 warnUnusedName :: (Name, Provenance) -> RnM ()
800 warnUnusedName (name, LocalDef)
801 = addUnusedWarning name (srcLocSpan (nameSrcLoc name))
802 (ptext SLIT("Defined but not used"))
804 warnUnusedName (name, Imported is)
807 warn spec = addUnusedWarning name span msg
809 span = importSpecLoc spec
810 pp_mod = quotes (ppr (importSpecModule spec))
811 msg = ptext SLIT("Imported from") <+> pp_mod <+> ptext SLIT("but not used")
813 addUnusedWarning name span msg
816 nest 2 $ pprNonVarNameSpace (occNameSpace (nameOccName name))
817 <+> quotes (ppr name)]
821 addNameClashErrRn rdr_name names
822 = addErr (vcat [ptext SLIT("Ambiguous occurrence") <+> quotes (ppr rdr_name),
823 ptext SLIT("It could refer to") <+> vcat (msg1 : msgs)])
826 msg1 = ptext SLIT("either") <+> mk_ref np1
827 msgs = [ptext SLIT(" or") <+> mk_ref np | np <- nps]
828 mk_ref gre = quotes (ppr (gre_name gre)) <> comma <+> pprNameProvenance gre
830 shadowedNameWarn doc shadow
831 = hsep [ptext SLIT("This binding for"),
833 ptext SLIT("shadows an existing binding")]
836 unknownNameErr rdr_name
837 = sep [ptext SLIT("Not in scope:"),
838 nest 2 $ pprNonVarNameSpace (occNameSpace (rdrNameOcc rdr_name))
839 <+> quotes (ppr rdr_name)]
841 unknownInstBndrErr cls op
842 = quotes (ppr op) <+> ptext SLIT("is not a (visible) method of class") <+> quotes (ppr cls)
845 = ptext SLIT("Illegal binding of built-in syntax:") <+> ppr (rdrNameOcc name)
846 -- The rdrNameOcc is because we don't want to print Prelude.(,)
848 dupNamesErr :: SDoc -> [Located RdrName] -> RnM ()
849 dupNamesErr descriptor located_names
851 vcat [ptext SLIT("Conflicting definitions for") <+> quotes (ppr name1),
852 locations, descriptor]
854 L _ name1 = head located_names
855 locs = map getLoc located_names
856 big_loc = foldr1 combineSrcSpans locs
857 one_line = isOneLineSpan big_loc
858 locations | one_line = empty
859 | otherwise = ptext SLIT("Bound at:") <+>
860 vcat (map ppr (sortLe (<=) locs))
862 badQualBndrErr rdr_name
863 = ptext SLIT("Qualified name in binding position:") <+> ppr rdr_name
866 = vcat [ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op),
867 ftext FSLIT("Use -fglasgow-exts to avoid this warning")]